Start MongoDB from within a Grunt task

2019-02-04 05:26发布

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.

3条回答
不美不萌又怎样
2楼-- · 2019-02-04 05:46

You can use grunt-shell to run the command:

grunt.initConfig({
    shell: {
        mongo: {
            command: 'mongod'
        }
    }
});
查看更多
霸刀☆藐视天下
3楼-- · 2019-02-04 05:49

To add to JJJ's answer, using grunt-shell-spawn if you want to make sure each project has it's own mongodb instance with it's own data you would do this:

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

The example also prints out only errors.

You would then just add shell:mongodb to your grunt server task list (preferably the first task), add data to your .gitignore (assuming you're using git) and you're good to go.

查看更多
仙女界的扛把子
4楼-- · 2019-02-04 06:05

You can use grunt-shell-spawn to do this. The previous answer recommends grunt-shell, which runs synchronously on the main process - blocking execution of other tasks.

shell: {
    mongo: {
        command: 'mongod',
        options: {
            async: true
        }
    }
}
查看更多
登录 后发表回答