Permalink
Browse files

Run action immediately upon execution. closes #1

  • Loading branch information...
1 parent 857cdb1 commit d4f791fcf40d824c4a67927252ec3f3df0263f4b @dfjones committed Mar 21, 2012
Showing with 68 additions and 48 deletions.
  1. +2 −0 README.md
  2. +1 −1 package.json
  3. +28 −23 watchman
  4. +9 −1 watchman.coffee
  5. +28 −23 watchman.js
View
@@ -55,6 +55,8 @@ Later after adding "newFile"...
Tue Apr 12 2011 23:08:36 - watching: test/newFile
+By default, Watchman will perform the action immediately upon running. To prevent this, pass -w as an option.
+
Install
--------
View
@@ -1,6 +1,6 @@
{ "name": "watchman",
"description": "A simple utility to watch files/directories and perform an action when they change.",
- "version": "0.1.4",
+ "version": "0.1.5",
"homepage": "http://github.com/dfjones/watchman",
"keywords": ["file", "filesystem", "change", "command line"],
"author": "Doug Jones <doug@dfjones.net>",
View
@@ -2,27 +2,38 @@
(function() {
var cli, exec, fs, getFormattedDate, log, path;
+
cli = require('cli');
+
fs = require('fs');
+
path = require('path');
+
exec = require('child_process');
+
exec = exec.exec;
+
cli.setUsage('watchman [options] target action');
+
cli.parse({
"ignore-hidden": ['i', "Do not watch hidden files"],
"rate": ['r', "Rate limit actions [like Ns, Nm, Nh, N is int]", "string", null],
- "queue": ['q', "Action queue size", "number", 1]
+ "queue": ['q', "Action queue size", "number", 1],
+ "watch-first": ['w', "Perform the action only when there is a change. Otherwise, action is executed once immediately"]
});
+
getFormattedDate = function() {
var d, s;
d = new Date();
return s = "" + (d.toDateString()) + " " + (d.toTimeString().split(" ")[0]);
};
+
log = function(s) {
var d;
d = getFormattedDate();
return console.log(d + " - " + s);
};
+
cli.main(function(args, options) {
var action, actionQueue, directoryWatcher, execAction, execFromQueue, find_files, queueAction, rate, rateMap, target, testHidden, useQueue, watched, watcher;
if (args.length < 2) {
@@ -40,9 +51,7 @@
useQueue = false;
actionQueue = [];
queueAction = function() {
- if (actionQueue.length < options["queue"]) {
- return actionQueue.push(action);
- }
+ if (actionQueue.length < options["queue"]) return actionQueue.push(action);
};
execFromQueue = function() {
var a, _i, _len;
@@ -53,21 +62,15 @@
return actionQueue = [];
};
execAction = function(toExec) {
- if (toExec != null) {
- toExec;
- } else {
- toExec = action;
- };
+ if (toExec == null) toExec = action;
log("Running action...");
return exec(toExec, function(error, stdout, stderr) {
log("stderr: " + stderr);
return log("stdout: " + stdout);
});
};
watcher = function(file) {
- if (file in watched) {
- return;
- }
+ if (file in watched) return;
watched[file] = true;
log("watching: " + file);
return fs.watchFile(file, {
@@ -86,9 +89,7 @@
});
};
directoryWatcher = function(dir) {
- if (dir in watched) {
- return;
- }
+ if (dir in watched) return;
watched[dir] = true;
log("watching directory: " + dir);
return fs.watchFile(dir, {
@@ -118,9 +119,7 @@
}
find_files = function(target, quiet) {
return path.exists(target, function(exists) {
- if (!quiet && !exists) {
- throw "Target file not found: " + target;
- }
+ if (!quiet && !exists) throw "Target file not found: " + target;
return fs.stat(target, function(err, stats) {
if (err != null) {
console.log(err + " for target: " + target);
@@ -140,17 +139,23 @@
return _results;
});
} else {
- if (!testHidden(target)) {
- return watcher(target);
- }
+ if (!testHidden(target)) return watcher(target);
}
});
});
};
try {
- return find_files(target);
+ find_files(target);
} catch (error) {
- return log("Error: " + error);
+ log("Error: " + error);
+ }
+ if (!options["watch-first"]) {
+ if (useQueue) {
+ return queueAction();
+ } else {
+ return execAction();
+ }
}
});
+
}).call(this);
View
@@ -11,7 +11,8 @@ cli.setUsage('watchman [options] target action')
cli.parse({
"ignore-hidden": ['i', "Do not watch hidden files"],
"rate": ['r', "Rate limit actions [like Ns, Nm, Nh, N is int]", "string", null],
- "queue": ['q', "Action queue size", "number", 1]
+ "queue": ['q', "Action queue size", "number", 1],
+ "watch-first": ['w', "Perform the action only when there is a change. Otherwise, action is executed once immediately"]
})
getFormattedDate = () ->
@@ -109,3 +110,10 @@ cli.main (args, options) ->
find_files(target)
catch error
log("Error: #{error}")
+
+ if not options["watch-first"]
+ if useQueue
+ queueAction()
+ else
+ execAction()
+
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit d4f791f

Please sign in to comment.