从Grunt任务中启动MongoDB

cla*_*ent 22 shell automation mongodb gruntjs

是否可以从Grunt任务中启动MongoDB?基本上当我运行我的开发环境时,grunt server我希望它可以通过运行来启动MongoDB服务器mongod.

小智 39

您可以使用grunt-shell-spawn来执行此操作.前面的回答建议grunt-shell,它在主进程上同步运行 - 阻止执行其他任务.

shell: {
    mongo: {
        command: 'mongod',
        options: {
            async: true
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


src*_*der 22

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

shell: {
    mongodb: {
        command: 'mongod --dbpath ./data/db',
        options: {
            async: true,
            stdout: false,
            stderr: true,
            failOnError: true,
            execOptions: {
                cwd: '.'
            }
        }
    }
},
Run Code Online (Sandbox Code Playgroud)

该示例还仅打印出错误.

然后shell:mongodb,您只需添加到您的grunt server任务列表(最好是第一个任务),添加data到您的.gitignore(假设您正在使用git)并且您很高兴.


Sin*_*hus 20

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

grunt.initConfig({
    shell: {
        mongo: {
            command: 'mongod'
        }
    }
});
Run Code Online (Sandbox Code Playgroud)