我在项目根目录下有一个Gruntfile。 我还通过Bower在app/components/jquery目录中安装了jQuery。
作为我的Gruntfile的一部分,我想对jQuery Gruntfile运行一些命令以构建自定义版本库。
我如何从我的Gruntfile访问其Gruntfile?
我在项目根目录下有一个Gruntfile。 我还通过Bower在app/components/jquery目录中安装了jQuery。
作为我的Gruntfile的一部分,我想对jQuery Gruntfile运行一些命令以构建自定义版本库。
我如何从我的Gruntfile访问其Gruntfile?
您可以创建一个简单的任务,在您想要的文件夹中生成grunt:
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();
});
});
如果你想要获得控制台输出,可以基于 @Sindre 的回答,只需要在代码中使用 console.log 输出 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();
});
});
根据 @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);
});
不确定是否可行,但你可以尝试一下。你的jQuery Gruntfile通过“module.exports”进行导出。这应该意味着你可以在你的代码中require它并使用它。
var jQueryGrunt = require('path-to-jquery-gruntfile');
jQueryGrunt.task.run(['your-task-you-want-to-run']);
很有趣听到那是否有效...
watch:true
运行我的单元测试。我想要能够运行它并在屏幕上看到输出。 - pgpb.padillaconsole.log(result.stdout);
。 - moefinley