UNPKG

wr

Version:

watch files and run a command when they change

153 lines (124 loc) 4.14 kB
// Generated by CoffeeScript 1.6.3 var Executor, ExecutorExec, ExecutorSpawn, childProcess, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; childProcess = require('child_process'); module.exports = Executor = (function() { Executor.getExecutor = function(fileSet, opts) { if (opts.exec) { return new ExecutorExec(fileSet, opts); } else { return new ExecutorSpawn(fileSet, opts); } }; function Executor(fileSet, opts) { this.fileSet = fileSet; this.opts = opts; } Executor.prototype.resetAfterCommand = function() { return this.fileSet.resetAfterCommand(); }; Executor.prototype.timerStart = function() { return this.startTime = (new Date()).valueOf(); }; Executor.prototype.timerElapsed = function() { var ms; ms = (new Date()).valueOf() - this.startTime; ms = Math.round(ms / 100); return ms / 10; }; Executor.prototype.logSuccess = function(message) { return this.opts.logSuccess(message); }; Executor.prototype.logError = function(message) { return this.opts.logError(message); }; Executor.prototype.logInfo = function(message) { return this.opts.logInfo(message); }; Executor.prototype.logVerbose = function(message) { if (!this.opts.verbose) { } }; return Executor; })(); ExecutorExec = (function(_super) { __extends(ExecutorExec, _super); function ExecutorExec(fileSet, opts) { ExecutorExec.__super__.constructor.apply(this, arguments); } ExecutorExec.prototype.run = function(cmd) { var callback, _this = this; this.timerStart(); callback = function(error, stdout, stderr) { return _this.done(error, stdout, stderr); }; return childProcess.exec(cmd, callback); }; ExecutorExec.prototype.done = function(error, stdout, stderr) { var secs; process.stdout.write(stdout); process.stderr.write(stderr.red); secs = this.timerElapsed(); if (error) { this.logError("" + secs + "s - command failed with rc:" + error.code); } else { this.logSuccess("" + secs + "s - command succeeded"); } return this.resetAfterCommand(); }; return ExecutorExec; })(Executor); ExecutorSpawn = (function(_super) { __extends(ExecutorSpawn, _super); function ExecutorSpawn(fileSet, opts) { ExecutorSpawn.__super__.constructor.apply(this, arguments); } ExecutorSpawn.prototype.run = function(cmd) { var args, proc, _ref, _this = this; this.timerStart(); _ref = this.splitCmd(cmd), cmd = _ref[0], args = _ref[1]; proc = childProcess.spawn(cmd, args); proc.stdin.end(); proc.stdout.on('data', function(data) { return _this.stdout(data); }); proc.stderr.on('data', function(data) { return _this.stderr(data); }); return proc.on('exit', function(code, sig) { return _this.exit(code, sig); }); }; ExecutorSpawn.prototype.stdout = function(data) { return process.stdout.write(data); }; ExecutorSpawn.prototype.stderr = function(data) { return process.stderr.write(data.red); }; ExecutorSpawn.prototype.exit = function(code, sig) { var secs; secs = this.timerElapsed(); if (code === 0) { this.logSuccess("" + secs + "s - command succeeded"); } else if (code) { this.logError("" + secs + "s - command failed with rc:" + code); } else if (sig) { this.logError("" + secs + "s - command failed with signal:" + sig); } else { this.logError("" + secs + "s - command failed for some unknown reason"); } return this.resetAfterCommand(); }; ExecutorSpawn.prototype.splitCmd = function(cmd) { var args, parts; cmd = cmd.replace(/(^\s+)|(\s+$)/g, ''); parts = cmd.split(/\s+/); cmd = parts[0]; args = parts.slice(1); return [cmd, args]; }; return ExecutorSpawn; })(Executor);