Skip to content

Instantly share code, notes, and snippets.

@millermedeiros
Last active September 10, 2022 03:06
Show Gist options
  • Star 73 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save millermedeiros/4724047 to your computer and use it in GitHub Desktop.
Save millermedeiros/4724047 to your computer and use it in GitHub Desktop.
execute multiple shell commands in series on node.js
// USAGE ------
// ============
var shell = require('./shellHelper');
// execute a single shell command
shell.exec('npm test --coverage', function(err){
console.log('executed test');
}});
// execute multiple commands in series
shell.series([
'node build release'
'git add -A',
'git commit --verbose'
], function(err){
console.log('executed many commands in a row');
});
// spawn a child process and execute shell command
// borrowed from https://github.com/mout/mout/ build script
// author Miller Medeiros
// released under MIT License
// version: 0.1.0 (2013/02/01)
// execute a single shell command where "cmd" is a string
exports.exec = function(cmd, cb){
// this would be way easier on a shell/bash script :P
var child_process = require('child_process');
var parts = cmd.split(/\s+/g);
var p = child_process.spawn(parts[0], parts.slice(1), {stdio: 'inherit'});
p.on('exit', function(code){
var err = null;
if (code) {
err = new Error('command "'+ cmd +'" exited with wrong status code "'+ code +'"');
err.code = code;
err.cmd = cmd;
}
if (cb) cb(err);
});
};
// execute multiple commands in series
// this could be replaced by any flow control lib
exports.series = function(cmds, cb){
var execNext = function(){
exports.exec(cmds.shift(), function(err){
if (err) {
cb(err);
} else {
if (cmds.length) execNext();
else cb(null);
}
});
};
execNext();
};
@swick7
Copy link

swick7 commented Jun 24, 2020

I would also like to see a solution that allows me to pipe the output from one command to the next, like any unix shell allows. For example: find . -name "*.js" | sort | uniq | wc -l

@definoob
Copy link

can we pass env variables here like below?

cycle=312 node index.js

@rajiff
Copy link

rajiff commented Mar 9, 2021

I would also like to see a solution that allows me to pipe the output from one command to the next, like any unix shell allows. For example: find . -name "*.js" | sort | uniq | wc -l

you can use async.workflow (https://caolan.github.io/async/v3/), that does exactly what you are hoping for

if that's not enough, please take a look at https://caolan.github.io/highland/ it does in style of streams

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment