Permalink
Browse files

Use eco template for action. Pass {file} as context. closes #3

For Dio.
  • Loading branch information...
1 parent 4c8876f commit ccb234b8d19f4d3eaa3e180b1c51384fcd6e4fe7 @dfjones committed Jan 4, 2013
Showing with 80 additions and 41 deletions.
  1. +20 −3 README.md
  2. +3 −2 package.json
  3. +21 −13 watchman
  4. +15 −10 watchman.coffee
  5. +21 −13 watchman.js
View
@@ -3,9 +3,7 @@ Watchman
Simple utility that watches files/directories for changes and performs an action when they change. Inspired (and partially based on) the coffee-script compiler.
-I'm sure this is reinventing 100 other, better tools.
-
-Yes I wrote it in coffee-script and NodeJS. Does that make me hip now?
+Yes, I wrote it in coffee-script and NodeJS. Does that make me hip now?
(Calm down, you don't need coffee-script to run it.)
@@ -55,8 +53,27 @@ Later after adding "newFile"...
Tue Apr 12 2011 23:08:36 - watching: test/newFile
+Even better, the action is an [eco](https://github.com/sstephenson/eco) template!
+
+ ./watchman test "echo 'The file <%= @file %> changed!'"
+ Thu Jan 03 2013 22:47:35 - watching directory: test
+ Thu Jan 03 2013 22:47:35 - watching: test/foo
+ Thu Jan 03 2013 22:48:17 - Running action...
+ Thu Jan 03 2013 22:48:17 - stderr:
+ Thu Jan 03 2013 22:48:17 - stdout: The file test/foo changed
+
By default, Watchman will perform the action immediately upon running. To prevent this, pass -w as an option.
+About Actions
+-------------
+
+The action you specify will be exec'ed in a shell with the same working directory as watchman. The action string is actually a template
+for [eco](https://github.com/sstephenson/eco). Currently, the context provided to the action template is simply:
+```
+{file: '/file/that/changed'}
+```
+
+
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.6",
+ "version": "0.1.7",
"homepage": "http://github.com/dfjones/watchman",
"keywords": ["file", "filesystem", "change", "command line"],
"author": "Doug Jones <doug@dfjones.net>",
@@ -17,6 +17,7 @@
},
"engines": { "node": ">= 0.4.0" },
"dependencies": {
- "cli": ">= 0.2.8"
+ "cli": ">= 0.2.8",
+ "eco": ">= 1.1.0"
}
}
View
@@ -1,10 +1,12 @@
#!/usr/bin/env node
// Generated by CoffeeScript 1.4.0
(function() {
- var cli, exec, fs, getFormattedDate, log, path;
+ var cli, eco, exec, fs, getFormattedDate, log, path;
cli = require('cli');
+ eco = require('eco');
+
fs = require('fs');
path = require('path');
@@ -35,7 +37,7 @@
};
cli.main(function(args, options) {
- var action, actionQueue, directoryWatcher, execAction, execFromQueue, find_files, queueAction, rate, rateMap, target, testHidden, useQueue, watched, watcher;
+ var action, actionQueue, directoryWatcher, execAction, execFromQueue, find_files, onFileChange, queueAction, rate, rateMap, target, testHidden, useQueue, watched, watcher;
if (args.length < 2) {
console.log("Please specify a target and action");
return;
@@ -50,29 +52,40 @@
watched = {};
useQueue = false;
actionQueue = [];
- queueAction = function() {
+ queueAction = function(action, file) {
if (actionQueue.length < options["queue"]) {
- return actionQueue.push(action);
+ return actionQueue.push([action, file]);
}
};
execFromQueue = function() {
var a, _i, _len;
for (_i = 0, _len = actionQueue.length; _i < _len; _i++) {
a = actionQueue[_i];
- execAction(a);
+ execAction(a[0], a[1]);
}
return actionQueue = [];
};
- execAction = function(toExec) {
+ execAction = function(toExec, file) {
if (toExec == null) {
toExec = action;
}
+ toExec = eco.render(toExec, {
+ file: file
+ });
log("Running action...");
return exec(toExec, function(error, stdout, stderr) {
log("stderr: " + stderr);
return log("stdout: " + stdout);
});
};
+ onFileChange = function(file) {
+ log("File changed: " + file);
+ if (useQueue) {
+ return queueAction(action, file);
+ } else {
+ return execAction(action, file);
+ }
+ };
watcher = function(file) {
if (file in watched) {
return;
@@ -86,12 +99,7 @@
if (curr.size === prev.size && curr.mtime.getTime() === prev.mtime.getTime()) {
return;
}
- log("File changed: " + file);
- if (useQueue) {
- return queueAction();
- } else {
- return execAction();
- }
+ return onFileChange(file);
});
};
directoryWatcher = function(dir) {
@@ -126,7 +134,7 @@
setInterval(execFromQueue, rate);
}
find_files = function(target, quiet) {
- return path.exists(target, function(exists) {
+ return fs.exists(target, function(exists) {
if (!quiet && !exists) {
throw "Target file not found: " + target;
}
View
@@ -1,6 +1,7 @@
#!/usr/bin/env coffee
cli = require 'cli'
+eco = require 'eco'
fs = require 'fs'
path = require 'path'
exec = require 'child_process'
@@ -42,33 +43,37 @@ cli.main (args, options) ->
useQueue = false
actionQueue = []
- queueAction = ->
+ queueAction = (action, file) ->
if actionQueue.length < options["queue"]
- actionQueue.push(action)
+ actionQueue.push([action, file])
execFromQueue = ->
for a in actionQueue
- execAction(a)
+ execAction(a[0], a[1])
actionQueue = []
- execAction = (toExec) ->
+ execAction = (toExec, file) ->
toExec ?= action
+ toExec = eco.render(toExec, {file})
log("Running action...")
exec toExec, (error, stdout, stderr) ->
log("stderr: " + stderr)
log("stdout: " + stdout)
+ onFileChange = (file) ->
+ log("File changed: " + file)
+ if useQueue
+ queueAction(action, file)
+ else
+ execAction(action, file)
+
watcher = (file) ->
return if file of watched
watched[file] = true
log("watching: #{file}")
fs.watchFile file, {persistent: true, interval: 500}, (curr, prev) ->
return if curr.size is prev.size and curr.mtime.getTime() is prev.mtime.getTime()
- log("File changed: " + file)
- if useQueue
- queueAction()
- else
- execAction()
+ onFileChange(file)
directoryWatcher = (dir) ->
return if dir of watched
@@ -93,7 +98,7 @@ cli.main (args, options) ->
setInterval(execFromQueue, rate)
find_files = (target, quiet) ->
- path.exists target, (exists) ->
+ fs.exists target, (exists) ->
throw "Target file not found: #{target}" if not quiet and not exists
fs.stat target, (err, stats) ->
if err?
View

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

Oops, something went wrong.

0 comments on commit ccb234b

Please sign in to comment.