UNPKG

browser-build

Version:

Makes commonjs modules available in the browser via window.require('module-name')

119 lines (111 loc) 3.3 kB
// Generated by CoffeeScript 1.6.3 (function() { var fs, np, util; require('sugar'); fs = require('fs'); np = require('path'); util = require('./utility'); exports.watchDirectory = function(dirname, options, listener) { var filter, fsListener, initial, notifyListener, unwatchFile, watchFile, watchedFiles; if (listener == null) { listener = options; options = {}; } if (options.persistent == null) { options.persistent = true; } if (options.interval == null) { options.interval = 100; } if (options.recursive == null) { options.recursive = true; } if (options.initial == null) { options.initial = 'initial'; } if (options.exclude == null) { options.exclude = util.defaultFileExclude; } filter = function(name) { if (util.isMatch(name, options.exclude, false)) { return false; } else { return util.isMatch(name, options.include, true); } }; watchedFiles = {}; notifyListener = function(filename, curr, prev, change, async) { if (async == null) { async = false; } if (filter(filename)) { if (async) { return process.nextTick(function() { return listener(filename, curr, prev, change); }); } else { return listener(filename, curr, prev, change); } } }; fsListener = function(filename, depth, curr, prev) { var change; change = curr.nlink === 0 ? 'deleted' : prev.nlink === 0 ? 'created' : 'modified'; notifyListener(filename, curr, prev, change); if (change !== 'deleted') { return watchFile(filename, depth, curr); } else { return unwatchFile(filename); } }; unwatchFile = function(filename) { fs.unwatchFile(filename, watchedFiles[filename]); return delete watchedFiles[filename]; }; watchFile = function(filename, depth, stats) { var boundListener, child, _i, _len, _ref; if (depth == null) { depth = 0; } if (stats == null) { stats = fs.statSync(filename); } if (stats.nlink > 0) { if (stats.isDirectory()) { if (!util.isMatch(filename, options.exclude, false)) { if (depth === 0 || options.recursive) { _ref = fs.readdirSync(filename); for (_i = 0, _len = _ref.length; _i < _len; _i++) { child = _ref[_i]; child = np.join(filename, child); watchFile(child, depth + 1); } } } } if (watchedFiles[filename] == null) { boundListener = fsListener.bind(this, filename, depth); watchedFiles[filename] = boundListener; fs.watchFile(filename, options, boundListener); if (initial) { notifyListener(filename, stats, stats, initial, true); } } } }; initial = options.initial; watchFile(dirname); initial = 'created'; return function() { var key, _results; _results = []; for (key in watchedFiles) { _results.push(unwatchFile(key)); } return _results; }; }; }).call(this); /* //@ sourceMappingURL=watcher.map */