Skip to content

Instantly share code, notes, and snippets.

@goatslacker
Created June 27, 2011 23:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goatslacker/1050077 to your computer and use it in GitHub Desktop.
Save goatslacker/1050077 to your computer and use it in GitHub Desktop.
Sequentially execute commands NodeJS
#!/usr/bin/env node
/*global require process */
const exec = require('child_process').exec;
/**
Returns a function to be executed
@param cmd {string} the command to run
@param callback {Function} the Function to execute after the command finishes running
*/
var run = function (cmd, callback) {
return function () {
console.log("Now running: " + cmd);
setTimeout(function () {
exec(cmd, function (err, stdout) {
if (err) {
throw err;
}
console.log(stdout);
if (callback) {
callback();
}
});
}, 1);
};
},
/**
Executes a set of instructions -- recursively builds using the run() function
@param instructions {Array} the commands to execute
@return Function
*/
build = function () {
var cmd = null,
instructions = Array.prototype.slice.call(arguments, 0);
if (instructions.length === 0) {
return cmd;
} else {
cmd = instructions.shift();
return run(cmd, build.apply(this, instructions));
}
};
(function () {
// example
var go = build(
'echo "hello"',
'echo "world"'
);
go();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment