如何通过Grunt运行节点脚本? [英] How do I run a node script through Grunt?
问题描述
我正在寻找通过我的gruntfile运行一个节点命令。我只需要运行:
I am looking to run a node command through my gruntfile. I just need to run:
node index.js
作为任何其他任务之前的第一个任务。我试着四处搜寻,但还没有找到答案。我相信这可能是简单的,但我不知道如何。我需要加载nmp任务吗?
as the first task before any other tasks. I tried searching around but haven't found the answer. I believe it might be something simple but I am not sure how. Do I need to load in nmp tasks?
这就是我的Gruntfile的样子:
This is how my Gruntfile looks like:
"use strict";
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js'
],
options: {
jshintrc: '.jshintrc',
},
},
// Before generating any new files, remove any previously-created files.
clean: {
tests: ['dist/*']
},
// Configuration to be run (and then tested).
mustache_render: {
json_data: {
files: [
{
data: 'jsons/offer.json',
template: 'offers.mustache',
dest: 'dist/offers.html'
},
{
data: 'jsons/profile.json',
template: 'profile.mustache',
dest: 'dist/profile.html'
}
]
}
}
});
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-mustache-render');
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('default', ['clean', 'jshint', 'mustache_render']);
};
我想在'clean'任务之前运行node index.js。
I want to run "node index.js" before the 'clean' task.
推荐答案
执行此操作的标准方法是使用 grunt-run
任务。
The standard way of doing this is to use the grunt-run
task.
Do:
Do:
npm install grunt-run --save-dev
在你的grunt文件中:
And in your grunt file:
grunt.loadNpmTasks('grunt-run');
然后一些配置文件(来自文档):
And then some config (from the documentation):
grunt.initConfig({
run: {
options: {
// Task-specific options go here.
},
your_target: {
cmd: 'node',
args: [
'index.js'
]
}
}
})
然后您将任务注册更改为:
Then you change the task registration to be this:
grunt.registerTask('default', ['run', 'clean', 'jshint', 'mustache_render']);
这篇关于如何通过Grunt运行节点脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!