|
1 |
| -var consoleMessage, cssExtensions, fileUtils, fs, lastRun, pathlib; |
| 1 | +var chokidar, consoleMessage, cssExtensions, lastRun, pathlib, |
| 2 | + __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; |
2 | 3 |
|
3 | 4 | require('colors');
|
4 | 5 |
|
5 |
| -fs = require('fs'); |
6 |
| - |
7 | 6 | pathlib = require('path');
|
8 | 7 |
|
9 |
| -fileUtils = require('../utils/file'); |
| 8 | +chokidar = require('chokidar'); |
10 | 9 |
|
11 | 10 | lastRun = {
|
12 | 11 | updateCSS: Date.now(),
|
13 | 12 | reload: Date.now()
|
14 | 13 | };
|
15 | 14 |
|
16 |
| -cssExtensions = ['css', 'styl', 'stylus', 'less']; |
| 15 | +cssExtensions = ['.css', '.styl', '.stylus', '.less']; |
17 | 16 |
|
18 | 17 | consoleMessage = {
|
19 | 18 | updateCSS: 'CSS files changed. Updating browser...',
|
20 | 19 | reload: 'Client files changed. Reloading browser...'
|
21 | 20 | };
|
22 | 21 |
|
23 | 22 | module.exports = function(ss, options) {
|
24 |
| - var allPaths, assetsToWatch, detectNewFiles, handleFileChange, watch; |
25 |
| - handleFileChange = function(action) { |
| 23 | + var dir, onChange, watchDirs, watcher; |
| 24 | + watchDirs = (function() { |
| 25 | + var _i, _len, _ref, _results; |
| 26 | + _ref = options.liveReload; |
| 27 | + _results = []; |
| 28 | + for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 29 | + dir = _ref[_i]; |
| 30 | + _results.push(pathlib.join(ss.root, options.dirs[dir])); |
| 31 | + } |
| 32 | + return _results; |
| 33 | + })(); |
| 34 | + watcher = chokidar.watch(watchDirs, { |
| 35 | + ignored: /(\/\.|~$)/ |
| 36 | + }); |
| 37 | + watcher.on('add', function(path) { |
| 38 | + return onChange(path, 'added'); |
| 39 | + }); |
| 40 | + watcher.on('change', function(path) { |
| 41 | + return onChange(path, 'changed'); |
| 42 | + }); |
| 43 | + watcher.on('unlink', function(path) { |
| 44 | + return onChange(path, 'removed'); |
| 45 | + }); |
| 46 | + watcher.on('error', function(path) { |
| 47 | + return console.log('✎'.red, ("Error: " + error).red); |
| 48 | + }); |
| 49 | + return onChange = function(path, event) { |
| 50 | + var action, _ref; |
| 51 | + action = (_ref = pathlib.extname(path), __indexOf.call(cssExtensions, _ref) >= 0) ? 'updateCSS' : 'reload'; |
26 | 52 | if ((Date.now() - lastRun[action]) > 1000) {
|
| 53 | + console.log('✎'.green, ("File " + event + ": " + path).grey); |
27 | 54 | console.log('✎'.green, consoleMessage[action].grey);
|
28 | 55 | ss.publish.all('__ss:' + action);
|
29 | 56 | return lastRun[action] = Date.now();
|
30 | 57 | }
|
31 | 58 | };
|
32 |
| - assetsToWatch = function() { |
33 |
| - var output; |
34 |
| - output = { |
35 |
| - files: [], |
36 |
| - dirs: [] |
37 |
| - }; |
38 |
| - options.liveReload.forEach(function(dir) { |
39 |
| - var path, result; |
40 |
| - path = pathlib.join(ss.root, options.dirs[dir]); |
41 |
| - result = fileUtils.readDirSync(path); |
42 |
| - output.files = output.files.concat(result.files); |
43 |
| - return output.dirs = output.dirs.concat(result.dirs); |
44 |
| - }); |
45 |
| - return output; |
46 |
| - }; |
47 |
| - allPaths = assetsToWatch(); |
48 |
| - watch = function(paths) { |
49 |
| - paths.dirs.forEach(function(dir) { |
50 |
| - return fs.watch(dir, detectNewFiles); |
51 |
| - }); |
52 |
| - return paths.files.forEach(function(file) { |
53 |
| - var changeAction, extension, watcher; |
54 |
| - extension = file.split('.')[file.split('.').length - 1]; |
55 |
| - changeAction = cssExtensions.indexOf(extension) >= 0 && 'updateCSS' || 'reload'; |
56 |
| - return watcher = fs.watch(file, function(event) { |
57 |
| - handleFileChange(changeAction); |
58 |
| - if (event === "rename") return watcher.close(); |
59 |
| - }); |
60 |
| - }); |
61 |
| - }; |
62 |
| - detectNewFiles = function() { |
63 |
| - var newPaths, pathsNow; |
64 |
| - pathsNow = assetsToWatch(); |
65 |
| - newPaths = { |
66 |
| - dirs: pathsNow.dirs.filter(function(dir) { |
67 |
| - return allPaths.dirs.indexOf(dir) === -1; |
68 |
| - }), |
69 |
| - files: pathsNow.files.filter(function(file) { |
70 |
| - return allPaths.files.indexOf(file) === -1; |
71 |
| - }) |
72 |
| - }; |
73 |
| - watch(newPaths); |
74 |
| - return allPaths = pathsNow; |
75 |
| - }; |
76 |
| - return watch(allPaths); |
77 | 59 | };
|
0 commit comments