Start and stop node asynchronously from your grunt build.
APACHE-2.0 License
Start and stop node asynchronously from your grunt build.
Grunt task to simplify testing by asynchrounously starting and stopping one or more node servers during the grunt build. This plugin contains two separate tasks run_node
and stop_node
. run_node
will typically be used before test to start the system under test and stop_node
will typically be used once the tests have been completed.
This plugin requires Grunt ^1.0
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-run-node --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-run-node');
In your project's Gruntfile, add a section named run_node
to the data object passed into grunt.initConfig()
to start one or more node servers asynchronously. To stop previously started node servers use the stop_node
task.
grunt.initConfig({
run_node: {
start: {
options: {
cwd: 'test',
stdio: [ 'ignore', 'ignore', 'ignore' ],
env: {
'foo': 'bar'
},
detached: true
},
files: { src: [ 'server/server.js'] }
}
},
stop_node: {
stop: {}
}
});
Type: String
Default value: process.cwd()
Used to set the current working directory for the executing node processes.
Type: String
Default value: [ 'ignore', (grunt.option('verbose') ? process.stdout : 'ignore'), process.stderr ]
The stdio
option is an array where each index corresponds to a stream in the node process.
The value is one of the following:
pipe
- Create a pipe between the child process and the parent process. The parent end of the pipe is exposed to the parent as a property on the child_process object as ChildProcess.stdio[file descriptor]. Pipes created for file descriptors 0 - 2 are also available as ChildProcess.stdin, ChildProcess.stdout and ChildProcess.stderr, respectively.ipc
- Create an IPC channel for passing messages/file descriptors between parent and child. A ChildProcess may have at most one IPC stdio file descriptor. Setting this option enables the ChildProcess.send() method. If the child writes JSON messages to this file descriptor, then this will trigger ChildProcess.on('message'). If the child is a Node.js program, then the presence of an IPC channel will enable process.send() and process.on('message').ignore
- Do not set this file descriptor in the child. Note that Node will always open file descriptor 0 - 2 for the processes it spawns. When any of these is ignored node will open /dev/null and attach it to the child's file descriptor.As a shorthand, the stdio argument may also be one of the following strings, rather than an array:
'ignore'
- ['ignore', 'ignore', 'ignore']
'pipe'
- ['pipe', 'pipe', 'pipe']
'inherit'
- [process.stdin, process.stdout, process.stderr]
or [0,1,2]
Type: Object
Default value: {}
Env can be used to specify environment key-value variables that will be visible to the new process.
Type: Boolean
Default value: false
If the detached option is set, the child process will be made the leader of a new process group. This makes it possible for the child to continue running after the parent exits.
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.