2012-12-15 49 views
3

所以,我有下面的grunt文件。我想添加一個任務來啓動我的節點應用程序,並觀察目錄中的更改並重新啓動。我一直在使用supervisor,node-dev(這很棒),但我想運行一個命令並啓動我的整個應用程序。有一個簡單的方法來做到這一點,但我只是想念它。這是寫在CoffeeScript中,以及(不知道這會改變一切)...從咕嚕手錶中運行節點應用程序

module.exports = function(grunt) { 
    grunt.initConfig({ 
    /*exec: { 
     startApi: { 
      command: "npm run-script start-api" 
     } 
    },*/ 
    //static server 
    server: { 
     port: 3333, 
     base: './public', 
     keepalive: true 
    }, 

    // Coffee to JS compilation 
    coffee: { 
     compile: { 
      files: { 
       './public/js/*.js': './src/client/app/**/*.coffee' 
      }, 
      options: { 
       //basePath: 'app/scripts' 
      } 
     } 
    }, 


    mochaTest: { 
     all: ['test/**/*.*'] 
    }, 


    watch: { 
     coffee: { 
      files: './src/client/app/**/*.coffee', 
      tasks: 'coffee' 
     }, 
     mochaTest: { 
      files: 'test/**/*.*', 
      tasks: 'mochaTest' 
     } 
    } 
}); 

grunt.loadNpmTasks('grunt-contrib-coffee'); 
grunt.loadNpmTasks('grunt-mocha-test'); 
//grunt.loadNpmTasks('grunt-exec'); 

grunt.registerTask('default', 'server coffee mochaTest watch'); 
}; 

正如你可以在註釋中看到,我試圖咕嚕-EXEC,但節點命令停止的其他任務的執行。

回答

8

您可以設置咕嚕運行默認任務和手錶任務當您啓動節點應用:

在app.js

var cp = require('child_process'); 
var grunt = cp.spawn('grunt', ['--force', 'default', 'watch']) 

grunt.stdout.on('data', function(data) { 
    // relay output to console 
    console.log("%s", data) 
}); 

然後只需運行node app正常!

Credit

+0

嘿你有一個更新的參考鏈接?我試圖找到它,但沒有運氣。 也不是創建一個子進程,我們可以不只是使用grunt-exec通過主管啓動應用程序嗎? – Leonidas

+0

看起來這個博客現在正在關閉:/我沒有任何其他參考。至於使用監督,這可能是可能的,但我不熟悉它。 – flynfish

+0

謝謝。關於你的例子,爲什麼不從Grunt運行你的應用程序而不是其他方式?雖然我想這樣,你可以使用像nodemon這樣的東西,它會在有變化時重新啓動服務器,然後再次運行grunt。 – Leonidas