1 |
|
2 | (function() {
|
3 | var DirWatcher, EventEmitter, FileWatcher, Watcher, cp, cp_r, find, fs, ls, mkdir_p, os, path, rm_rf, touch, util,
|
4 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
|
5 | __hasProp = {}.hasOwnProperty,
|
6 | __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; };
|
7 |
|
8 | path = require('path');
|
9 |
|
10 | fs = require('fs');
|
11 |
|
12 | exports.touch = touch = function(filepath, encoding) {
|
13 | var dir_to;
|
14 | if (encoding == null) {
|
15 | encoding = 'utf-8';
|
16 | }
|
17 | dir_to = path.dirname(touch);
|
18 | return fs.writeFileSync(filepath, '', encoding);
|
19 | };
|
20 |
|
21 | exports.rm_rf = rm_rf = function(folderpath, root) {
|
22 | var file, files, stats, _i, _len;
|
23 | if (root == null) {
|
24 | root = true;
|
25 | }
|
26 | files = fs.readdirSync(path.resolve(folderpath));
|
27 | for (_i = 0, _len = files.length; _i < _len; _i++) {
|
28 | file = files[_i];
|
29 | file = path.resolve("" + folderpath + "/" + file);
|
30 | stats = fs.statSync(file);
|
31 | if (stats.isDirectory()) {
|
32 | rm_rf(file, false);
|
33 | fs.rmdirSync(file);
|
34 | } else {
|
35 | fs.unlinkSync(file);
|
36 | }
|
37 | }
|
38 | if (root) {
|
39 | return fs.rmdirSync(folderpath);
|
40 | }
|
41 | };
|
42 |
|
43 | exports.mkdir_p = mkdir_p = function(fullpath, mode) {
|
44 | var exists, folder, folderpath, folders, index, _i, _len;
|
45 | if (mode == null) {
|
46 | mode = '0755';
|
47 | }
|
48 | fullpath = path.resolve(fullpath);
|
49 | folders = fullpath.split(path.sep);
|
50 | if (folders[0] === '') {
|
51 | folders[0] = '/';
|
52 | }
|
53 | for (index = _i = 0, _len = folders.length; _i < _len; index = ++_i) {
|
54 | folder = folders[index];
|
55 | folderpath = path.join.apply(null, folders.slice(0, index + 1));
|
56 | exists = fs.existsSync(folderpath);
|
57 | if (exists && index === folders.length - 1) {
|
58 | throw new Error(error("Folder exists: " + folder.red));
|
59 | return false;
|
60 | } else if (!exists) {
|
61 | fs.mkdirSync(folderpath, mode);
|
62 | }
|
63 | }
|
64 | return true;
|
65 | };
|
66 |
|
67 | exports.cp = cp = function(from, to) {
|
68 | if (!fs.statSync(from).isDirectory()) {
|
69 | return fs.writeFileSync(to, fs.readFileSync(from));
|
70 | }
|
71 | };
|
72 |
|
73 | exports.cp_r = cp_r = function(from, to) {
|
74 | var dir_to, file_from, file_to, files, _i, _len, _ref, _results;
|
75 | from = path.resolve(from);
|
76 | to = path.resolve(to);
|
77 | if ((from.slice(-1)) === '/') {
|
78 | from = from.slice(0, -1);
|
79 | }
|
80 | if ((to.slice(-1)) === '/') {
|
81 | to = to.slice(0, -1);
|
82 | }
|
83 | _ref = (files = find(from, /.*/, false));
|
84 | _results = [];
|
85 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
86 | file_from = _ref[_i];
|
87 | file_to = file_from.replace(from, to);
|
88 | dir_to = path.dirname(file_to);
|
89 | if (!fs.existsSync(dir_to)) {
|
90 | mkdir_p(dir_to);
|
91 | }
|
92 | _results.push(cp(file_from, file_to));
|
93 | }
|
94 | return _results;
|
95 | };
|
96 |
|
97 | exports.find = find = function(folderpath, pattern, include_dirs) {
|
98 | var file, filepath, files, found, found_under, _i, _len;
|
99 | if (include_dirs == null) {
|
100 | include_dirs = false;
|
101 | }
|
102 | found = [];
|
103 | files = fs.readdirSync(folderpath);
|
104 | for (_i = 0, _len = files.length; _i < _len; _i++) {
|
105 | file = files[_i];
|
106 | filepath = path.join(folderpath, file);
|
107 | if ((fs.statSync(filepath)).isDirectory()) {
|
108 | if (include_dirs && filepath.match(pattern)) {
|
109 | found = found.concat(filepath);
|
110 | }
|
111 | found_under = find(filepath, pattern, include_dirs);
|
112 | found = found.concat(found_under);
|
113 | } else {
|
114 | if (filepath.match(pattern)) {
|
115 | found.push(filepath);
|
116 | }
|
117 | }
|
118 | }
|
119 | return found;
|
120 | };
|
121 |
|
122 | exports.ls = ls = function(folderpath) {
|
123 | var file, filepath, files, found, _i, _len;
|
124 | found = [];
|
125 | files = fs.readdirSync(folderpath);
|
126 | for (_i = 0, _len = files.length; _i < _len; _i++) {
|
127 | file = files[_i];
|
128 | filepath = path.join(folderpath, file);
|
129 | if ((fs.statSync(filepath)).isDirectory()) {
|
130 | found.push(filepath);
|
131 | }
|
132 | }
|
133 | return found;
|
134 | };
|
135 |
|
136 | fs = require('fs');
|
137 |
|
138 | os = require('os');
|
139 |
|
140 | path = require('path');
|
141 |
|
142 | util = require('util');
|
143 |
|
144 | EventEmitter = require('events').EventEmitter;
|
145 |
|
146 | FileWatcher = (function() {
|
147 |
|
148 | function FileWatcher(watcher, location, parent, dispatch_created) {
|
149 | this.watcher = watcher;
|
150 | this.location = location;
|
151 | this.parent = parent;
|
152 | this.dispatch_created = dispatch_created != null ? dispatch_created : false;
|
153 | this.onchange = __bind(this.onchange, this);
|
154 |
|
155 | this.config();
|
156 | this.init();
|
157 | this.watch();
|
158 | }
|
159 |
|
160 | FileWatcher.prototype.config = function() {
|
161 | this.type = 'file';
|
162 | return this.curr = fs.statSync(this.location);
|
163 | };
|
164 |
|
165 | FileWatcher.prototype.init = function() {
|
166 | if (this.dispatch_created) {
|
167 | return this.watcher.emit('create', this);
|
168 | }
|
169 | };
|
170 |
|
171 | FileWatcher.prototype.watch = function() {
|
172 | var options;
|
173 | options = {
|
174 | persistent: this.watcher.persistent
|
175 | };
|
176 | this._ref = fs.watch(this.location, options, this.onchange);
|
177 | if (/^win/.test(os.platform())) {
|
178 | this._ref.on('error', function() {});
|
179 | }
|
180 | return this.watcher.emit('watch', this);
|
181 | };
|
182 |
|
183 | FileWatcher.prototype.unwatch = function() {
|
184 | this._ref.close();
|
185 | return this.watcher.emit('unwatch', this);
|
186 | };
|
187 |
|
188 | FileWatcher.prototype.onchange = function() {
|
189 | if (!fs.existsSync(this.location)) {
|
190 | this.prev = this.curr;
|
191 | this.curr = null;
|
192 | return;
|
193 | }
|
194 | this.prev = this.curr;
|
195 | this.curr = fs.statSync(this.location);
|
196 | if (this.curr.mtime > this.prev.mtime) {
|
197 | return this.watcher.emit('change', this);
|
198 | }
|
199 | };
|
200 |
|
201 | FileWatcher.prototype["delete"] = function() {
|
202 | this.unwatch();
|
203 | this.parent.tree[this.location] = null;
|
204 | delete this.parent.tree[this.location];
|
205 | return this.watcher.emit('delete', this);
|
206 | };
|
207 |
|
208 | return FileWatcher;
|
209 |
|
210 | })();
|
211 |
|
212 | DirWatcher = (function() {
|
213 |
|
214 | function DirWatcher(watcher, location, parent, dispatch_created) {
|
215 | this.watcher = watcher;
|
216 | this.location = location;
|
217 | this.parent = parent;
|
218 | this.dispatch_created = dispatch_created != null ? dispatch_created : false;
|
219 | this.onchange = __bind(this.onchange, this);
|
220 |
|
221 | this.config();
|
222 | this.init();
|
223 | this.watch();
|
224 | }
|
225 |
|
226 | DirWatcher.prototype.config = function() {
|
227 | this.type = 'dir';
|
228 | this.tree = {};
|
229 | return this.curr = fs.statSync(this.location);
|
230 | };
|
231 |
|
232 | DirWatcher.prototype.init = function() {
|
233 | var dir, file, fullpath, name, _i, _len, _ref;
|
234 | _ref = fs.readdirSync(this.location);
|
235 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
236 | name = _ref[_i];
|
237 | fullpath = path.resolve("" + this.location + "/" + name);
|
238 | if (fs.statSync(fullpath).isDirectory()) {
|
239 | dir = new DirWatcher(this.watcher, fullpath, this, this.dispatch_created);
|
240 | this.tree[fullpath] = dir;
|
241 | } else if (fs.statSync(fullpath).isFile()) {
|
242 | if (this.watcher.pattern.test(fullpath)) {
|
243 | file = new FileWatcher(this.watcher, fullpath, this, this.dispatch_created);
|
244 | this.tree[fullpath] = file;
|
245 | }
|
246 | }
|
247 | }
|
248 | if (this.dispatch_created) {
|
249 | return this.watcher.emit('create', this);
|
250 | }
|
251 | };
|
252 |
|
253 | DirWatcher.prototype.watch = function() {
|
254 | var options;
|
255 | options = {
|
256 | persistent: this.watcher.persistent
|
257 | };
|
258 | this._ref = fs.watch(this.location, options, this.onchange);
|
259 | if (/^win/.test(os.platform())) {
|
260 | this._ref.on('error', function() {});
|
261 | }
|
262 | return this.watcher.emit('watch', this);
|
263 | };
|
264 |
|
265 | DirWatcher.prototype.unwatch = function(propagate) {
|
266 | var item, location, _ref;
|
267 | if (propagate) {
|
268 | _ref = this.tree;
|
269 | for (location in _ref) {
|
270 | item = _ref[location];
|
271 | item.unwatch(propagate);
|
272 | }
|
273 | }
|
274 | this._ref.close();
|
275 | return this.watcher.emit('unwatch', this);
|
276 | };
|
277 |
|
278 | DirWatcher.prototype.onchange = function() {
|
279 | var created, deleted, _i, _j, _len, _len1, _ref, _ref1, _results;
|
280 | if (!fs.existsSync(this.location)) {
|
281 | this.prev = this.curr;
|
282 | this.curr = null;
|
283 | if (this.location === this.watcher.root) {
|
284 | this["delete"]();
|
285 | }
|
286 | return;
|
287 | }
|
288 | this.prev = this.curr;
|
289 | this.curr = fs.statSync(this.location);
|
290 | ls = this.diff();
|
291 | _ref = ls.deleted;
|
292 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
293 | deleted = _ref[_i];
|
294 | deleted["delete"]();
|
295 | }
|
296 | _ref1 = ls.created;
|
297 | _results = [];
|
298 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
|
299 | created = _ref1[_j];
|
300 | if (fs.statSync(created).isDirectory()) {
|
301 | _results.push(this.tree[created] = new DirWatcher(this.watcher, created, this, true));
|
302 | } else if (fs.statSync(created).isFile()) {
|
303 | _results.push(this.tree[created] = new FileWatcher(this.watcher, created, this, true));
|
304 | } else {
|
305 | _results.push(void 0);
|
306 | }
|
307 | }
|
308 | return _results;
|
309 | };
|
310 |
|
311 | DirWatcher.prototype.diff = function() {
|
312 | var curr, fullpath, isdir, k, name, prev, status, v, _i, _len, _ref;
|
313 | prev = this.tree;
|
314 | curr = {};
|
315 | _ref = fs.readdirSync(this.location);
|
316 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
317 | name = _ref[_i];
|
318 | fullpath = path.resolve("" + this.location + "/" + name);
|
319 | isdir = (fs.statSync(fullpath)).isDirectory();
|
320 | if (isdir || this.watcher.pattern.test(fullpath)) {
|
321 | curr[fullpath] = fullpath;
|
322 | }
|
323 | }
|
324 | status = {
|
325 | deleted: [],
|
326 | created: []
|
327 | };
|
328 | for (k in curr) {
|
329 | v = curr[k];
|
330 | if (prev[k] == null) {
|
331 | status.created.push(v);
|
332 | }
|
333 | }
|
334 | for (k in prev) {
|
335 | v = prev[k];
|
336 | if (curr[k] == null) {
|
337 | status.deleted.push(v);
|
338 | }
|
339 | }
|
340 | return status;
|
341 | };
|
342 |
|
343 | DirWatcher.prototype["delete"] = function(dispatch_event) {
|
344 | var item, location, _ref;
|
345 | if (dispatch_event == null) {
|
346 | dispatch_event = true;
|
347 | }
|
348 | _ref = this.tree;
|
349 | for (location in _ref) {
|
350 | item = _ref[location];
|
351 | item["delete"](this.watcher.recursive);
|
352 | }
|
353 | this.unwatch();
|
354 | this.parent.tree[this.location] = null;
|
355 | delete this.parent.tree[this.location];
|
356 | if (dispatch_event) {
|
357 | return this.watcher.emit('delete', this);
|
358 | }
|
359 | };
|
360 |
|
361 | return DirWatcher;
|
362 |
|
363 | })();
|
364 |
|
365 | Watcher = (function(_super) {
|
366 |
|
367 | __extends(Watcher, _super);
|
368 |
|
369 | function Watcher(root, pattern, recursive, persistent) {
|
370 | var _this = this;
|
371 | this.pattern = pattern;
|
372 | this.recursive = recursive != null ? recursive : false;
|
373 | this.persistent = persistent != null ? persistent : true;
|
374 | this.config(root);
|
375 | setTimeout(function() {
|
376 | return _this.init();
|
377 | }, 1);
|
378 | }
|
379 |
|
380 | Watcher.prototype.config = function(root) {
|
381 | this.tree = {};
|
382 | this.root = path.resolve(root);
|
383 | if (!fs.existsSync(this.root)) {
|
384 | throw new Error("Not found: " + this.root);
|
385 | }
|
386 | };
|
387 |
|
388 | Watcher.prototype.init = function() {
|
389 | if ((fs.statSync(this.root)).isDirectory()) {
|
390 | return this.tree[this.root] = new DirWatcher(this, this.root, this);
|
391 | } else if ((fs.statSync(this.root)).isFile()) {
|
392 | return this.tree[this.root] = new FileWatcher(this, this.root, this);
|
393 | }
|
394 | };
|
395 |
|
396 | Watcher.prototype.close = function() {
|
397 | var item;
|
398 | item = this.tree[this.root];
|
399 | if (item.type === 'dir') {
|
400 | item.unwatch(true);
|
401 | } else {
|
402 | item.unwatch();
|
403 | }
|
404 | this.removeAllListeners('create');
|
405 | this.removeAllListeners('watch');
|
406 | this.removeAllListeners('change');
|
407 | this.removeAllListeners('unwatch');
|
408 | return this.removeAllListeners('delete');
|
409 | };
|
410 |
|
411 | return Watcher;
|
412 |
|
413 | })(EventEmitter);
|
414 |
|
415 | exports.watch = function(root, pattern, recursive, persistent) {
|
416 | return new Watcher(root, pattern, recursive, persistent);
|
417 | };
|
418 |
|
419 | }).call(this);
|