Dynamic Grunt involving n subdirectories

喜欢而已 提交于 2020-03-26 04:51:45

问题


I have a folder layout such that:

/ 
-- css/
-- js/
-- apps/
-- -- myFirstApp/
-- -- mySecondApp/
-- -- ...

Each of these are git submodules, and have a corresponding Gruntfile, package.json, etc. What I want to do is the same sequence of commands, but differ depending on the respective package.json.

My command list is this:

npm install
grunt dist
copy app/css/[fileName].css (from package.json) to css/
copy app/js/[fileName].js to js/
copy app/js/[fileName].html to /

Is there a plugin or something I'm overlooking that I can use with grunt to do this? I don't want to do it statically if at all possible -- I'd like to only have to update the submodule list for this to work.


回答1:


I don't know of any pre-built Grunt task that will do this for you, but writing the task isn't so difficult. You'll need to pull in the Node fs module to deal with the filesystem and obviously there will be some other things to do... here's a general structure for it with some code and some TODO's:

var fs = require("fs"),
    path = require("path");

module.exports = function ( grunt ) {
  grunt.initConfig({

    ... // all of your other Grunt config

    // options for our new task
    copymodulefiles: {
      all: {
        rootDir: "apps/"
      }
    }

  });

  // Here's our custom task definition
  grunt.registerMultiTask("copymodulefiles", "Copies files from sub-projects", function() {
    var done = this.async(), // tell Grunt this is an async task
        root = grunt.config(this.name + "." + this.target + ".rootDir"),
        modules = fs.readdirSync(root);

    modules.forEach(function(dirName) {
      var pkg = fs.readFileSync(root + dirName + path.sep + "package.json", "utf8");
          pkgJson = JSON.parse(pkg);

      // TODO: find what you need in the pkgJson variable
      //       copy files from wherever to wherever

      // You can write a file like so:
      fs.writeFile(theFilenameToWrite, "Contents of the new file", function (err) {
        // (check for errors!)

        // log it?
        grunt.log.ok("file written!");
      });

    });

    // Determine when all are complete and call "done()" to tell Grunt everything's complete
    // call Grunt's "done" method to signal task completion
    done();

  });
};



回答2:


Try with grunt-shell i found it perfect and did similar tasks like what you are trying to do.

Have a look at my Gruntfile.js configuration what i have written to run shell commands:

shell: {
        multiple: {
            command: ['bower install',
                      'mv bower_components/** public/',
                  'rm -rf bower_components'
                  ].join('&&')
        }
    }

So here i am running bower, then i am copying its components to public folder and after that i am deleting the bower_components folder. So i guess from here onwards you can customize this script as per your usage.



来源:https://stackoverflow.com/questions/21383719/dynamic-grunt-involving-n-subdirectories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!