Permalink
Browse files

replace charm with colors; didn't work in node 0.10

  • Loading branch information...
1 parent 984029d commit d684efb7182bd866c875be2bb459a692a5661599 @pmuellr committed Oct 8, 2013
Showing with 74 additions and 169 deletions.
  1. +3 −17 Makefile
  2. +0 −7 README.md
  3. +5 −41 lib-src/Executor.coffee
  4. +12 −1 lib-src/FileSet.coffee
  5. +9 −41 lib-src/command.coffee
  6. +2 −0 lib-src/wr.coffee
  7. +6 −31 lib/Executor.js
  8. +14 −3 lib/FileSet.js
  9. +7 −8 lib/FileWatcher.js
  10. +9 −15 lib/command.js
  11. +3 −1 lib/wr.js
  12. +4 −4 package.json
View
20 Makefile
@@ -31,25 +31,11 @@ install:
#-------------------------------------------------------------------------------
build:
- @node_modules/.bin/coffee --bare --compile --output lib lib-src/*.coffee
+ node_modules/.bin/coffee --bare --compile --output lib lib-src/*.coffee
#-------------------------------------------------------------------------------
-vendor: \
- npm_coffee \
- npm_charm \
- npm_optimist
-
-#-------------------------------------------------------------------------------
-npm_coffee:
- npm install coffee-script@1.1.3
-
-#-------------------------------------------------------------------------------
-npm_optimist:
- npm install optimist@0.3.0
-
-#-------------------------------------------------------------------------------
-npm_charm:
- npm install charm@0.0.5
+vendor:
+ npm install
#-------------------------------------------------------------------------------
help:
View
7 README.md
@@ -46,13 +46,6 @@ The following options are available:
> Generate additional diagnostic information.
-`--stdoutcolor color`
-
-`--stderrcolor color`
-
-> Display stdout and stderr in specific colors; color values:
-> `black red green yellow blue magenta cyan white`.
-
`--exec`
> Use exec instead of spawn to run the command.
View
46 lib-src/Executor.coffee
@@ -16,10 +16,6 @@
childProcess = require 'child_process'
-charm = require 'charm'
-charmOut = charm(process.stdout)
-charmErr = charm(process.stderr)
-
#-------------------------------------------------------------------------------
module.exports = class Executor
@@ -76,28 +72,12 @@ class ExecutorExec extends Executor
callback = (error, stdout, stderr) => @done(error, stdout, stderr)
- childProcess.exec(cmd, callback)
+ return childProcess.exec(cmd, callback)
#---------------------------------------------------------------------------
done: (error, stdout, stderr) ->
- if not @opts.stdoutcolor
- process.stdout.write(stdout)
- else
- charmOut
- .push(true)
- .foreground(@opts.stdoutcolor)
- .write(stdout)
- .pop(true)
-
-
- if not @opts.stderrcolor
- process.stderr.write(stderr)
- else
- charmErr
- .push(true)
- .foreground(@opts.stderrcolor)
- .write(stderr)
- .pop(true)
+ process.stdout.write stdout
+ process.stderr.write stderr.red
secs = @timerElapsed()
@@ -131,27 +111,11 @@ class ExecutorSpawn extends Executor
#---------------------------------------------------------------------------
stdout: (data) ->
- if not @opts.stdoutcolor
- process.stdout.write(data)
- return
-
- charmOut
- .push(true)
- .foreground(@opts.stdoutcolor)
- .write(data)
- .pop(true)
+ process.stdout.write stdout
#---------------------------------------------------------------------------
stderr: (data) ->
- if not @opts.stderrcolor
- process.stderr.write(data)
- return
-
- charmErr
- .push(true)
- .foreground(@opts.stderrcolor)
- .write(data)
- .pop(true)
+ process.stderr.write stderr.red
#---------------------------------------------------------------------------
exit: (code, sig) ->
View
13 lib-src/FileSet.coffee
@@ -37,6 +37,9 @@ module.exports = class FileSet
@executor = Executor.getExecutor(@, @opts)
+ @childProcess = null
+ @childStartTime = 0
+
#---------------------------------------------------------------------------
getMtime: (fileName) ->
@allMtimes[fileName] || 0
@@ -87,7 +90,15 @@ module.exports = class FileSet
runCommand: ->
@opts.logInfo "running '#{@cmd}'"
- @executor.run(@cmd)
+ if @childProcess
+ if Date.now() - @childStartTime > 5000
+ @childProcess.kill()
+
+ @childStartTime = Date.now()
+ @childProcess = @executor.run(@cmd)
+
+ @childProcess.on "exit", =>
+ @childProcess = null
#---------------------------------------------------------------------------
expandFiles: ->
View
50 lib-src/command.coffee
@@ -17,13 +17,10 @@
fs = require 'fs'
path = require 'path'
-charm = require 'charm'
optimist = require 'optimist'
wr = require './wr'
-charm = charm(process.stderr)
-
existsSync = fs.existsSync || path.existsSync
#-------------------------------------------------------------------------------
@@ -52,12 +49,6 @@ exports.run = ->
.boolean( 'exec')
.describe('exec', 'run command with exec instead of spawn')
- .string( 'stdoutcolor')
- .describe('stdoutcolor', 'display stdout in the specified color')
-
- .string( 'stderrcolor')
- .describe('stderrcolor', 'display stderr in the specified color')
-
.alias( 'p', 'poll')
.describe('poll', 'use poll-based file watching')
@@ -109,8 +100,6 @@ exports.run = ->
opts.verbose = argv.verbose
opts.chime = argv.chime
- opts.stdoutcolor = argv.stdoutcolor
- opts.stderrcolor = argv.stderrcolor
opts.exec = argv.exec
opts.poll = argv.poll
@@ -174,43 +163,22 @@ getVersion = () ->
logError = (message) ->
date = new Date()
time = date.toLocaleTimeString()
- message = "#{time} wr: #{message}"
- charm
- .push(true)
- .display('bright')
- .background('red')
- .foreground('white')
- .write(message)
- .pop(true)
- .write('\n')
- .down(1)
+ message = "#{time} wr: #{message}".red.inverse.white
+
+ console.log message
#---------------------------------------------------------------------------
logSuccess = (message) ->
date = new Date()
time = date.toLocaleTimeString()
- message = "#{time} wr: #{message}"
- charm
- .push(true)
- .display('bright')
- .background('green')
- .foreground('white')
- .write(message)
- .pop(true)
- .write('\n')
- .down(1)
+ message = "#{time} wr: #{message}".green.inverse.white
+
+ console.log message
#---------------------------------------------------------------------------
logInfo = (message) ->
date = new Date()
time = date.toLocaleTimeString()
- message = "#{time} wr: #{message}"
- charm
- .push(true)
- .display('bright')
- .background('blue')
- .foreground('white')
- .write(message)
- .pop(true)
- .write('\n')
- .down(1)
+ message = "#{time} wr: #{message}".blue.inverse.white
+
+ console.log message
View
2 lib-src/wr.coffee
@@ -14,6 +14,8 @@
# limitations under the License.
#-------------------------------------------------------------------------------
+require "colors"
+
FileSet = require './FileSet'
#-------------------------------------------------------------------------------
View
37 lib/Executor.js
@@ -1,18 +1,11 @@
-// Generated by CoffeeScript 1.5.0
-var Executor, ExecutorExec, ExecutorSpawn, charm, charmErr, charmOut, childProcess,
+// 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');
-charm = require('charm');
-
-charmOut = charm(process.stdout);
-
-charmErr = charm(process.stderr);
-
module.exports = Executor = (function() {
-
Executor.getExecutor = function(fileSet, opts) {
if (opts.exec) {
return new ExecutorExec(fileSet, opts);
@@ -64,7 +57,6 @@ module.exports = Executor = (function() {
})();
ExecutorExec = (function(_super) {
-
__extends(ExecutorExec, _super);
function ExecutorExec(fileSet, opts) {
@@ -83,16 +75,8 @@ ExecutorExec = (function(_super) {
ExecutorExec.prototype.done = function(error, stdout, stderr) {
var secs;
- if (!this.opts.stdoutcolor) {
- process.stdout.write(stdout);
- } else {
- charmOut.push(true).foreground(this.opts.stdoutcolor).write(stdout).pop(true);
- }
- if (!this.opts.stderrcolor) {
- process.stderr.write(stderr);
- } else {
- charmErr.push(true).foreground(this.opts.stderrcolor).write(stderr).pop(true);
- }
+ process.stdout.write(stdout);
+ process.stderr.write(stderr.red);
secs = this.timerElapsed();
if (error) {
this.logError("" + secs + "s - command failed with rc:" + error.code);
@@ -107,7 +91,6 @@ ExecutorExec = (function(_super) {
})(Executor);
ExecutorSpawn = (function(_super) {
-
__extends(ExecutorSpawn, _super);
function ExecutorSpawn(fileSet, opts) {
@@ -133,19 +116,11 @@ ExecutorSpawn = (function(_super) {
};
ExecutorSpawn.prototype.stdout = function(data) {
- if (!this.opts.stdoutcolor) {
- process.stdout.write(data);
- return;
- }
- return charmOut.push(true).foreground(this.opts.stdoutcolor).write(data).pop(true);
+ return process.stdout.write(stdout);
};
ExecutorSpawn.prototype.stderr = function(data) {
- if (!this.opts.stderrcolor) {
- process.stderr.write(data);
- return;
- }
- return charmErr.push(true).foreground(this.opts.stderrcolor).write(data).pop(true);
+ return process.stderr.write(stderr.red);
};
ExecutorSpawn.prototype.exit = function(code, sig) {
View
17 lib/FileSet.js
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.5.0
+// Generated by CoffeeScript 1.6.3
var Executor, FileSet, FileWatcher, existsSync, fs, path;
fs = require('fs');
@@ -12,7 +12,6 @@ FileWatcher = require('./FileWatcher');
existsSync = fs.existsSync || path.existsSync;
module.exports = FileSet = (function() {
-
function FileSet(files, opts) {
this.files = files;
this.opts = opts;
@@ -29,6 +28,8 @@ module.exports = FileSet = (function() {
this.opts.logInfo = (function() {});
}
this.executor = Executor.getExecutor(this, this.opts);
+ this.childProcess = null;
+ this.childStartTime = 0;
}
FileSet.prototype.getMtime = function(fileName) {
@@ -80,8 +81,18 @@ module.exports = FileSet = (function() {
};
FileSet.prototype.runCommand = function() {
+ var _this = this;
this.opts.logInfo("running '" + this.cmd + "'");
- return this.executor.run(this.cmd);
+ if (this.childProcess) {
+ if (Date.now() - this.childStartTime > 5000) {
+ this.childProcess.kill();
+ }
+ }
+ this.childStartTime = Date.now();
+ this.childProcess = this.executor.run(this.cmd);
+ return this.childProcess.on("exit", function() {
+ return _this.childProcess = null;
+ });
};
FileSet.prototype.expandFiles = function() {
View
15 lib/FileWatcher.js
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.5.0
+// Generated by CoffeeScript 1.6.3
var FileWatcher, FileWatcherNoPoll, FileWatcherPoll, existsSync, fs, path,
__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; };
@@ -10,7 +10,6 @@ path = require('path');
existsSync = fs.existsSync || path.existsSync;
module.exports = FileWatcher = (function() {
-
FileWatcher.get = function(fileSet, opts) {
if (opts.poll) {
return new FileWatcherPoll(fileSet, opts);
@@ -53,15 +52,14 @@ module.exports = FileWatcher = (function() {
})();
FileWatcherNoPoll = (function(_super) {
-
__extends(FileWatcherNoPoll, _super);
function FileWatcherNoPoll(fileSet, opts) {
FileWatcherNoPoll.__super__.constructor.apply(this, arguments);
}
FileWatcherNoPoll.prototype.watch = function(files) {
- var file, watcher, _i, _len;
+ var e, file, watcher, _i, _len;
this.watchers = [];
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
@@ -70,7 +68,8 @@ FileWatcherNoPoll = (function(_super) {
persist: true
}, this.getCB(file));
this.watchers.push(watcher);
- } catch (e) {
+ } catch (_error) {
+ e = _error;
this.fileSet.logError("exception watching '" + file + "': " + e);
if (e.code === "EMFILE") {
this.fileSet.logError("increase available file handles with `ulimit -n <number>`");
@@ -95,15 +94,14 @@ FileWatcherNoPoll = (function(_super) {
})(FileWatcher);
FileWatcherPoll = (function(_super) {
-
__extends(FileWatcherPoll, _super);
function FileWatcherPoll(fileSet, opts) {
FileWatcherPoll.__super__.constructor.apply(this, arguments);
}
FileWatcherPoll.prototype.watch = function(files) {
- var file, options, _i, _len, _ref;
+ var e, file, options, _i, _len, _ref;
this.files = files;
options = {
interval: 1000 * this.opts.poll,
@@ -114,7 +112,8 @@ FileWatcherPoll = (function(_super) {
file = _ref[_i];
try {
fs.watchFile(file, options, this.getCB(file));
- } catch (e) {
+ } catch (_error) {
+ e = _error;
this.fileSet.logError("exception watching '" + file + "': " + e);
return;
}
View
24 lib/command.js
@@ -1,18 +1,14 @@
-// Generated by CoffeeScript 1.5.0
-var charm, existsSync, fs, getDotWrContents, getVersion, logError, logInfo, logSuccess, optimist, path, printHelp, wr;
+// Generated by CoffeeScript 1.6.3
+var existsSync, fs, getDotWrContents, getVersion, logError, logInfo, logSuccess, optimist, path, printHelp, wr;
fs = require('fs');
path = require('path');
-charm = require('charm');
-
optimist = require('optimist');
wr = require('./wr');
-charm = charm(process.stderr);
-
existsSync = fs.existsSync || path.existsSync;
exports.run = function() {
@@ -27,7 +23,7 @@ exports.run = function() {
}
}
optimist = optimist(args);
- argv = optimist.usage('Usage: $0 [options] command [file ...]').alias('v', 'verbose').boolean('verbose').describe('verbose', 'generate verbose diagnostics').alias('c', 'chime')["default"]('chime', 5).describe('chime', 'generate a diagnostic every so many minutes').boolean('exec').describe('exec', 'run command with exec instead of spawn').string('stdoutcolor').describe('stdoutcolor', 'display stdout in the specified color').string('stderrcolor').describe('stderrcolor', 'display stderr in the specified color').alias('p', 'poll').describe('poll', 'use poll-based file watching').boolean('V').describe('V', 'print the version').boolean('?').describe('?', 'print help').boolean('h').describe('h', 'print help').argv;
+ argv = optimist.usage('Usage: $0 [options] command [file ...]').alias('v', 'verbose').boolean('verbose').describe('verbose', 'generate verbose diagnostics').alias('c', 'chime')["default"]('chime', 5).describe('chime', 'generate a diagnostic every so many minutes').boolean('exec').describe('exec', 'run command with exec instead of spawn').alias('p', 'poll').describe('poll', 'use poll-based file watching').boolean('V').describe('V', 'print the version').boolean('?').describe('?', 'print help').boolean('h').describe('h', 'print help').argv;
if (argv.chime) {
if (typeof argv.chime !== 'number') {
console.error("the chime option value is not a number");
@@ -63,8 +59,6 @@ exports.run = function() {
opts = {};
opts.verbose = argv.verbose;
opts.chime = argv.chime;
- opts.stdoutcolor = argv.stdoutcolor;
- opts.stderrcolor = argv.stderrcolor;
opts.exec = argv.exec;
opts.poll = argv.poll;
opts.logError = logError;
@@ -126,22 +120,22 @@ logError = function(message) {
var date, time;
date = new Date();
time = date.toLocaleTimeString();
- message = "" + time + " wr: " + message;
- return charm.push(true).display('bright').background('red').foreground('white').write(message).pop(true).write('\n').down(1);
+ message = ("" + time + " wr: " + message).red.inverse.white;
+ return console.log(message);
};
logSuccess = function(message) {
var date, time;
date = new Date();
time = date.toLocaleTimeString();
- message = "" + time + " wr: " + message;
- return charm.push(true).display('bright').background('green').foreground('white').write(message).pop(true).write('\n').down(1);
+ message = ("" + time + " wr: " + message).green.inverse.white;
+ return console.log(message);
};
logInfo = function(message) {
var date, time;
date = new Date();
time = date.toLocaleTimeString();
- message = "" + time + " wr: " + message;
- return charm.push(true).display('bright').background('blue').foreground('white').write(message).pop(true).write('\n').down(1);
+ message = ("" + time + " wr: " + message).blue.inverse.white;
+ return console.log(message);
};
View
4 lib/wr.js
@@ -1,6 +1,8 @@
-// Generated by CoffeeScript 1.5.0
+// Generated by CoffeeScript 1.6.3
var FileSet;
+require("colors");
+
FileSet = require('./FileSet');
module.exports.run = function(cmd, files, opts) {
View
8 package.json
@@ -2,7 +2,7 @@
"name": "wr",
"description": "watch files and run a command when they change",
"author": "Patrick Mueller",
- "version": "1.2.2",
+ "version": "1.3.0",
"tags": [ "command", "watch" ],
"licenses": [
@@ -21,12 +21,12 @@
},
"dependencies": {
- "optimist": "0.3.x",
- "charm": "0.1.x"
+ "optimist": "0.6.x",
+ "colors": "0.6.x"
},
"devDependencies": {
- "coffee-script": "1.5.x"
+ "coffee-script": "1.6.x"
},
"main" : "./lib/command",

0 comments on commit d684efb

Please sign in to comment.