如何通过Grunt运行节点脚本?

我正在寻找通过我的gruntfile运行节点命令。 我只需要运行:

node index.js 

作为任何其他任务之前的第一项任务。 我试着四处搜寻,但还没有find答案。 我相信这可能是简单的,但我不知道如何。 我是否需要加载nmp任务?

这是我的Gruntfile的样子:

 "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”。

这样做的标准方法是使用grunt-run任务 。

做:

 npm install grunt-run --save-dev 

并在你的咕噜文件:

 grunt.loadNpmTasks('grunt-run'); 

然后一些configuration(从文档):

 grunt.initConfig({ run: { options: { // Task-specific options go here. }, your_target: { cmd: 'node', args: [ 'index.js' ] } } }) 

然后你把任务注册改成这样:

 grunt.registerTask('default', ['run', 'clean', 'jshint', 'mustache_render']);