包含n个子目录的Dynamic Grunt [英] Dynamic Grunt involving n subdirectories

查看:163
本文介绍了包含n个子目录的Dynamic Grunt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这些都是git子模块,并且有相应的Gruntfile,package.json等。我想要做的是相同的命令序列,但取决于各自的package.json。



我的命令列表是这样的:

  npm install 
grunt dist
将应用程序/ css / [fileName] .css(从package.json)复制到css /
复制应用程序/ js / [fileName]。 js to js /
将app / js / [fileName] .html复制到/

是有一个插件或者我忽略了,我可以用它来做这件事吗?如果可能的话,我不想静态地做 - 我想只需要更新子模块列表就可以使用。

解决方案

我不知道任何预先构建的Grunt任务会为您完成,但编写任务并不困难。你需要引入Node fs 模块来处理文件系统,显然还有其他一些事情要做......这里有一些关于它的一般结构代码和一些 TODO 的:

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

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

... //所有其​​他Grunt配置

//新任务的选项
copymodulefiles:{
all:{
rootDir:apps /
}
}

});

//这是我们的自定义任务定义
grunt.registerMultiTask(copymodulefiles,从子项目复制文件,function(){
var done = this.async (),//告诉Grunt这是一个异步任务
root = grunt.config(this.name +。+ this.target +.rootDir),$ b $ modules = fs.readdirSync( root);

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

// TODO:在pkgJson变量中找到你需要的东西
//将文件从任何地方复制到任何地方

//你可以这样写一个文件:
fs.writeFile(theFilenameToWrite,新文件的内容,function(err){
//(检查错误!)

//记录它
grunt.log.ok(file written!);
});

});

//确定何时完成并调用done() 告诉Grunt一切完成
//调用Grunt的完成方法来表示任务完成
done();

});
};


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.

解决方案

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();

  });
};

这篇关于包含n个子目录的Dynamic Grunt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆