2017-03-27 1 views
1

내 gruntfile을 통해 노드 명령을 실행하려고합니다. 다른 작업을 수행하기 전에 첫 번째 작업으로 다음을 실행하면됩니다.어떻게 Grunt를 통해 노드 스크립트를 실행합니까?

node index.js 

나는 주변을 검색해 보았지만 대답을 찾지 못했습니다. 나는 그것이 단순한 무언가일지도 모르다 그러나 나는 확실하지 않다는 것을 믿는다. nmp 작업으로로드해야합니까? 나는 '청소'작업하기 전에 "노드하는 index.js"를 실행하려면

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


}; 

:

이처럼 내 Gruntfile 보이는 방법이다.

+0

흠 ...이 파일을 실행 한 결과는 무엇입니까? –

+0

질문이 업데이트되었습니다. – Blueboye

+0

이 답변을 확인하십시오, 저를 위해 일했습니다! https://stackoverflow.com/a/47304117/5346095 –

답변

3

이렇게하는 표준 방법은 grunt-run task을 사용하는 것입니다.

음주 : 다음

grunt.loadNpmTasks('grunt-run'); 

그리고 (문서에서) 일부 설정 :

npm install grunt-run --save-dev 

그리고 당신의 불평 소리 파일

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 안에 있습니까? 그렇다면 콘텐츠를 공유 할 수 있습니까? –

관련 문제