2013-07-26 50 views
21

是否可以從Grunt任務中啓動MongoDB?基本上,當我使用grunt server運行我的開發環境時,我希望它啓動MongoDB服務器,也可以運行mongod從Grunt任務中啓動MongoDB

+0

對不起@SindreSorhus,我從來沒有回來工作。答案現在被接受。 – claydiffrient

+0

grunt-shell正在阻塞,因此該命令會掛起構建步驟的其餘部分。 – JJJ

回答

20

您可以使用grunt-shell運行命令:

grunt.initConfig({ 
    shell: { 
     mongo: { 
      command: 'mongod' 
     } 
    } 
}); 
39

您可以使用grunt-shell-spawn做到這一點。以前的答案建議grunt-shell,它在主進程中同步運行 - 阻止其他任務的執行。

shell: { 
    mongo: { 
     command: 'mongod', 
     options: { 
      async: true 
     } 
    } 
} 
23

。如果你想添加到JJJ的答案,使用grunt-shell-spawn確保每個項目都有它有它自己的數據自己的MongoDB實例,你這樣做:

shell: { 
    mongodb: { 
     command: 'mongod --dbpath ./data/db', 
     options: { 
      async: true, 
      stdout: false, 
      stderr: true, 
      failOnError: true, 
      execOptions: { 
       cwd: '.' 
      } 
     } 
    } 
}, 

這個例子也只打印出錯誤。

然後,您想補充shell:mongodbgrunt server任務列表(最好是第一任務),添加data.gitignore(假設你使用GIT)和你去好。

+0

使用默認的mongo設置,我使用了不帶初始目錄標記的db路徑'/ data/db'。除此之外,這是JJJ已經很好的答案的一個很好的補充。 – Scott