Files
elasticsearch-js/scripts/_steps.js
2013-12-04 20:11:24 -06:00

103 lines
2.4 KiB
JavaScript

module.exports = function (argv, steps) {
var async = require('async');
var fs = require('fs');
var path = require('path');
var format = require('util').format;
var cp = require('child_process');
function log() {
var out = format.apply(console, arguments);
if (argv.verbose) {
out = '\n' + out + '\n';
}
console.log(out);
}
var tasks = {
run: function (params, exitCb) {
var cmd = params.cmd;
var args = params.args;
var opts = {
stdio: argv.verbose ? 'inherit' : 'ignore'
};
if (params.cwd) {
opts.cwd = path.resolve(params.cwd);
}
log('running', cmd, args.join(' '), (opts.cwd ? 'in ' + opts.cwd : ''));
var proc = cp.spawn(cmd, args, opts);
proc.on('error', function (err) {
console.error('Error! --', err.message);
process.exit(1);
});
proc.on('exit', function (status) {
if (status) {
console.error('Error! --', cmd, 'exit status was', status);
process.exit(1);
} else {
exitCb();
}
});
},
copy: function (params, done) {
var from = params.from;
var to = params.to;
log('copying', from, 'to', to);
var read = fs.createReadStream(from);
var write = fs.createWriteStream(to);
read.pipe(write);
read.on('error', function (err) {
console.error('unable to read: ' + from);
console.error(err.message);
process.exit(1);
});
write.on('error', function (err) {
console.error('unable to write to: ' + to);
console.error(err.message);
process.exit(1);
});
write.on('finish', function () {
done();
});
}
};
async.forEachSeries(steps, function (args, done) {
// pass the callback to the task
args.push(done);
// get the task name
var taskName = args.shift();
// find the task
var task = tasks;
taskName.split('.').forEach(function (name) {
if (task && task[name]) {
task = task[name];
} else {
throw new Error(taskName + ' is an invalid task, unable to get ' + name + ' from ' + task);
}
});
if (typeof task === 'function') {
task.apply(null, args);
} else {
throw new Error(taskName + ' is an invalid task, does not resolve to a function.');
}
}, function () {
console.log('✔︎');
process.exit();
});
};