Running a grunt task on one Gruntfile from another

2020-02-17 05:29发布

I have a Gruntfile in the root of my project. I also have jQuery installed via Bower in an app/components/jquery directory.

As part of my Gruntfile I'd like to run some commands on the jQuery Gruntfile to build a custom version of the library.

How can I get at their Gruntfile from mine?

标签: gruntjs
4条回答
smile是对你的礼貌
2楼-- · 2020-02-17 06:11

Based on @Sindre's and @Stephen's answer, we can also get the console output "in real time" without being buffered:

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);
});
查看更多
地球回转人心会变
3楼-- · 2020-02-17 06:11

dont know if that works, but you could give it a try. your jQuery Gruntfile is exported via "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...

查看更多
我想做一个坏孩纸
4楼-- · 2020-02-17 06:16

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();
    });
});
查看更多
SAY GOODBYE
5楼-- · 2020-02-17 06:17

If you want to get console output, building on @Sindre's answer, all you have to do is console log the 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();
    });
});
查看更多
登录 后发表回答