npm install exec-series





A Node module to run commands in order
``javascript
const execSeries = require('exec-series');
execSeries(['echo "foo"', 'echo "bar"'], (err, stdouts, stderrs) => {
if (err) {
throw err;
}
console.log(stdouts); // yields: ['foo\n', 'bar\n']
console.log(stderrs); // yields: ['', '']
});
`
On Linux, you can do almost the same thing with && operator like below:
`javascript
const {exec} = require('child_process');
exec('echo "foo" && echo "bar"', (err, stdout, stderr) => {
//...
});
`
However, some environments, such as Windows PowerShell, don't support && operator. This module helps you to create a cross-platform Node program.
``
npm install exec-series
`javascript`
const execSeries = require('exec-series');
commands: Array of String (the commands to run) Object
options: ([child_process.exec][exec] options with maxBuffer defaulting to 10 MB) Function
callback:
It sequentially runs the commands using [child_process.exec][exec]. If the first command has finished successfully, the second command will run, and so on.
After the last command has finished, it runs the callback function.
When one of the commands fails, it immediately calls the callback function and the rest of the commands won't be run.
#### callback(error, stdoutArray, stderrArray)
error: Error if one of the commands fails, otherwise undefined Array
stdoutArray: of String (stdout of the commands) Array
stderrArray: of String (stderr of the commands)
`javascript`
execSeries([
'mkdir foo',
'echo bar',
'exit 200',
'mkdir baz'
], (err, stdouts, stderrs) => {
err.code; //=> 200
stdouts; //=> ['', 'bar\n', '']
stderrs; //=> ['', '', '']
fs.statSync('foo').isDirectory; //=> true
fs.statSync('baz'); // throw an error
});
Callback function is optional.
`javascript
execSeries(['mkdir foo', 'mkdir bar']);
setTimeout(() => {
fs.statSync('foo').isDirectory(); //=> true
fs.statSync('bar').isDirectory(); //=> true
}, 1000);
``
Copyright (c) 2014 - 2016 Shinnosuke Watanabe
Licensed under the MIT License.
[exec]: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback