22

Is it possible to start MongoDB from within a Grunt task? Basically when I'm running my development environment with grunt server I want it to start up the MongoDB server as well possibly by running mongod.

4

3 回答 3

39

您可以使用grunt-shell-spawn来执行此操作。上一个答案推荐grunt-shell,它在主进程上同步运行——阻塞其他任务的执行。

shell: {
    mongo: {
        command: 'mongod',
        options: {
            async: true
        }
    }
}
于 2013-08-26T15:35:19.210 回答
22

要添加到 JJJ 的答案中,如果您想确保每个项目都有自己的 mongodb 实例和自己的数据,请使用grunt-shell-spawn ,您可以这样做:

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

该示例还仅打印出错误。

然后,您只需添加shell:mongodb到您的grunt server任务列表(最好是第一个任务),添加data到您的.gitignore(假设您正在使用 git),您就可以开始了。

于 2014-03-30T14:39:14.560 回答
20

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

grunt.initConfig({
    shell: {
        mongo: {
            command: 'mongod'
        }
    }
});
于 2013-07-26T09:50:09.133 回答