Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor to use the "forever" module, much better

  • Loading branch information...
commit 280b34f7771484e9fac6e0536761db51eb9ed6fd 1 parent a9de28c
@lancejpollard authored
Showing with 305 additions and 230 deletions.
  1. +6 −6 Cakefile
  2. +12 −0 README.md
  3. +2 −1  bin/design.io
  4. BIN  favicon.ico
  5. +93 −92 lib/design.io/client.js
  6. +1 −0  lib/design.io/command.js
  7. +0 −3  lib/design.io/connection.js
  8. +16 −3 lib/design.io/listener.js
  9. +24 −20 lib/design.io/listener/mac.js
  10. +5 −2 lib/design.io/listener/mac.rb
  11. +27 −14 lib/design.io/process.js
  12. +2 −2 lib/design.io/server.js
  13. +28 −13 lib/design.io/watcher.js
  14. +4 −2 package.json
  15. +0 −1  src/design.io/client.coffee
  16. +1 −0  src/design.io/command.coffee
  17. +0 −1  src/design.io/connection.coffee
  18. +12 −4 src/design.io/listener.coffee
  19. +15 −15 src/design.io/listener/mac.coffee
  20. +0 −11 src/design.io/listener/mac.rb
  21. +23 −14 src/design.io/process.coffee
  22. +4 −3 src/design.io/server.coffee
  23. +30 −23 src/design.io/watcher.coffee
  24. 0  {spec → test}/app/application.coffee
  25. 0  {spec → test}/app/application.js
  26. 0  {spec → test}/app/images/delete.png
  27. 0  {spec → test}/app/images/ok.png
  28. 0  {spec → test}/app/images/stickynote-large.png
  29. 0  {spec → test}/app/images/stickynote-medium.png
  30. 0  {spec → test}/app/javascripts/application.coffee
  31. 0  {spec → test}/app/javascripts/application.js
  32. 0  {spec → test}/app/javascripts/example.coffee
  33. 0  {spec → test}/app/javascripts/nested-example.coffee
  34. 0  {spec → test}/app/javascripts/socket.io.js
  35. 0  {spec → test}/app/server.js
  36. 0  {spec → test}/app/stylesheets/another.styl
  37. 0  {spec → test}/app/stylesheets/application.styl
  38. 0  {spec → test}/app/stylesheets/css.css
  39. 0  {spec → test}/app/stylesheets/style.css
  40. 0  {spec → test}/app/views/index.jade
  41. 0  {spec → test}/app/views/index.mustache
  42. 0  {spec → test}/app/views/layout.jade
  43. 0  {spec → test}/javascriptExtension.coffee
  44. 0  {spec → test}/stylesheetExtension.coffee
  45. 0  {spec → test}/tmp/test.css
  46. 0  {spec → test}/tmp/test.js
View
12 Cakefile
@@ -5,23 +5,23 @@ fs = require 'fs'
task 'coffee', ->
coffee = spawn './node_modules/coffee-script/bin/coffee', ['-o', 'lib', '-w', 'src']
coffee.stdout.on 'data', (data) -> console.log data.toString().trim()
- #coffee2 = spawn './node_modules/coffee-script/bin/coffee', ['-w', 'spec', '-o', 'spec']
+ #coffee2 = spawn './node_modules/coffee-script/bin/coffee', ['-w', 'test', '-o', 'test']
#coffee2.stdout.on 'data', (data) -> console.log data.toString().trim()
#coffee2.stderr.on 'data', (data) -> console.log data.toString().trim()
-task 'spec', 'Run jasmine specs', ->
- spec = spawn './node_modules/jasmine-node/bin/jasmine-node', ['--coffee', './spec']
- spec.stdout.on 'data', (data) ->
+task 'test', 'Run jasmine tests', ->
+ test = spawn './node_modules/jasmine-node/bin/jasmine-node', ['--coffee', './test']
+ test.stdout.on 'data', (data) ->
data = data.toString().replace(/^\s*|\s*$/g, '')
if data.match(/\u001b\[3\dm[\.F]\u001b\[0m/)
sys.print data
else
data = "\n#{data}" if data.match(/Finished/)
console.log data
- spec.stderr.on 'data', (data) -> console.log data.toString().trim()
+ test.stderr.on 'data', (data) -> console.log data.toString().trim()
task 'watch-old', 'Compile assets', ->
- watcher = spawn "node", ["./lib/design.io/watcher", "--directory", "./spec", "--watchfile", "Watchfile"]
+ watcher = spawn "node", ["./lib/design.io/watcher", "--directory", "./test", "--watchfile", "Watchfile"]
watcher.stdout.on 'data', (data) -> console.log data.toString().trim()
watcher.stderr.on 'data', (data) -> console.log data.toString().trim()
View
12 README.md
@@ -12,6 +12,14 @@ Here is the [example app](https://github.com/viatropos/design.io-example) for th
```
npm install design.io -g
+design.io start
+design.io stop
+```
+
+## Run
+
+```
+design.io
```
## Extensions
@@ -113,6 +121,10 @@ app.post '/design.io/:action', (request, response) ->
- http://stackoverflow.com/questions/3459476/how-to-append-to-a-file-in-node
- http://francisshanahan.com/index.php/2011/stream-a-webcam-using-javascript-nodejs-android-opera-mobile-web-sockets-and-html5/
+## Resources
+
+- https://github.com/joyent/node/issues/search?q=fs.watch
+
## License
(The MIT License)
View
3  bin/design.io 100644 → 100755
@@ -1,3 +1,4 @@
#!/usr/bin/env node
-require('../lib/design.io/process.js')
+require.main.paths.push(process.cwd() + "/node_modules")
+require('design.io/lib/design.io/process.js')
View
BIN  favicon.ico
Binary file not shown
View
185 lib/design.io/client.js
@@ -1,108 +1,109 @@
-window.DesignIO = (function() {
- function DesignIO(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.socket = io.connect(this.url);
- this.connect();
- }
+ window.DesignIO = (function() {
- DesignIO.prototype.connect = function() {
- var self, socket;
- socket = this.socket;
- self = this;
- return socket.on('connect', function() {
- socket.emit('userAgent', self.userAgent());
- socket.on('watch', function(data) {
- return self.watch(data);
- });
- return socket.on('exec', function(data) {
- return self.exec(data);
+ function DesignIO(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.socket = io.connect(this.url);
+ this.connect();
+ }
+
+ DesignIO.prototype.connect = function() {
+ var self, socket;
+ socket = this.socket;
+ self = this;
+ return socket.on('connect', function() {
+ socket.emit('userAgent', self.userAgent());
+ socket.on('watch', function(data) {
+ return self.watch(data);
+ });
+ return socket.on('exec', function(data) {
+ return self.exec(data);
+ });
});
- });
- };
+ };
- DesignIO.prototype.on = function(name, callback) {
- return this.callbacks[name] = callback;
- };
+ DesignIO.prototype.on = function(name, callback) {
+ return this.callbacks[name] = callback;
+ };
- DesignIO.prototype.runCallback = function(name, data) {
- if (this.callbacks[name]) this.callbacks[name].call(this, data);
- return true;
- };
+ DesignIO.prototype.runCallback = function(name, data) {
+ if (this.callbacks[name]) this.callbacks[name].call(this, data);
+ return true;
+ };
- DesignIO.prototype.watch = function(data) {
- var watcher, watchers, _i, _len, _results;
- this.watchers = watchers = JSON.parse(data, this.reviver).body;
- _results = [];
- for (_i = 0, _len = watchers.length; _i < _len; _i++) {
- watcher = watchers[_i];
- watcher.client = this;
- watcher.log = function(data) {
- data.path || (data.path = this.path);
- data.action || (data.action = this.action);
- data.timestamp || (data.timestamp = new Date);
- data.id = this.id;
- return this.client.log(data);
- };
- if (watcher.hasOwnProperty("connect")) {
- _results.push(watcher.connect());
- } else {
- _results.push(void 0);
+ DesignIO.prototype.watch = function(data) {
+ var watcher, watchers, _i, _len, _results;
+ this.watchers = watchers = JSON.parse(data, this.reviver).body;
+ _results = [];
+ for (_i = 0, _len = watchers.length; _i < _len; _i++) {
+ watcher = watchers[_i];
+ watcher.client = this;
+ watcher.log = function(data) {
+ data.path || (data.path = this.path);
+ data.action || (data.action = this.action);
+ data.timestamp || (data.timestamp = new Date);
+ data.id = this.id;
+ return this.client.log(data);
+ };
+ if (watcher.hasOwnProperty("connect")) {
+ _results.push(watcher.connect());
+ } else {
+ _results.push(void 0);
+ }
}
- }
- return _results;
- };
+ return _results;
+ };
- DesignIO.prototype.exec = function(data) {
- var watcher, watchers, _i, _len;
- data = JSON.parse(data, this.reviver);
- watchers = this.watchers;
- for (_i = 0, _len = watchers.length; _i < _len; _i++) {
- watcher = watchers[_i];
- if (watcher.id === data.id) {
- watcher.path = data.path;
- watcher.action = data.action;
- if (watcher.hasOwnProperty(data.action)) watcher[data.action](data);
+ DesignIO.prototype.exec = function(data) {
+ var watcher, watchers, _i, _len;
+ data = JSON.parse(data, this.reviver);
+ watchers = this.watchers;
+ for (_i = 0, _len = watchers.length; _i < _len; _i++) {
+ watcher = watchers[_i];
+ if (watcher.id === data.id) {
+ watcher.path = data.path;
+ watcher.action = data.action;
+ if (watcher.hasOwnProperty(data.action)) watcher[data.action](data);
+ }
}
- }
- return this.runCallback(data.action, data);
- };
+ return this.runCallback(data.action, data);
+ };
- DesignIO.prototype.log = function(data) {
- if (typeof data === "object") {
- data.userAgent = window.navigator.userAgent;
- data.url = window.location.href;
- }
- return this.socket.emit('log', JSON.stringify(data, this.replacer));
- };
+ DesignIO.prototype.log = function(data) {
+ if (typeof data === "object") {
+ data.userAgent = window.navigator.userAgent;
+ data.url = window.location.href;
+ }
+ return this.socket.emit('log', JSON.stringify(data, this.replacer));
+ };
- DesignIO.prototype.userAgent = function() {
- return {
- userAgent: window.navigator.userAgent,
- url: window.location.href
+ DesignIO.prototype.userAgent = function() {
+ return {
+ userAgent: window.navigator.userAgent,
+ url: window.location.href
+ };
};
- };
- DesignIO.prototype.replacer = function(key, value) {
- if (typeof value === "function") {
- return "(" + value + ")";
- } else {
- return value;
- }
- };
+ DesignIO.prototype.replacer = function(key, value) {
+ if (typeof value === "function") {
+ return "(" + value + ")";
+ } else {
+ return value;
+ }
+ };
- DesignIO.prototype.reviver = function(key, value) {
- if (typeof value === "string" && !!value.match(/^(?:\(function\s*\([^\)]*\)\s*\{|\(\/)/) && !!value.match(/(?:\}\s*\)|\/\w*\))$/)) {
- return eval(value);
- } else {
- return value;
- }
- };
+ DesignIO.prototype.reviver = function(key, value) {
+ if (typeof value === "string" && !!value.match(/^(?:\(function\s*\([^\)]*\)\s*\{|\(\/)/) && !!value.match(/(?:\}\s*\)|\/\w*\))$/)) {
+ return eval(value);
+ } else {
+ return value;
+ }
+ };
- return DesignIO;
+ return DesignIO;
-})();
+ })();
View
1  lib/design.io/command.js
@@ -11,6 +11,7 @@
program.watchfile || (program.watchfile = "Watchfile");
program.port = program.port ? parseInt(program.port) : process.env.PORT || 4181;
program.url || (program.url = "http://localhost:" + program.port);
+ program.command = program.args[0] || "watch";
}
Command.prototype.run = function() {
View
3  lib/design.io/connection.js
@@ -10,10 +10,7 @@
io.set('log level', 1);
designer = io.of("/design.io");
designer.on("connection", function(socket) {
- console.log("CONNECTION")
socket.on("userAgent", function(data) {
- console.log("USER AGENT")
- console.log(data);
return socket.set("userAgent", data, function() {
socket.emit("ready");
Watcher.connect();
View
19 lib/design.io/listener.js
@@ -9,15 +9,17 @@
Listener = (function() {
- function Listener(root, callback) {
+ function Listener(options, callback) {
var directories, files, path, paths, self, source, stat, _i, _len;
- this.root = root;
+ this.root = options.root;
+ this.ignored = options.ignore || [];
this.directories = directories = {};
this.files = files = {};
- paths = require('findit').sync(root);
+ paths = File.glob(this.root);
self = this;
for (_i = 0, _len = paths.length; _i < _len; _i++) {
source = paths[_i];
+ if (!File.exists(source)) continue;
stat = File.stat(source);
path = _path.join(root, source.replace(root, ""));
if (!stat.isDirectory()) {
@@ -35,6 +37,16 @@
}
}
+ Listener.prototype.ignore = function(path) {
+ var ignoredPath, _i, _len, _ref;
+ _ref = this.ignored;
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ ignoredPath = _ref[_i];
+ if (path.indexOf(ignoredPath) === 0) return true;
+ }
+ return false;
+ };
+
Listener.prototype.changed = function(path, callback) {
var absolutePath, action, base, changed, current, deleted, directories, entries, entry, files, previous, relativePath, timestamp, _i, _len, _results;
entries = File.entries(path);
@@ -43,6 +55,7 @@
directories = this.directories;
files = this.files;
base = this.root;
+ if (this.ignore(path)) return;
if (directories[path] && entries.length < directories[path].length) {
directories = this.directories;
action = "destroy";
View
44 lib/design.io/listener/mac.js
@@ -1,35 +1,39 @@
(function() {
- var Mac, exec, spawn, _ref;
+ var Mac;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
- _ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec;
-
Mac = (function() {
__extends(Mac, require('../listener'));
- function Mac(pathfinder, callback) {
- var command, self;
- Mac.__super__.constructor.call(this, pathfinder, callback);
- self = this;
- command = spawn('ruby', ["" + __dirname + "/mac.rb"]);
- command.stdout.setEncoding('utf8');
- command.stdout.on('data', function(data) {
+ function Mac(options, callback) {
+ var child, forever;
+ var _this = this;
+ Mac.__super__.constructor.apply(this, arguments);
+ forever = require("forever");
+ child = forever.start(["ruby", "" + __dirname + "/mac.rb", this.root.replace(" ", "\\ ")], {
+ max: 10,
+ silent: true
+ });
+ child.on("stdout", function(data) {
var path, _i, _len, _results;
- data = JSON.parse("[" + data.replace(/\]\[/g, ",").replace(/[\[\]]/g, "") + "]");
- _results = [];
- for (_i = 0, _len = data.length; _i < _len; _i++) {
- path = data[_i];
- _results.push(self.changed(path.slice(0, -1), callback));
+ data = data.toString().trim();
+ try {
+ data = JSON.parse("[" + data.replace(/\]\[/g, ",").replace(/[\[\]]/g, "") + "]");
+ _results = [];
+ for (_i = 0, _len = data.length; _i < _len; _i++) {
+ path = data[_i];
+ _results.push(_this.changed(path.slice(0, -1), callback));
+ }
+ return _results;
+ } catch (error) {
+ return _console.error(error.toString());
}
- return _results;
});
- command.stdout.setEncoding('utf8');
- command.stderr.on('data', function(data) {
+ child.on("stderr", function(data) {
return _console.error(data.toString().trim());
});
- command.stdin.write(this.root);
- command.stdin.end();
+ forever.startServer(child);
}
return Mac;
View
7 lib/design.io/listener/mac.rb
@@ -1,10 +1,13 @@
+require 'rubygems'
require 'rb-fsevent'
fsevent = FSEvent.new
STDOUT.sync = true
io = STDOUT
-directory = STDIN.read
+directory = ARGV[0]
+
+#directory = STDIN.read
fsevent.watch directory do |directories|
- io.write directories
+ io.write directories #.inspect?
end
fsevent.run
View
41 lib/design.io/process.js
@@ -1,22 +1,35 @@
(function() {
- var command, exec, server, spawn, _ref;
-
- _ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec;
+ var command, forever, server;
global._console || (global._console = require('underscore.logger'));
command = new (require("" + __dirname + "/command"))(process.argv);
- command.run();
-
- server = spawn("node", ["" + __dirname + "/server", "--watchfile", command.program.watchfile, "--directory", command.program.directory, "--port", command.program.port]);
-
- server.stdout.on('data', function(data) {});
-
- server.stderr.on('data', function(data) {
- return console.log(data.toString().trim());
- });
-
- _console.info("Design.io started on port " + command.program.port);
+ if (command.program.command === "start") {
+ forever = require("forever");
+ server = forever.start(["node", "" + __dirname + "/server.js", "--watchfile", command.program.watchfile, "--directory", command.program.directory, "--port", command.program.port], {
+ max: 1,
+ silent: true,
+ killTree: true
+ });
+ server.on("stdout", function(data) {
+ return console.log(data.toString().trim());
+ });
+ server.on("error", function(error) {
+ return console.log(error);
+ });
+ server.on("exit", function() {
+ return console.log("EXIT");
+ });
+ server.on("start", function(process, data) {
+ return this;
+ });
+ server.on("stderr", function(data) {
+ return _console.error(data.toString().trim());
+ });
+ forever.startServer(server);
+ } else {
+ command.run();
+ }
}).call(this);
View
4 lib/design.io/server.js
@@ -3,8 +3,6 @@
command = new (require("./command"))(process.argv);
- command.run();
-
io = require('socket.io');
express = require("express");
@@ -30,6 +28,8 @@
return response.send(request.params.event);
});
+ app.get("design.io", function(request, response) {});
+
_console.info("Design.io started on port " + command.program.port);
}).call(this);
View
41 lib/design.io/watcher.js
@@ -1,5 +1,5 @@
(function() {
- var Pathfinder, Shift, Watcher, async, fs, path, request, uuid;
+ var File, Pathfinder, Shift, Watcher, async, fs, path, request, uuid;
fs = require('fs');
@@ -15,6 +15,8 @@
Pathfinder = require('pathfinder');
+ File = Pathfinder.File;
+
require('underscore.logger');
Watcher = (function() {
@@ -23,7 +25,7 @@
if (options == null) options = {};
this.directory = options.directory;
this.pathfinder = new Pathfinder(this.directory);
- this.watchfile = options.watchfile;
+ this.watchfile = File.absolutePath(options.watchfile);
this.port = options.port;
this.url = options.url;
if (!this.watchfile) throw new Error("You must specify the watchfile");
@@ -31,7 +33,10 @@
throw new Error("You must specify the directory to watch");
}
this.read(function() {
- return new (require('./listener/mac'))(Watcher.pathfinder.root, function(path, options) {
+ return new (require('./listener/mac'))({
+ root: Watcher.pathfinder.root,
+ ignore: Watcher.ignoredPaths
+ }, function(path, options) {
return Watcher.changed(path, options);
});
});
@@ -39,16 +44,24 @@
};
Watcher.read = function(callback) {
- var self;
- self = this;
+ var _this = this;
+ Watcher.ignoredPaths = [];
return fs.readFile(this.watchfile, "utf-8", function(error, result) {
- var engine;
+ 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));
+ }
return engine.render(result, function(error, result) {
var context;
- context = " function() { var watch = this.watch; var ignorePaths = this.ignorePaths; var watcher = this.watcher; global.Watcher = require('./watcher'); " + result + " delete global.Watcher } ";
- eval("(" + context + ")").call(new Watcher.Watchfile);
- if (callback) return callback.call(self);
+ context = "global.Watcher = require('design.io/lib/design.io/watcher');\nvar __watchfile = new Watcher.Watchfile();\nvar watch = __watchfile.watch;\nvar ignorePaths = __watchfile.ignorePaths;\n" + result + "\ndelete global.Watcher";
+ mainModule._compile(context, mainModule.filename);
+ if (callback) return callback.call(_this);
});
});
};
@@ -89,7 +102,9 @@
};
Watcher.replacer = function(key, value) {
- if (typeof value === "function" || value instanceof RegExp) {
+ if (value instanceof RegExp) {
+ return "(function() { return new RegExp('" + value + "') })";
+ } else if (typeof value === "function") {
return "(" + value + ")";
} else {
return value;
@@ -136,7 +151,7 @@
switch (watcher[action].length) {
case 0:
case 1:
- watcher[action].call(watcher);
+ watcher[action].call(watcher, path);
return watcherCallback();
case 2:
return watcher[action].call(watcher, path, watcherCallback);
@@ -325,8 +340,8 @@
}
Watchfile.prototype.ignorePaths = function() {
- var args;
- return args = Array.prototype.slice.call(arguments, 0, arguments.length);
+ Watcher.ignoredPaths || (Watcher.ignoredPaths = []);
+ return Watcher.ignoredPaths = Watcher.ignoredPaths.concat(Array.prototype.slice.call(arguments, 0, arguments.length));
};
Watchfile.prototype.watch = function() {
View
6 package.json
@@ -29,6 +29,8 @@
"commander": ">= 0.3.2",
"socket.io": ">= 0.8.6",
"request": ">= 2.1.1",
+ "seq": ">= 0.3.5",
+ "findit": ">= 0.1.2",
"pathfinder": ">= 0.1.7",
"shift": ">= 0.1.8",
"async": ">= 0.1.15",
@@ -36,9 +38,9 @@
"underscore.logger": ">= 0.3.1",
"connect": ">= 1.7.3",
"express": ">= 2.5.0",
- "findit": ">= 0.1.1"
+ "forever": ">= 0.8.5"
},
"scripts": {
- "install": "gem install rb-fsevent --no-ri --no-rdoc ; npm install design.io-stylesheets ; npm install design.io-javascripts"
+ "install": "gem install rb-fsevent --no-ri --no-rdoc"
}
}
View
1  src/design.io/client.coffee
@@ -6,7 +6,6 @@ class window.DesignIO
@port = options.port || 4181
@url = options.url || "#{window.location.protocol}//#{window.location.hostname}:#{@port}/design.io"
@socket = io.connect(@url)
-
@connect()
connect: ->
View
1  src/design.io/command.coffee
@@ -14,6 +14,7 @@ class Command
program.watchfile ||= "Watchfile"
program.port = if program.port then parseInt(program.port) else (process.env.PORT || 4181)
program.url ||= "http://localhost:#{program.port}"
+ program.command = program.args[0] || "watch"
run: ->
program = @program
View
1  src/design.io/connection.coffee
@@ -11,7 +11,6 @@ module.exports = (portOrIo) ->
designer.on "connection", (socket) ->
socket.on "userAgent", (data) ->
- console.log data
socket.set "userAgent", data, ->
socket.emit "ready"
Watcher.connect()
View
16 src/design.io/listener.coffee
@@ -3,13 +3,16 @@ Pathfinder = require 'pathfinder'
File = Pathfinder.File
class Listener
- constructor: (root, callback) ->
- @root = root
+ constructor: (options, callback) ->
+ @root = options.root
+ @ignored = options.ignore || []
@directories = directories = {}
@files = files = {}
- paths = require('findit').sync(root)
+ paths = File.glob(@root)
self = @
+
for source in paths
+ continue unless File.exists(source)
stat = File.stat(source)
path = _path.join(root, source.replace(root, ""))
unless stat.isDirectory()
@@ -20,6 +23,11 @@ class Listener
console.log error.stack
else
directories[path] = File.entries(path)
+
+ ignore: (path) ->
+ for ignoredPath in @ignored
+ return true if path.indexOf(ignoredPath) == 0
+ return false
changed: (path, callback) ->
entries = File.entries(path)
@@ -28,7 +36,7 @@ class Listener
directories = @directories
files = @files
base = @root
-
+ return if @ignore(path)
if directories[path] && entries.length < directories[path].length
directories = @directories
action = "destroy"
View
30 src/design.io/listener/mac.coffee
@@ -1,26 +1,26 @@
-{spawn, exec} = require 'child_process'
-# https://github.com/thibaudgg/rb-fsevent
-
class Mac extends (require('../listener'))
- constructor: (pathfinder, callback) ->
- super(pathfinder, callback)
+ constructor: (options, callback) ->
+ super
- self = @
+ forever = require("forever")
- command = spawn 'ruby', ["#{__dirname}/mac.rb"]
- command.stdout.setEncoding('utf8')
- command.stdout.on 'data', (data) ->
+ child = forever.start ["ruby","#{__dirname}/mac.rb", @root.replace(" ", "\\ ")],
+ max : 10
+ silent : true
+
+ child.on "stdout", (data) =>
+ data = data.toString().trim()
try
data = JSON.parse("[" + data.replace(/\]\[/g, ",").replace(/[\[\]]/g, "") + "]")
# console.log(data.toString().trim())
for path in data
- self.changed(path[0..-2], callback)
+ @changed(path[0..-2], callback)
catch error
-
- command.stdout.setEncoding('utf8')
- command.stderr.on 'data', (data) ->
+ _console.error error.toString()
+
+ child.on "stderr", (data) ->
_console.error data.toString().trim()
- command.stdin.write @root
- command.stdin.end()
+ forever.startServer(child)
+
module.exports = Mac
View
11 src/design.io/listener/mac.rb
@@ -1,11 +0,0 @@
-require 'rubygems'
-require 'rb-fsevent'
-
-fsevent = FSEvent.new
-STDOUT.sync = true
-io = STDOUT
-directory = STDIN.read
-fsevent.watch directory do |directories|
- io.write directories #.inspect?
-end
-fsevent.run
View
37 src/design.io/process.coffee
@@ -1,19 +1,28 @@
-{spawn, exec} = require 'child_process'
-
global._console ||= require('underscore.logger')
command = new (require("#{__dirname}/command"))(process.argv)
-command.run()
-server = spawn "node", [
- "#{__dirname}/server",
- "--watchfile", command.program.watchfile,
- "--directory", command.program.directory,
- "--port", command.program.port
-]
-server.stdout.on 'data', (data) ->
- # console.log data.toString().trim()
-server.stderr.on 'data', (data) ->
- console.log data.toString().trim()
+if command.program.command == "start"
+ forever = require("forever")
+ server = forever.start([
+ "node",
+ "#{__dirname}/server.js",
+ "--watchfile", command.program.watchfile,
+ "--directory", command.program.directory,
+ "--port", command.program.port
+ ], {max: 1, silent: true, killTree: true})
-_console.info("Design.io started on port #{command.program.port}")
+ server.on "stdout", (data) ->
+ console.log data.toString().trim()
+ server.on "error", (error) ->
+ console.log error
+ server.on "exit", ->
+ console.log "EXIT"
+ server.on "start", (process, data) ->
+ @
+ server.on "stderr", (data) ->
+ _console.error data.toString().trim()
+
+ forever.startServer(server)
+else
+ command.run()
View
7 src/design.io/server.coffee
@@ -1,13 +1,11 @@
command = new (require("./command"))(process.argv)
-command.run()
+#command.run()
io = require('socket.io')
express = require("express")
connect = require('connect')
Watcher = require("../../lib/design.io/watcher")
-#Watcher.initialize watchfile: "Watchfile", directory: process.cwd(), port: 4181, url: "http://localhost:4181"
-
app = express.createServer()
# io = io.listen(app)
@@ -24,5 +22,8 @@ app.use connect.bodyParser()
app.post '/design.io/:event', (request, response) ->
designer.emit request.params.event, JSON.stringify(request.body)
response.send request.params.event
+
+app.get "design.io", (request, response) ->
+
_console.info "Design.io started on port #{command.program.port}"
View
53 src/design.io/watcher.coffee
@@ -5,13 +5,14 @@ async = require 'async'
Shift = require 'shift'
request = require 'request'
Pathfinder = require 'pathfinder'
+File = Pathfinder.File
require 'underscore.logger'
class Watcher
@initialize: (options = {}) ->
@directory = options.directory
@pathfinder = new Pathfinder(@directory)
- @watchfile = options.watchfile
+ @watchfile = File.absolutePath(options.watchfile)
@port = options.port
@url = options.url
@@ -19,31 +20,34 @@ class Watcher
throw new Error("You must specify the directory to watch") unless @directory
@read ->
- new (require('./listener/mac')) Watcher.pathfinder.root, (path, options) ->
+ new (require('./listener/mac')) root: Watcher.pathfinder.root, ignore: Watcher.ignoredPaths, (path, options) ->
Watcher.changed(path, options)
@
@read: (callback) ->
- self = @
-
- fs.readFile @watchfile, "utf-8", (error, result) ->
- engine = new Shift.CoffeeScript
- engine.render result, (error, result) ->
- context = "
- function() {
- var watch = this.watch;
- var ignorePaths = this.ignorePaths;
- var watcher = this.watcher;
- global.Watcher = require('./watcher');
- #{result}
- delete global.Watcher
- }
- "
-
- eval("(#{context})").call(new Watcher.Watchfile)
-
- callback.call(self) if callback
+ Watcher.ignoredPaths = []
+ fs.readFile @watchfile, "utf-8", (error, result) =>
+ engine = new Shift.CoffeeScript
+ mainModule = require.main
+ paths = mainModule.paths
+ mainModule.moduleCache and= {}
+ mainModule.filename = '.'
+ if process.binding('natives').module
+ {Module} = require 'module'
+ mainModule.paths = Module._nodeModulePaths(File.dirname(@watchfile))
+ engine.render result, (error, result) =>
+ context = """
+ global.Watcher = require('design.io/lib/design.io/watcher');
+ var __watchfile = new Watcher.Watchfile();
+ var watch = __watchfile.watch;
+ var ignorePaths = __watchfile.ignorePaths;
+ #{result}
+ delete global.Watcher
+ """
+ mainModule._compile context, mainModule.filename
+ #eval("(#{context})").call()
+ callback.call(@) if callback
@store: ->
@_store ||= []
@@ -69,7 +73,9 @@ class Watcher
data
@replacer: (key, value) ->
- if typeof value == "function" || value instanceof RegExp
+ if value instanceof RegExp
+ "(function() { return new RegExp('#{value}') })"
+ else if typeof value == "function"
"(#{value})"
else
value
@@ -270,7 +276,8 @@ class Watcher
Watcher._store = undefined
ignorePaths: ->
- args = Array.prototype.slice.call(arguments, 0, arguments.length)
+ Watcher.ignoredPaths ||= []
+ Watcher.ignoredPaths = Watcher.ignoredPaths.concat Array.prototype.slice.call(arguments, 0, arguments.length)
watch: ->
Watcher.create(arguments...)
View
0  spec/app/application.coffee → test/app/application.coffee
File renamed without changes
View
0  spec/app/application.js → test/app/application.js
File renamed without changes
View
0  spec/app/images/delete.png → test/app/images/delete.png
File renamed without changes
View
0  spec/app/images/ok.png → test/app/images/ok.png
File renamed without changes
View
0  spec/app/images/stickynote-large.png → test/app/images/stickynote-large.png
File renamed without changes
View
0  spec/app/images/stickynote-medium.png → test/app/images/stickynote-medium.png
File renamed without changes
View
0  spec/app/javascripts/application.coffee → test/app/javascripts/application.coffee
File renamed without changes
View
0  spec/app/javascripts/application.js → test/app/javascripts/application.js
File renamed without changes
View
0  spec/app/javascripts/example.coffee → test/app/javascripts/example.coffee
File renamed without changes
View
0  spec/app/javascripts/nested-example.coffee → test/app/javascripts/nested-example.coffee
File renamed without changes
View
0  spec/app/javascripts/socket.io.js → test/app/javascripts/socket.io.js
File renamed without changes
View
0  spec/app/server.js → test/app/server.js
File renamed without changes
View
0  spec/app/stylesheets/another.styl → test/app/stylesheets/another.styl
File renamed without changes
View
0  spec/app/stylesheets/application.styl → test/app/stylesheets/application.styl
File renamed without changes
View
0  spec/app/stylesheets/css.css → test/app/stylesheets/css.css
File renamed without changes
View
0  spec/app/stylesheets/style.css → test/app/stylesheets/style.css
File renamed without changes
View
0  spec/app/views/index.jade → test/app/views/index.jade
File renamed without changes
View
0  spec/app/views/index.mustache → test/app/views/index.mustache
File renamed without changes
View
0  spec/app/views/layout.jade → test/app/views/layout.jade
File renamed without changes
View
0  spec/javascriptExtension.coffee → test/javascriptExtension.coffee
File renamed without changes
View
0  spec/stylesheetExtension.coffee → test/stylesheetExtension.coffee
File renamed without changes
View
0  spec/tmp/test.css → test/tmp/test.css
File renamed without changes
View
0  spec/tmp/test.js → test/tmp/test.js
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.