Skip to content

Commit

Permalink
new build
Browse files Browse the repository at this point in the history
  • Loading branch information
Myles Byrne committed Mar 19, 2012
1 parent 511aa58 commit 72ef204
Showing 1 changed file with 29 additions and 63 deletions.
92 changes: 29 additions & 63 deletions lib/wach-cli.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,54 @@
(function() {
var exit, localTime, logInfo, minimatch, npmVersion, passesGlobFilters, path, spawn, substitutePath, support, termColor, termColorWrap, usage, watch;
var exit, localTime, log, matchesGlobs, minimatch, npmVersion, parseArgs, path, spawn, substitutePath, usage, watch, _ref;
path = require('path');
spawn = require('child_process').spawn;
minimatch = require('minimatch');
support = require('./support');
_ref = require('./support'), parseArgs = _ref.parseArgs, npmVersion = _ref.npmVersion, log = _ref.log, localTime = _ref.localTime, exit = _ref.exit, matchesGlobs = _ref.matchesGlobs;
watch = require('./wach');
this.run = function(args) {
var command, commandRunning, help, only, version, _ref;
_ref = support.parseArgs(args), help = _ref.help, version = _ref.version, command = _ref.command, only = _ref.only;
if (help != null) {
this.run = function(rawArgs) {
var args, command, commandRunning, cwd, help, only, version, _ref2;
args = parseArgs(rawArgs);
_ref2 = parseArgs(rawArgs), help = _ref2.help, version = _ref2.version, command = _ref2.command, only = _ref2.only;
if (args.help != null) {
exit(0, usage);
}
if (version != null) {
if (args.version != null) {
exit(0, npmVersion());
}
if (command == null) {
if (args.command == null) {
exit(1, usage);
}
logInfo("Will run: " + command);
if (only.length === 0) {
logInfo("when any files added or updated.");
log.info("Will run: " + args.command);
if (args.only.length === 0) {
log.info("when any files added or updated");
} else {
logInfo("when files matching {" + (only.join(',')) + "} added or updated");
log.info("when files matching {" + (args.only.join(',')) + "} added or updated");
}
if (args.except.length !== 0) {
log.info("except those matching {" + (args.except.join(',')) + "}");
}
commandRunning = false;
return watch(process.cwd(), function(changedPath) {
cwd = process.cwd();
return watch(cwd, function(changedPath) {
var child, commandWithPathSubsitution;
changedPath = path.relative(process.cwd(), changedPath);
changedPath = path.relative(cwd, changedPath);
if (commandRunning) {
return;
}
if (!path.existsSync(changedPath)) {
return;
}
if (!passesGlobFilters(changedPath, only)) {
if ((args.only.length !== 0) && (!matchesGlobs(changedPath, args.only))) {
return;
}
if ((args.except.length !== 0) && (matchesGlobs(changedPath, args.except))) {
return;
}
commandWithPathSubsitution = substitutePath(command, changedPath);
logInfo("");
logInfo("changed: " + changedPath + " (" + (localTime()) + ")");
logInfo("running: " + commandWithPathSubsitution);
logInfo("");
commandWithPathSubsitution = substitutePath(args.command, changedPath);
log.info("");
log.info("changed: " + changedPath + " (" + (localTime()) + ")");
log.info("running: " + commandWithPathSubsitution);
log.info("");
child = spawn('sh', ['-c', commandWithPathSubsitution]);
commandRunning = true;
child.stdout.pipe(process.stdout);
Expand All @@ -53,47 +61,5 @@
substitutePath = function(command, path) {
return command.replace('{}', path);
};
passesGlobFilters = function(path, filters) {
var exp, pass, _i, _len;
if (filters.length === 0) {
return true;
} else {
pass = false;
for (_i = 0, _len = filters.length; _i < _len; _i++) {
exp = filters[_i];
if (minimatch(path, exp)) {
pass = true;
}
}
return pass;
}
};
logInfo = function(msg) {
return console.error(termColorWrap('0;38;5;246', "- " + msg));
};
termColorWrap = function(code, str) {
return termColor(code) + str + termColor();
};
termColor = function(code) {
if (code == null) {
code = '';
}
return '\033' + '[' + code + 'm';
};
localTime = function() {
return (new Date).toTimeString().split(' ')[0];
};
exit = function(status, message) {
if (message != null) {
console.log(message);
}
return process.exit(status);
};
npmVersion = function() {
return JSON.parse(require('fs').readFileSync(__dirname + '/../package.json')).version;
};
usage = "Usage:\n wach [options] <command>\n\nRequired:\n <command>\n Run every time an update occurs in the directory being monitored.\n The `@` will be subsituted with the path that changed.\n\nOptions:\n -o|--only <glob>\n Only run <command> when the path that changed matches <glob>. Quote the\n glob or add a trailing comma to prevent your shell from automatically\n expanding it.\n\nExamples:\n wach make\n wach -o *.c, make\n wach -o *.coffee, coffee @\n TEST_DIR=generators wach -o **/*.rb, bundle exec rake test";
this._test = {
passesGlobFilters: passesGlobFilters
};
usage = "Run a command every when file changes occur in the current directory. If\nthe command you want to run is a long running process like a web server\nsee `wachs`\n\nUsage:\n wach [options] <command>\n\nRequired:\n <command>\n Run every time an update occurs in the directory being monitored.\n If the command includes `{}` it will be subsituted for the path that changed.\n\nOptions:\n -o|--only <globs>\n Only run <command> when the path that changed matches <globs>.\n\n -e|--except <globs>\n Only run <command> when the path that changed doesn't match <globs>.\n\n Quote the <globs> (\"*.c\") or add a trailing comma (*.c,) to prevent your shell from\n automatically expanding them.\n\nExamples:\n wach make\n wach -o *.c, make\n wach -o *.coffee, coffee {}\n TEST_DIR=generators wach -o **/*.rb, bundle exec rake test";
}).call(this);

0 comments on commit 72ef204

Please sign in to comment.