How do I run a node script through Grunt?

醉酒当歌 提交于 2020-01-15 11:18:20

问题


I am looking to run a node command through my gruntfile. I just need to run:

node index.js

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?

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']);


};

I want to run "node index.js" before the 'clean' task.


回答1:


The standard way of doing this is to use the grunt-run task.

Do:

npm install grunt-run --save-dev

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']);


来源:https://stackoverflow.com/questions/43056988/how-do-i-run-a-node-script-through-grunt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!