Created
June 27, 2011 23:05
-
-
Save goatslacker/1050077 to your computer and use it in GitHub Desktop.
Sequentially execute commands NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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