2017-03-27 6 views
1

私はgruntfileでnodeコマンドを実行しようとしています。私は実行する必要があります:グランツ経由でノードスクリプトを実行するにはどうすればよいですか?

node index.js 

他のタスクの前の最初のタスクとして実行する必要があります。私は周りを検索しようとしたが、答えを見つけていない。私はそれが何か単純かもしれないと信じていますが、私はどのように確信がありません。 nmpタスクを読み込む必要がありますか?

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


}; 

私は「クリーン」タスクの前に「ノードindex.js」を実行したい:

これは私のGruntfileがどのように見えるかです。

+0

hmm ...上記のファイルを実行した結果はどうなりますか? –

+0

質問が更新されました。 – Blueboye

+0

この回答を確認してください、私のために働いた! https://stackoverflow.com/a/47304117/5346095 –

答えて

3

これを行う標準的な方法は、grunt-run taskを使用することです。

の操作を行います。

npm install grunt-run --save-dev 

そして、あなたのイサキファイル内:

grunt.loadNpmTasks('grunt-run'); 

そして(ドキュメントから)いくつかの設定:

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

次にあなたにタスク登録を変更これは:

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

代わりに、「grunt-exec」は同じ必要性を達成できる別のプラグインです – theaccordance

+0

そうです。これを行うには多くの方法があります。私はちょうど私が最もよく知っているものを提案した。 –

+0

@AndrewEisenbergしかし、npmスクリプトはどこで呼び出されますか?私は、この実行タスクを通して呼び出されたnpmスクリプトを見ることができます。 index.jsの中にありますか?はいの場合は、コンテンツを共有してください。 –