Skip to content

Instantly share code, notes, and snippets.

@smonn
Last active December 13, 2015 18:08
Show Gist options
  • Save smonn/4953329 to your computer and use it in GitHub Desktop.
Save smonn/4953329 to your computer and use it in GitHub Desktop.
This is my little play script written in Node.js. I use it to fetch the latest changes from the version control system and build the project(s) I'm working on. Consider it a Makefile written in JavaScript. Probably not entirely necessary, but I find it useful and more fun to maintain than a shell script.
/*jshint node: true */
'use strict';
var exec = require('child_process').exec,
fs = require('fs'),
cwd = 'C:\\working\\directory',
env = {
'PATH': 'C:\\Windows\\System32\\;'
},
commands = {
'command name': 'command-line --with-params'
},
current = 'command name';
function nextCommand() {
var keys = Object.keys(commands),
options = { name: current, cmd: commands[current] },
nextIndex = keys.indexOf(current) + 1;
if (nextIndex >= keys.length + 1 || nextIndex <= 0) {
return null;
}
current = keys[nextIndex];
return options;
}
function runCommand(options) {
console.log('Running ' + options.name + '...');
// uncomment to record the run time
//console.time(options.name);
exec(options.cmd, { env: env, cwd: cwd }, function (error, stdout, stderr) {
if (stdout) {
fs.appendFileSync('stdout.log', '\n\n===== ' + options.name + ' - ' + new Date() + ' ==========\n');
fs.appendFileSync('stdout.log', stdout);
}
if (stderr) {
fs.appendFileSync('stderr.log', '\n\n===== ' + options.name + ' - ' + new Date() + ' ==========\n');
fs.appendFileSync('stderr.log', stderr);
}
if (error !== null) {
console.error(options.name + ' completed with errors, check the log files.');
}
}).on('exit', function (code) {
if (code === 0) {
console.log(options.name + ' completed.');
}
}).on('close', function () {
var nextOptions;
// uncomment to record the run time
//console.timeEnd(options.name);
nextOptions = nextCommand();
if (nextOptions) {
runCommand(nextOptions);
}
});
}
fs.writeFileSync('stdout.log', '');
fs.writeFileSync('stderr.log', '');
runCommand(nextCommand());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment