简体   繁体   English

在另一个Gruntfile上运行grunt任务

[英]Running a grunt task on one Gruntfile from another

I have a Gruntfile in the root of my project. 我的项目根目录中有一个Gruntfile。 I also have jQuery installed via Bower in an app/components/jquery directory. 我还在app / components / jquery目录中通过Bower安装了jQuery。

As part of my Gruntfile I'd like to run some commands on the jQuery Gruntfile to build a custom version of the library. 作为我的Gruntfile的一部分,我想在jQuery Gruntfile上运行一些命令来构建库的自定义版本。

How can I get at their Gruntfile from mine? 我如何从我的Gruntfile中获取?

You can create a simple task that spawns grunt in the folder you want: 您可以创建一个简单的任务,在您想要的文件夹中生成咕噜声

grunt.registerTask('run-grunt', function () {
    var done = this.async();
    grunt.util.spawn({
        grunt: true,
        args: [''],
        opts: {
            cwd: 'app/components/jquery'
        }
    }, function (err, result, code) {
        done();
    });
});

If you want to get console output, building on @Sindre's answer, all you have to do is console log the result.stdout. 如果你想获得控制台输出,建立@Sindre的答案,你所要做的就是控制台记录result.stdout。

grunt.registerTask('run-grunt', function() {
    var cb = this.async();
    grunt.util.spawn({
        grunt: true,
        args: ['clean', 'copy:fonts'],
        opts: {
            cwd: 'bower_components/bootstrap'
        }
    }, function(error, result, code) {
        console.log(result.stdout);
        cb();
    });
});

Based on @Sindre's and @Stephen's answer, we can also get the console output "in real time" without being buffered: 根据@Sindre和@ Stephen的回答,我们还可以“实时”获取控制台输出而不进行缓冲:

grunt.registerTask('run-grunt', function() {
  var cb = this.async();
  var child = grunt.util.spawn({
      grunt: true,
      args: ['clean', 'copy:fonts'],
      opts: {
          cwd: 'bower_components/bootstrap'
      }
  }, function(error, result, code) {
      cb();
  });

  child.stdout.pipe(process.stdout);
  child.stderr.pipe(process.stderr);
});

dont know if that works, but you could give it a try. 不知道这是否有效,但你可以尝试一下。 your jQuery Gruntfile is exported via "module.exports". 你的jQuery Gruntfile是通过“module.exports”导出的。 that should mean, that you can require it in your code and use it. 这应该意味着,您可以在代码中使用它并使用它。

var jQueryGrunt = require('path-to-jquery-gruntfile');
jQueryGrunt.task.run(['your-task-you-want-to-run']);

will be interesting to hear if that works... 听听这是否有效将会很有趣......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM