【问题标题】:How to transform a npm script to a grunt task?如何将 npm 脚本转换为 grunt 任务?
【发布时间】:2014-09-07 14:23:54
【问题描述】:
我的 nodeJS 有以下脚本。
"scripts": {
"start": "grunt",
"test": "node --debug --harmony node_modules/grunt-cli/bin/grunt test"
}
我正在运行节点 v0.11.13,所以我需要设置 --harmony 标志。在 grunt 上,如果我使用 npm test 启动它们,测试就会正确配置,但我更愿意将它们全部放在 gruntfile 中。有没有办法配置 grunt 来启动服务器并运行测试?
【问题讨论】:
标签:
javascript
node.js
gruntjs
nodemon
【解决方案1】:
您可以使用这些节点标志创建一个生成 grunt 的别名任务,如下所示:
grunt.registerTask('debug', function() {
var done = this.async();
// Specify tasks to run spawned
var tasks = Array.prototype.slice.call(arguments, 0);
grunt.util.spawn({
// Use the existing node path
cmd: process.execPath,
// Add the flags and use process.argv[1] to get path to grunt bin
args: ['--debug', '--harmony', process.argv[1]].concat(tasks),
// Print everything this process is doing to the parent stdio
opts: { stdio: 'inherit' }
}, done);
});
然后你可以启动服务器并运行测试:grunt default debug:test
或者任何组合:
-
grunt server test(在没有节点标志的情况下同时运行)
-
grunt debug:server:test(同时使用节点标志运行)。