Permalink
Browse files

Renamed so coffee script is not a hard dependency of watchman.

Improved help and error messages.
  • Loading branch information...
1 parent 379cf74 commit 80cfe3fed082c562d22f9d766791bd90994ce37b @dfjones committed Apr 13, 2011
Showing with 226 additions and 215 deletions.
  1. +131 −91 watchman
  2. +95 −0 watchman.coffee
  3. +0 −124 watchman.js
View
@@ -1,91 +1,131 @@
-#!/usr/bin/env coffee
-
-cli = require 'cli'
-fs = require 'fs'
-path = require 'path'
-winston = require 'winston'
-exec = require 'child_process'
-exec = exec.exec
-
-cli.setUsage('watchman [options] target action')
-
-cli.parse({
- "ignore-hidden": ['i', "Do not watch hidden files"],
- "rate-limit": ['r', "Rate limit actions", "string", null],
- "queue-size": ['q', "Action queue size", "int", 1]
-})
-
-cli.main (args, options) ->
- if args.length < 2
- console.log "Please specify a target and action"
- return
-
- rateMap = {
- s: 1000
- m: 1000*60
- h: 1000*60*60
- }
-
- target = args[0]
- action = args[1]
-
- watched = {}
-
- useQueue = false
- actionQueue = []
-
- queueAction = ->
- if actionQueue.length < options["queue-size"]
- actionQueue.push(action)
-
- execFromQueue = ->
- for a in actionQueue
- execAction(a)
- actionQueue = []
-
- execAction = (toExec) ->
- toExec ?= action
- winston.info("Running action...")
- exec toExec, (error, stdout, stderr) ->
- winston.info("stderr: " + stderr)
- winston.info("stdout: " + stdout)
-
- watcher = (file) ->
- return if file of watched
- watched[file] = true
- winston.info("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()
- winston.info("File changed: " + file)
- if useQueue
- queueAction()
- else
- execAction()
-
- directoryWatcher = (dir) ->
- return if dir of watched
- watched[dir] = true
- winston.info("watching directory: #{dir}")
- fs.watchFile dir, {persistent: true, interval: 500}, (curr, prev) ->
- find_files(dir)
-
- testHidden = (file) -> options["ignore-hidden"] and file[0] is '.'
-
- if options["rate-limit"]?
- useQueue = true
- rate = options["rate-limit"]
- rate = parseInt(rate[...-1]) * rateMap[rate[-1..]]
- setInterval(execFromQueue, rate)
-
- find_files = (target) ->
- path.exists target, (exists) ->
- throw new "Target file not found: #{target}" if not exists
- fs.stat target, (err, stats) ->
- if stats.isDirectory()
- directoryWatcher(target)
- fs.readdir target, (err, files) ->
- find_files(target + "/" + file) for file in files when not testHidden(file)
- else
- watcher(target) if not testHidden(target)
-
- find_files(target)
+#!/usr/bin/env node
+
+(function() {
+ var cli, exec, fs, path, winston;
+ cli = require('cli');
+ fs = require('fs');
+ path = require('path');
+ winston = require('winston');
+ 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]
+ });
+ 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) {
+ console.log("Please specify a target and action");
+ return;
+ }
+ rateMap = {
+ s: 1000,
+ m: 1000 * 60,
+ h: 1000 * 60 * 60
+ };
+ target = args[0];
+ action = args[1];
+ watched = {};
+ useQueue = false;
+ actionQueue = [];
+ queueAction = function() {
+ if (actionQueue.length < options["queue"]) {
+ return actionQueue.push(action);
+ }
+ };
+ execFromQueue = function() {
+ var a, _i, _len;
+ for (_i = 0, _len = actionQueue.length; _i < _len; _i++) {
+ a = actionQueue[_i];
+ execAction(a);
+ }
+ return actionQueue = [];
+ };
+ execAction = function(toExec) {
+ toExec != null ? toExec : toExec = action;
+ winston.info("Running action...");
+ return exec(toExec, function(error, stdout, stderr) {
+ winston.info("stderr: " + stderr);
+ return winston.info("stdout: " + stdout);
+ });
+ };
+ watcher = function(file) {
+ if (file in watched) {
+ return;
+ }
+ watched[file] = true;
+ winston.info("watching: " + file);
+ return fs.watchFile(file, {
+ persistent: true,
+ interval: 500
+ }, function(curr, prev) {
+ if (curr.size === prev.size && curr.mtime.getTime() === prev.mtime.getTime()) {
+ return;
+ }
+ winston.info("File changed: " + file);
+ if (useQueue) {
+ return queueAction();
+ } else {
+ return execAction();
+ }
+ });
+ };
+ directoryWatcher = function(dir) {
+ if (dir in watched) {
+ return;
+ }
+ watched[dir] = true;
+ winston.info("watching directory: " + dir);
+ return fs.watchFile(dir, {
+ persistent: true,
+ interval: 500
+ }, function(curr, prev) {
+ return find_files(dir);
+ });
+ };
+ testHidden = function(file) {
+ return options["ignore-hidden"] && file[0] === '.';
+ };
+ if (options["rate"] != null) {
+ useQueue = true;
+ rate = options["rate"];
+ try {
+ rate = parseInt(rate.slice(0, -1)) * rateMap[rate.slice(-1)];
+ } catch (error) {
+ console.log("Error parsing rate. Rates must be expressed as Ns, Nm, Nh where N is an integer and s, m, h stand for seconds, mintues, hours respectively");
+ return;
+ }
+ setInterval(execFromQueue, rate);
+ }
+ find_files = function(target) {
+ return path.exists(target, function(exists) {
+ if (!exists) {
+ throw new ("Target file not found: " + target);
+ }
+ return fs.stat(target, function(err, stats) {
+ if (stats.isDirectory()) {
+ directoryWatcher(target);
+ return fs.readdir(target, function(err, files) {
+ var file, _i, _len, _results;
+ _results = [];
+ for (_i = 0, _len = files.length; _i < _len; _i++) {
+ file = files[_i];
+ if (!testHidden(file)) {
+ _results.push(find_files(target + "/" + file));
+ }
+ }
+ return _results;
+ });
+ } else {
+ if (!testHidden(target)) {
+ return watcher(target);
+ }
+ }
+ });
+ });
+ };
+ return find_files(target);
+ });
+}).call(this);
View
@@ -0,0 +1,95 @@
+#!/usr/bin/env coffee
+
+cli = require 'cli'
+fs = require 'fs'
+path = require 'path'
+winston = require 'winston'
+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]
+})
+
+cli.main (args, options) ->
+ if args.length < 2
+ console.log "Please specify a target and action"
+ return
+
+ rateMap = {
+ s: 1000
+ m: 1000*60
+ h: 1000*60*60
+ }
+
+ target = args[0]
+ action = args[1]
+
+ watched = {}
+
+ useQueue = false
+ actionQueue = []
+
+ queueAction = ->
+ if actionQueue.length < options["queue"]
+ actionQueue.push(action)
+
+ execFromQueue = ->
+ for a in actionQueue
+ execAction(a)
+ actionQueue = []
+
+ execAction = (toExec) ->
+ toExec ?= action
+ winston.info("Running action...")
+ exec toExec, (error, stdout, stderr) ->
+ winston.info("stderr: " + stderr)
+ winston.info("stdout: " + stdout)
+
+ watcher = (file) ->
+ return if file of watched
+ watched[file] = true
+ winston.info("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()
+ winston.info("File changed: " + file)
+ if useQueue
+ queueAction()
+ else
+ execAction()
+
+ directoryWatcher = (dir) ->
+ return if dir of watched
+ watched[dir] = true
+ winston.info("watching directory: #{dir}")
+ fs.watchFile dir, {persistent: true, interval: 500}, (curr, prev) ->
+ find_files(dir)
+
+ testHidden = (file) -> options["ignore-hidden"] and file[0] is '.'
+
+ if options["rate"]?
+ useQueue = true
+ rate = options["rate"]
+ try
+ rate = parseInt(rate[...-1]) * rateMap[rate[-1..]]
+ catch error
+ console.log("Error parsing rate. Rates must be expressed as Ns, Nm, Nh where N is an integer and s, m, h stand for seconds, mintues, hours respectively")
+ return
+ setInterval(execFromQueue, rate)
+
+ find_files = (target) ->
+ path.exists target, (exists) ->
+ throw new "Target file not found: #{target}" if not exists
+ fs.stat target, (err, stats) ->
+ if stats.isDirectory()
+ directoryWatcher(target)
+ fs.readdir target, (err, files) ->
+ find_files(target + "/" + file) for file in files when not testHidden(file)
+ else
+ watcher(target) if not testHidden(target)
+
+ find_files(target)
Oops, something went wrong.

0 comments on commit 80cfe3f

Please sign in to comment.