Browse files

working with multiple hooks

  • Loading branch information...
1 parent 6598134 commit 85c060a5d0d367f819a7fad1942bc7ee4452f87b @lancejpollard committed Feb 20, 2012
View
5 lib/design.io/client.js
@@ -1,12 +1,13 @@
window.DesignIO = (function() {
- function DesignIO(options) {
+ function DesignIO(namespace, options) {
options || (options = {});
this.callbacks = {};
this.watchers = [];
this.port = options.port || 4181;
- this.url = options.url || ("" + window.location.protocol + "//" + window.location.hostname + ":" + this.port + "/design.io");
+ this.namespace = namespace;
+ this.url = options.url || ("" + window.location.protocol + "//" + window.location.hostname + ":" + this.port + "/" + namespace);
this.socket = io.connect(this.url);
this.connect();
}
View
11 lib/design.io/command.js
@@ -8,7 +8,7 @@
command = function(argv) {
var program, slug;
program = require("commander");
- program.option("-d, --directory [value]", "directory to watch files from").option("-w, --watchfile [value]", "location of Watchfile").option("-p, --port <n>", "port for the socket connection").option("-u, --url [value]", "URL for the socket connection").option("-i, --interval <n>", "interval (in milliseconds) files should be scanned (only useful if you can't use FSEvents). Not implemented").option("-n, --namespace [value]", "Namespace for the project").parse(process.argv);
+ program.option("-d, --directory [value]", "directory to watch files from").option("-w, --watchfile [value]", "location of Watchfile").option("-p, --port <n>", "port for the socket connection").option("--debug", "Debug?").option("-u, --url [value]", "URL for the socket connection").option("-i, --interval <n>", "interval (in milliseconds) files should be scanned (only useful if you can't use FSEvents). Not implemented").option("-n, --namespace [value]", "Namespace for the project").parse(process.argv);
program.directory || (program.directory = process.cwd());
program.watchfile || (program.watchfile = "Watchfile");
program.port = program.port ? parseInt(program.port) : process.env.PORT || 4181;
@@ -31,7 +31,7 @@
switch (program.command) {
case "start":
return forever.start(["node", "" + __dirname + "/command/start.js"], {
- silent: true,
+ silent: false,
max: 1
});
case "stop":
@@ -47,10 +47,11 @@
})();
child.on("start", function() {});
child.on("exit", function() {});
- child.on("stdout", function(data) {
- return console.log(data.toString());
+ child.on("stop", function() {});
+ child.on("stdout", function(data) {});
+ child.on("stderr", function(error) {
+ return console.log(error.toString());
});
- child.on("stderr", function() {});
child.on("error", function() {});
forever.startServer(child);
return program;
View
36 lib/design.io/command/start.js
@@ -1,5 +1,5 @@
(function() {
- var Hook, Project, app, command, connect, designer, express, hook, io;
+ var Hook, Project, app, command, connect, express, hook, io;
command = require(__dirname)(process.argv);
@@ -15,12 +15,14 @@
io = require('socket.io').listen(app);
- designer = io.of("/design.io");
-
Project = require("../project");
- designer.on("connection", function(socket) {
+ io.set('log level', 1);
+
+ io.on("connection", function(socket) {
socket.on("userAgent", function(data) {
+ socket.room = data.namespace;
+ socket.join(data.namespace);
return socket.set("userAgent", data, function() {
socket.emit("ready");
Project.find(data.namespace).connect();
@@ -32,7 +34,8 @@
return true;
});
return socket.on("disconnect", function() {
- return socket.emit("user disconnected");
+ socket.emit("user disconnected");
+ return socket.leave(socket.room);
});
});
@@ -44,31 +47,22 @@
hook = new Hook({
name: "design.io-server",
- debug: true
+ debug: false,
+ silent: true,
+ m: false
});
hook.on("hook::ready", function(data) {
- return console.log("hook started");
+ return _console.info("Design.io started on port " + command.port);
});
- hook.on("design.io-watcher::initialized", function(data) {
- var action, namespace, paths;
- action = data.action, paths = data.paths, namespace = data.namespace;
- return designer.broadcast(action, JSON.stringify(data));
- });
-
- hook.on("design.io-watcher::changed", function(data) {
- var action, current, namespace, path, previous, timestamp;
- console.log("design.io-watcher!!!");
- console.log(data);
- action = data.action, timestamp = data.timestamp, previous = data.previous, current = data.current, path = data.path, namespace = data.namespace;
- return designer.broadcast(action, JSON.stringify(data));
+ hook.on("*::exec", function(data, callback, event) {
+ if (!event.name.match("design.io-watcher")) return;
+ return io.sockets["in"](data.namespace).emit(data.action, JSON.stringify(data));
});
hook.start();
app.get("design.io", function(request, response) {});
- _console.info("Design.io started on port " + command.port);
-
}).call(this);
View
2 lib/design.io/command/stop.js
@@ -7,7 +7,7 @@
hook = new Hook({
name: "design.io-stop",
- debug: true
+ debug: false
});
hook.on("hook::ready", function(data) {
View
4 lib/design.io/command/watch.js
@@ -3,7 +3,9 @@
command = require(__dirname)(process.argv);
- project = new (require("../project"))(command);
+ project = require("../project");
+
+ project = new project(command);
project.watch();
View
2 lib/design.io/extensions/watchfile.js
@@ -1,6 +1,6 @@
module.exports = function() {
- return Watcher.create(Watcher.watchfile, {
+ return Watcher.create(require("../project").find().watchfile, {
update: function() {
return this.updateAll();
},
View
4 lib/design.io/listener.js
@@ -25,14 +25,14 @@
path = _path.join(root, source.replace(root, ""));
if (!stat.isDirectory()) {
files[path] = stat;
- initialized.push(File.absolutePath(path));
+ initialized.push(File.relativePath(path));
} else {
directories[path] = File.entries(path);
}
}
try {
callback.call(self, initialized, {
- action: "initialized"
+ action: "initialize"
});
} catch (error) {
console.log(error.stack);
View
254 lib/design.io/project.js
@@ -0,0 +1,254 @@
+(function() {
+ var File, Hook, Pathfinder, Project, Shift, Watcher, async, fs;
+ var __slice = Array.prototype.slice;
+
+ Hook = require("hook.io").Hook;
+
+ Pathfinder = require('pathfinder');
+
+ File = Pathfinder.File;
+
+ Watcher = require("./watcher");
+
+ async = require('async');
+
+ fs = require('fs');
+
+ Shift = require('shift');
+
+ Project = (function() {
+
+ Project.store = function() {
+ return this._store || (this._store = {});
+ };
+
+ Project.all = Project.store;
+
+ Project.timeout = 10 * 1000;
+
+ Project.find = function(namespace) {
+ var cwd, project, _ref;
+ if (namespace) {
+ return this.all()[namespace];
+ } else {
+ cwd = process.cwd();
+ _ref = this.all();
+ for (namespace in _ref) {
+ project = _ref[namespace];
+ if (project.root === cwd) return project;
+ }
+ return null;
+ }
+ };
+
+ Project.prototype.replacer = function(key, value) {
+ if (value instanceof RegExp) {
+ return "(function() { return new RegExp('" + value + "') })";
+ } else if (typeof value === "function") {
+ return "(" + value + ")";
+ } else {
+ return value;
+ }
+ };
+
+ Project.prototype.reviver = function(key, value) {
+ if (typeof value === "string" && !!value.match(/^(?:\(function\s*\([^\)]*\)\s*\{|\(\/)/) && !!value.match(/(?:\}\s*\)|\/\w*\))$/)) {
+ return eval(value);
+ } else {
+ return value;
+ }
+ };
+
+ function Project(options) {
+ var store;
+ if (options == null) options = {};
+ this.root = File.absolutePath(options.root);
+ this.namespace = options.namespace;
+ this.watchfile = File.absolutePath(options.watchfile);
+ this.pathfinder = new Pathfinder(this.root);
+ this.ignoredPaths = [];
+ this.watchers = [];
+ Watcher.pathfinder = this.pathfinder;
+ if (!this.watchfile) throw new Error("You must specify the watchfile");
+ if (!this.root) throw new Error("You must specify the directory to watch");
+ store = this.constructor.store();
+ if (store.hasOwnProperty(this.namespace)) {
+ throw new Error("Only one project per namespace");
+ }
+ store[this.namespace] = this;
+ this.hook = new Hook({
+ name: "design.io-watcher",
+ debug: false,
+ silent: true
+ });
+ }
+
+ Project.prototype.watch = function() {
+ var hook;
+ var _this = this;
+ hook = this.hook;
+ hook.on("hook::ready", function(data) {
+ hook.emit("ready", data);
+ _this.read(function() {
+ return new (require('./listener/mac'))({
+ root: _this.root,
+ ignore: _this.ignoredPaths
+ }, function(path, options) {
+ var _i, _len, _ref, _results;
+ options.namespace = _this.namespace;
+ options.paths = path instanceof Array ? path : [path];
+ _ref = options.paths;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ path = _ref[_i];
+ _results.push(_this.changed(path, options));
+ }
+ return _results;
+ });
+ });
+ return _this;
+ });
+ hook.on("design.io-server::stop", function() {
+ return hook.stop();
+ });
+ hook.start();
+ return this;
+ };
+
+ Project.prototype.createWatcher = function() {
+ return this.watchers.push((function(func, args, ctor) {
+ ctor.prototype = func.prototype;
+ var child = new ctor, result = func.apply(child, args);
+ return typeof result === "object" ? result : child;
+ })(Watcher, [this].concat(__slice.call(arguments)), function() {}));
+ };
+
+ Project.prototype.read = function(callback) {
+ var _this = this;
+ Watcher.ignoredPaths = [];
+ return fs.readFile(this.watchfile, "utf-8", function(error, result) {
+ var Module, engine, mainModule, paths;
+ engine = new Shift.CoffeeScript;
+ mainModule = require.main;
+ paths = mainModule.paths;
+ mainModule.moduleCache && (mainModule.moduleCache = {});
+ mainModule.filename = '.';
+ if (process.binding('natives').module) {
+ Module = require('module').Module;
+ mainModule.paths = Module._nodeModulePaths(File.dirname(_this.watchfile));
+ }
+ result = "global.Watcher = {}\n\n__project = require(\"design.io/lib/design.io/project\").find(\"" + _this.namespace + "\")\n\nignorePaths = ->\n __project.ignoredPaths ||= []\n __project.ignoredPaths = __project.ignoredPaths.concat Array.prototype.slice.call(arguments, 0, arguments.length)\n\nwatch = ->\n __project.createWatcher(arguments...)\n\nglobal.Watcher.pathfinder = __project.pathfinder\nglobal.Watcher.create = ->\n __project.createWatcher(arguments...)\n \n" + result;
+ return engine.render(result, function(error, result) {
+ mainModule._compile(result, mainModule.filename);
+ if (callback) return callback.call(_this);
+ });
+ });
+ };
+
+ Project.prototype.changed = function(path, options) {
+ if (options == null) options = {};
+ return this.queue.push({
+ binding: this,
+ path: path,
+ options: options
+ });
+ };
+
+ Project.prototype.queue = async.queue(function(change, callback) {
+ return change.binding.change(change.path, change.options, callback);
+ }, 1);
+
+ Project.prototype.change = function(path, options, callback) {
+ var iterator, watchers;
+ watchers = this.watchers;
+ options.duration || (options.duration = this.constructor.timeout);
+ iterator = function(watcher, next) {
+ return watcher.invoke(path, options, next);
+ };
+ return async.forEachSeries(watchers, iterator, function(error) {
+ return process.nextTick(callback);
+ });
+ };
+
+ Project.prototype.toJSON = function() {
+ var data, watcher, watchers, _i, _len;
+ watchers = this.watchers;
+ data = [];
+ for (_i = 0, _len = watchers.length; _i < _len; _i++) {
+ watcher = watchers[_i];
+ data.push(watcher.toJSON());
+ }
+ return data;
+ };
+
+ Project.prototype.update = function() {
+ return this.read(this.connect);
+ };
+
+ Project.prototype.connect = function() {
+ return this.broadcast("watch", {
+ body: this.toJSON()
+ });
+ };
+
+ Project.prototype.broadcast = function(action, data, callback) {
+ if (data.action === "initialize") {
+ if (callback) callback.call(this, null, null);
+ return;
+ }
+ data = JSON.stringify(data, this.replacer);
+ return this.hook.emit(action, data);
+ };
+
+ Project.prototype.log = function(data) {
+ var action, path, timestamp, watcher, watchers, _i, _len, _results;
+ watchers = this.watchers;
+ path = data.path;
+ action = data.action;
+ timestamp = data.timestamp;
+ _results = [];
+ for (_i = 0, _len = watchers.length; _i < _len; _i++) {
+ watcher = watchers[_i];
+ if (watcher.hasOwnProperty("server") && watcher.server.hasOwnProperty(action) && watcher.id === data.id) {
+ server.watcher = watcher;
+ server.path = path;
+ server.action = action;
+ server.timestamp = timestamp;
+ try {
+ _results.push(!!server[action](data));
+ } catch (error) {
+ _results.push(console.log(error.stack));
+ }
+ } else {
+ _results.push(void 0);
+ }
+ }
+ return _results;
+ };
+
+ Project.Watchfile = (function() {
+
+ function Watchfile() {
+ Watcher._store = void 0;
+ }
+
+ Watchfile.prototype.ignorePaths = function() {
+ Project.ignoredPaths || (Project.ignoredPaths = []);
+ return Project.ignoredPaths = Project.ignoredPaths.concat(Array.prototype.slice.call(arguments, 0, arguments.length));
+ };
+
+ Watchfile.prototype.watch = function() {
+ return Project.createWatcher.apply(Project, arguments);
+ };
+
+ return Watchfile;
+
+ })();
+
+ return Project;
+
+ })();
+
+ module.exports = Project;
+
+}).call(this);
View
14 lib/design.io/watcher.js
@@ -1,5 +1,5 @@
(function() {
- var File, Pathfinder, Project, Shift, Watcher, async, fs, path, request, uuid;
+ var File, Pathfinder, Project, Watcher, fs, path, uuid;
var __slice = Array.prototype.slice;
fs = require('fs');
@@ -8,15 +8,9 @@
uuid = require('node-uuid');
- async = require('async');
-
- Shift = require('shift');
-
- request = require('request');
-
Pathfinder = require('pathfinder');
- Project = require('project');
+ Project = require('./project');
File = Pathfinder.File;
@@ -27,13 +21,15 @@
function Watcher() {
var arg, args, key, methods, project, value, _i, _len;
project = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
+ this.project = project;
methods = args.pop();
if (typeof methods === "function") methods = methods.call(this);
if (args[0] instanceof Array) args = args[0];
this.ignore = null;
this.patterns = [];
for (_i = 0, _len = args.length; _i < _len; _i++) {
arg = args[_i];
+ if (!arg) continue;
this.patterns.push(typeof arg === "string" ? new RegExp(arg) : arg);
}
for (key in methods) {
@@ -138,6 +134,8 @@
data.action || (data.action = this.action);
data.path || (data.path = this.path);
data.id = this.id;
+ data.timestamp = this.timestamp;
+ data.namespace = this.project.namespace;
action = args.shift() || "exec";
return this.project.broadcast(action, data, callback);
};
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "design.io",
- "version": "0.2.7",
+ "version": "0.2.9",
"description": "Design and Test Your App in Real-Time from TextMate",
"homepage": "http://github.com/viatropos/design.io",
"main": "lib/design.io.js",
View
5 src/design.io/client.coffee
@@ -1,10 +1,11 @@
class window.DesignIO
- constructor: (options) ->
+ constructor: (namespace, options) ->
options ||= {}
@callbacks = {}
@watchers = []
@port = options.port || 4181
- @url = options.url || "#{window.location.protocol}//#{window.location.hostname}:#{@port}/design.io"
+ @namespace = namespace
+ @url = options.url || "#{window.location.protocol}//#{window.location.hostname}:#{@port}/#{namespace}"
@socket = io.connect(@url)
@connect()
View
9 src/design.io/command.coffee
@@ -9,6 +9,7 @@ command = (argv) ->
.option("-d, --directory [value]", "directory to watch files from")
.option("-w, --watchfile [value]", "location of Watchfile")
.option("-p, --port <n>", "port for the socket connection")
+ .option("--debug", "Debug?")
.option("-u, --url [value]", "URL for the socket connection")
.option("-i, --interval <n>", "interval (in milliseconds) files should be scanned (only useful if you can't use FSEvents). Not implemented")
.option("-n, --namespace [value]", "Namespace for the project")
@@ -33,7 +34,7 @@ command.run = (argv) ->
child = switch program.command
when "start"
- forever.start ["node", "#{__dirname}/command/start.js"], silent: true, max: 1
+ forever.start ["node", "#{__dirname}/command/start.js"], silent: false, max: 1
when "stop"
forever.start ["node", "#{__dirname}/command/stop.js"], silent: true, max: 1
else
@@ -42,11 +43,13 @@ command.run = (argv) ->
child.on "start", ->
child.on "exit", ->
+
+ child.on "stop", ->
child.on "stdout", (data) ->
- console.log data.toString()
- child.on "stderr", ->
+ child.on "stderr", (error) ->
+ console.log error.toString()
child.on "error", ->
View
35 src/design.io/command/start.coffee
@@ -7,12 +7,13 @@ express = require("express")
connect = require('connect')
app = express.createServer()
io = require('socket.io').listen(app)
-designer = io.of("/design.io")
Project = require("../project")
-# io.set 'log level', 1
+io.set 'log level', 1
-designer.on "connection", (socket) ->
+io.on "connection", (socket) ->
socket.on "userAgent", (data) ->
+ socket.room = data.namespace
+ socket.join(data.namespace)
socket.set "userAgent", data, ->
socket.emit "ready"
Project.find(data.namespace).connect()
@@ -21,35 +22,27 @@ designer.on "connection", (socket) ->
socket.on "log", (data) ->
Project.find(data.namespace).log(data)
true
-
+
socket.on "disconnect", ->
socket.emit "user disconnected"
-
+ socket.leave(socket.room)
+
app.listen(command.port)
app.use express.static(__dirname + '/../..')
app.use connect.bodyParser()
-hook = new Hook(name: "design.io-server", debug: true)
+hook = new Hook(name: "design.io-server", debug: false, silent: true, m: false)
hook.on "hook::ready", (data) ->
- console.log "hook started"
+ _console.info "Design.io started on port #{command.port}"
-hook.on "design.io-watcher::initialized", (data) ->
- # updated, new Date, /Users/..., cwd, "my-project"
- {action, paths, namespace} = data
- # emit to browser
- designer.broadcast action, JSON.stringify(data)
-
-hook.on "design.io-watcher::changed", (data) ->
- console.log "design.io-watcher!!!"
- console.log data
+hook.on "*::exec", (data, callback, event) ->
+ return unless event.name.match("design.io-watcher")
# updated, new Date, /Users/..., cwd, "my-project"
- {action, timestamp, previous, current, path, namespace} = data
+ # {action, timestamp, previous, current, path, namespace} = data
# emit to browser
- designer.broadcast action, JSON.stringify(data)
+ io.sockets.in(data.namespace).emit data.action, JSON.stringify(data)
hook.start()
-app.get "design.io", (request, response) ->
-
-_console.info "Design.io started on port #{command.port}"
+app.get "design.io", (request, response) ->
View
2 src/design.io/command/stop.coffee
@@ -2,7 +2,7 @@
command = require(__dirname)(process.argv)
Hook = require("hook.io").Hook
-hook = new Hook(name: "design.io-stop", debug: true)
+hook = new Hook(name: "design.io-stop", debug: false)
hook.on "hook::ready", (data) ->
hook.emit "ready", data
View
6 src/design.io/command/watch.coffee
@@ -1,4 +1,6 @@
#!/usr/bin/env node
+
command = require(__dirname)(process.argv)
-project = new (require("../project"))(command)
-project.watch()
+project = require("../project")
+project = new project(command)
+project.watch()
View
2 src/design.io/extensions/watchfile.coffee
@@ -1,5 +1,5 @@
module.exports = ->
- Watcher.create Watcher.watchfile,
+ Watcher.create require("../project").find().watchfile,
update: ->
@updateAll()
View
4 src/design.io/listener.coffee
@@ -18,7 +18,7 @@ class Listener
path = _path.join(root, source.replace(root, ""))
unless stat.isDirectory()
files[path] = stat
- initialized.push File.absolutePath(path)
+ initialized.push File.relativePath(path)
#try
# callback.call(self, File.relativePath(path), action: "initialize")
#catch error
@@ -27,7 +27,7 @@ class Listener
directories[path] = File.entries(path)
try
- callback.call self, initialized, action: "initialized"
+ callback.call self, initialized, action: "initialize"
catch error
console.log error.stack
View
51 src/design.io/project.coffee
@@ -1,6 +1,10 @@
-Hook = require("hook.io").Hook
-File = require("pathfinder").File
-Watcher = require("./watcher")
+Hook = require("hook.io").Hook
+Pathfinder = require 'pathfinder'
+File = Pathfinder.File
+Watcher = require("./watcher")
+async = require 'async'
+fs = require 'fs'
+Shift = require 'shift'
# This is the base class.
#
@@ -18,7 +22,7 @@ class Project
@all()[namespace]
else
cwd = process.cwd()
- for project in @all()
+ for namespace, project of @all()
return project if project.root == cwd
null
@@ -44,17 +48,20 @@ class Project
@root = File.absolutePath(options.root)
@namespace = options.namespace
@watchfile = File.absolutePath(options.watchfile)
+ @pathfinder = new Pathfinder(@root)
@ignoredPaths = []
@watchers = []
+ Watcher.pathfinder = @pathfinder
+
throw new Error("You must specify the watchfile") unless @watchfile
throw new Error("You must specify the directory to watch") unless @root
store = @constructor.store()
throw new Error("Only one project per namespace") if store.hasOwnProperty(@namespace)
store[@namespace] = @
- @hook = new Hook(name: "design.io-watcher", debug: true)
+ @hook = new Hook(name: "design.io-watcher", debug: false, silent: true)
watch: ->
hook = @hook
@@ -64,12 +71,16 @@ class Project
@read =>
new (require('./listener/mac')) root: @root, ignore: @ignoredPaths, (path, options) =>
- options.namespace = command.namespace
+ options.namespace = @namespace
options.paths = if path instanceof Array then path else [path]
-
- @changed(path, options)
+
+ for path in options.paths
+ @changed(path, options)
@
+ hook.on "design.io-server::stop", =>
+ hook.stop()
+
hook.start()
@
@@ -91,6 +102,8 @@ class Project
mainModule.paths = Module._nodeModulePaths(File.dirname(@watchfile))
result = """
+ global.Watcher = {}
+
__project = require("design.io/lib/design.io/project").find("#{@namespace}")
ignorePaths = ->
@@ -99,7 +112,11 @@ class Project
watch = ->
__project.createWatcher(arguments...)
-
+
+ global.Watcher.pathfinder = __project.pathfinder
+ global.Watcher.create = ->
+ __project.createWatcher(arguments...)
+
#{result}
"""
@@ -109,10 +126,10 @@ class Project
callback.call(@) if callback
changed: (path, options = {}) ->
- @queue.push path: path, options: options
+ @queue.push binding: @, path: path, options: options
queue: async.queue((change, callback) ->
- @change(change.path, change.options, callback)
+ change.binding.change(change.path, change.options, callback)
, 1)
change: (path, options, callback) ->
@@ -121,7 +138,7 @@ class Project
iterator = (watcher, next) ->
watcher.invoke(path, options, next)
-
+
async.forEachSeries watchers, iterator, (error) ->
process.nextTick(callback)
@@ -141,9 +158,9 @@ class Project
@broadcast "watch", body: @toJSON()
broadcast: (action, data, callback) ->
- #if data.action == "initialize"
- # callback.call(@, null, null) if callback
- # return
+ if data.action == "initialize"
+ callback.call(@, null, null) if callback
+ return
data = JSON.stringify(data, @replacer)
# url = "#{@url}/design.io/#{action}"
@@ -179,4 +196,6 @@ class Project
Project.ignoredPaths = Project.ignoredPaths.concat Array.prototype.slice.call(arguments, 0, arguments.length)
watch: ->
- Project.createWatcher(arguments...)
+ Project.createWatcher(arguments...)
+
+module.exports = Project
View
18 src/design.io/watcher.coffee
@@ -1,11 +1,8 @@
fs = require 'fs'
path = require 'path'
uuid = require 'node-uuid'
-async = require 'async'
-Shift = require 'shift'
-request = require 'request'
Pathfinder = require 'pathfinder'
-Project = require 'project'
+Project = require './project'
File = Pathfinder.File
require 'underscore.logger'
@@ -16,13 +13,17 @@ require 'underscore.logger'
# @author Lance Pollard
class Watcher
constructor: (project, args...) ->
+ @project = project
methods = args.pop()
methods = methods.call(@) if typeof methods == "function"
args = args[0] if args[0] instanceof Array
@ignore = null
@patterns = []
+
for arg in args
- @patterns.push if typeof arg == "string" then new RegExp(arg) else arg
+ continue unless arg
+ @patterns.push(if typeof arg == "string" then new RegExp(arg) else arg)
+
@[key] = value for key, value of methods
@id ||= uuid()
@@ -58,6 +59,7 @@ class Watcher
match: (path) ->
return false if @ignore && !!@ignore.exec(path)
patterns = @patterns
+
for pattern in patterns
return true if !!pattern.exec(path)
false
@@ -113,8 +115,10 @@ class Watcher
callback = null
data.action ||= @action
data.path ||= @path
- data.id = @id
- action = args.shift() || "exec"
+ data.id = @id
+ data.timestamp = @timestamp
+ data.namespace = @project.namespace
+ action = args.shift() || "exec"
@project.broadcast action, data, callback
toJSON: ->

0 comments on commit 85c060a

Please sign in to comment.