Skip to content

Commit 0358522

Browse files
committed
rebase on latest socketstream, latest chokidar, better events
1 parent 1afeaef commit 0358522

File tree

3 files changed

+51
-92
lines changed

3 files changed

+51
-92
lines changed

lib/client/live_reload.js

+34-52
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,59 @@
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; };
23

34
require('colors');
45

5-
fs = require('fs');
6-
76
pathlib = require('path');
87

9-
fileUtils = require('../utils/file');
8+
chokidar = require('chokidar');
109

1110
lastRun = {
1211
updateCSS: Date.now(),
1312
reload: Date.now()
1413
};
1514

16-
cssExtensions = ['css', 'styl', 'stylus', 'less'];
15+
cssExtensions = ['.css', '.styl', '.stylus', '.less'];
1716

1817
consoleMessage = {
1918
updateCSS: 'CSS files changed. Updating browser...',
2019
reload: 'Client files changed. Reloading browser...'
2120
};
2221

2322
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';
2652
if ((Date.now() - lastRun[action]) > 1000) {
53+
console.log('✎'.green, ("File " + event + ": " + path).grey);
2754
console.log('✎'.green, consoleMessage[action].grey);
2855
ss.publish.all('__ss:' + action);
2956
return lastRun[action] = Date.now();
3057
}
3158
};
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);
7759
};

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"socket.io": "= 0.9.6",
2929
"redis": "= 0.7.1",
3030
"connect": "= 2.0.3",
31-
"connect-redis": "= 1.3.0"
31+
"connect-redis": "= 1.3.0",
32+
"chokidar": "= 0.2.3"
3233
},
3334
"devDependencies": {
3435
"coffee-script": "= 1.2.0",

src/client/live_reload.coffee

+15-39
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
# Detects changes in client files and sends an event to connected browsers instructing them to refresh the page
44

55
require('colors')
6-
fs = require('fs')
76
pathlib = require('path')
8-
fileUtils = require('../utils/file')
7+
chokidar = require('chokidar')
98

109
lastRun =
1110
updateCSS: Date.now()
1211
reload: Date.now()
1312

14-
cssExtensions = ['css', 'styl', 'stylus', 'less']
13+
cssExtensions = ['.css', '.styl', '.stylus', '.less']
1514

1615
consoleMessage =
1716
updateCSS: 'CSS files changed. Updating browser...'
@@ -20,42 +19,19 @@ consoleMessage =
2019

2120
module.exports = (ss, options) ->
2221

23-
handleFileChange = (action) ->
22+
watchDirs = for dir in options.liveReload
23+
pathlib.join ss.root, options.dirs[dir]
24+
25+
watcher = chokidar.watch watchDirs, { ignored: /(\/\.|~$)/ }
26+
watcher.on 'add', (path) -> onChange(path, 'added')
27+
watcher.on 'change', (path) -> onChange(path, 'changed')
28+
watcher.on 'unlink', (path) -> onChange(path, 'removed')
29+
watcher.on 'error', (path) -> console.log(''.red, "Error: #{error}".red)
30+
31+
onChange = (path, event) ->
32+
action = if pathlib.extname(path) in cssExtensions then 'updateCSS' else 'reload'
2433
if (Date.now() - lastRun[action]) > 1000 # Reload browser max once per second
34+
console.log(''.green, "File #{event}: #{path}".grey);
2535
console.log(''.green, consoleMessage[action].grey)
2636
ss.publish.all('__ss:' + action)
27-
lastRun[action] = Date.now()
28-
29-
assetsToWatch = ->
30-
output = {files: [], dirs: []}
31-
options.liveReload.forEach (dir) ->
32-
path = pathlib.join(ss.root, options.dirs[dir])
33-
result = fileUtils.readDirSync(path)
34-
output.files = output.files.concat(result.files)
35-
output.dirs = output.dirs.concat(result.dirs)
36-
output
37-
38-
allPaths = assetsToWatch()
39-
40-
watch = (paths) ->
41-
paths.dirs.forEach (dir) -> fs.watch(dir, detectNewFiles)
42-
paths.files.forEach (file) ->
43-
extension = file.split('.')[file.split('.').length-1]
44-
changeAction = cssExtensions.indexOf(extension) >= 0 && 'updateCSS' || 'reload'
45-
watcher = fs.watch file, (event) ->
46-
handleFileChange(changeAction)
47-
if event == "rename"
48-
watcher.close()
49-
# Disabling for now as file = the old filename and although fs.watch
50-
# should pass (event, filename) this does not work on all OSes
51-
#watch({files: [file], dirs: []})
52-
53-
detectNewFiles = ->
54-
pathsNow = assetsToWatch()
55-
newPaths =
56-
dirs: pathsNow.dirs.filter (dir) -> allPaths.dirs.indexOf(dir) == -1
57-
files: pathsNow.files.filter (file) -> allPaths.files.indexOf(file) == -1
58-
watch(newPaths)
59-
allPaths = pathsNow
60-
61-
watch(allPaths)
37+
lastRun[action] = Date.now()

0 commit comments

Comments
 (0)