vigil
Version:
Simple, efficient file watching.
53 lines (44 loc) • 1.78 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var _watch, cluster, fs, path, utils, watch;
cluster = require('cluster');
fs = require('fs');
path = require('path');
utils = require('./utils');
watch = require('./watch');
_watch = function(dir) {
return require('vigil').watch(dir, function(filename, stats, isModule) {
console.log(filename + " changed, reloading");
return process.exit(0);
});
};
module.exports = function(fn, cb) {
if (cb == null) {
cb = function() {};
}
return utils.tmpFile('.worker-tmp', function(err, filename, fd) {
var code;
if (err != null) {
throw err;
}
code = "// try to require coffee-script so subsequent requires to coffee files works\ntry {\n // CoffeeScript 1.7+\n require('coffee-script/register');\n} catch (err) {\n // do nothing, coffee-script isn't supported, oh well\n}\n\n// change mainModule to fake file in cwd, so requires work as expected\nprocess.mainModule.filename = '" + (path.join(process.cwd(), 'tmp-worker')) + "';\n\n// set __dirname\n__dirname = '" + (process.cwd()) + "';\n\n// start watching cwd for changes\n(" + (_watch.toString().replace('vigil', __dirname)) + "('" + (process.cwd()) + "'));\n\n// execute callback as worker process\n(" + (fn.toString()) + "());";
return fs.write(fd, code, 0, 'utf8', function(err) {
if (err != null) {
throw err;
}
return fs.close(fd, function() {
cluster.setupMaster({
exec: filename,
silent: false
});
cluster.fork();
cluster.on('exit', function(worker, code, signal) {
return cluster.fork();
});
return cluster.once('listening', function() {
return cb();
});
});
});
});
};
//# sourceMappingURL=run.js.map