From 80fc1032c98dc9eddac85827d1a4d5ff5fa29d6b Mon Sep 17 00:00:00 2001 From: xzyfer Date: Tue, 3 Apr 2018 21:50:26 +1000 Subject: [PATCH] Replace Gaze with Chokidar --- bin/emcc | 12 ++ bin/node-sass | 34 +++-- lib/watcher.js | 60 +++++---- package.json | 2 +- test/watcher.js | 320 +++++++++--------------------------------------- 5 files changed, 110 insertions(+), 318 deletions(-) create mode 100755 bin/emcc diff --git a/bin/emcc b/bin/emcc new file mode 100755 index 000000000..e2b822262 --- /dev/null +++ b/bin/emcc @@ -0,0 +1,12 @@ +#!/bin/sh + +skip=0 + +for arg; do + shift + [ "$skip" = "1" ] && skip=0 && continue + [ "$arg" = "-arch" ] && skip=1 && continue + set -- "$@" "$arg" +done + +emcc $@ diff --git a/bin/node-sass b/bin/node-sass index 62abdb229..333fa49c7 100755 --- a/bin/node-sass +++ b/bin/node-sass @@ -2,7 +2,7 @@ var Emitter = require('events').EventEmitter, forEach = require('async-foreach').forEach, - Gaze = require('gaze'), + chokidar = require('chokidar'), meow = require('meow'), util = require('util'), path = require('path'), @@ -230,39 +230,33 @@ function getOptions(args, options) { function watch(options, emitter) { var handler = function(files) { - files.added.forEach(function(file) { - var watch = gaze.watched(); - Object.keys(watch).forEach(function (dir) { - if (watch[dir].indexOf(file) !== -1) { - gaze.add(file); - } - }); - }); - files.changed.forEach(function(file) { if (path.basename(file)[0] !== '_') { renderFile(file, options, emitter); } }); - - files.removed.forEach(function(file) { - gaze.remove(file); - }); }; - var gaze = new Gaze(); - gaze.add(watcher.reset(options)); - gaze.on('error', emitter.emit.bind(emitter, 'error')); + watcher.reset(options); + + var service = chokidar.watch(options.includePath, { + ignore: /(?!\.s[ac]ss$)/, + followSymlinks: options.follow, + }); + + service.on('error', function(error) { + emitter.emit.error(error); + }); - gaze.on('changed', function(file) { + service.on('change', function(file) { handler(watcher.changed(file)); }); - gaze.on('added', function(file) { + service.on('add', function(file) { handler(watcher.added(file)); }); - gaze.on('deleted', function(file) { + service.on('unlink', function(file) { handler(watcher.removed(file)); }); } diff --git a/lib/watcher.js b/lib/watcher.js index cdb965c53..596bcba2c 100644 --- a/lib/watcher.js +++ b/lib/watcher.js @@ -23,68 +23,64 @@ watcher.reset = function(opts) { }; watcher.changed = function(absolutePath) { - var files = { - added: [], - changed: [], - removed: [], - }; + var files = []; this.reset(); - if (absolutePath && path.basename(absolutePath)[0] !== '_') { - files.changed.push(absolutePath); + if (!absolutePath) { + return files; + } + + if (path.basename(absolutePath)[0] !== '_') { + if (Object.keys(graph.index).indexOf(absolutePath) !== -1) { + files.push(absolutePath); + } } graph.visitAncestors(absolutePath, function(parent) { if (path.basename(parent)[0] !== '_') { - files.changed.push(parent); + files.push(parent); } }); - graph.visitDescendents(absolutePath, function(child) { - files.added.push(child); - }); - return files; }; watcher.added = function(absolutePath) { - var files = { - added: [], - changed: [], - removed: [], - }; + var files = []; + var oldIndex = Object.keys(graph.index); this.reset(); - if (Object.keys(graph.index).indexOf(absolutePath) === -1) { - files.added.push(absolutePath); + if (!absolutePath) { + return files; } - graph.visitDescendents(absolutePath, function(child) { - files.added.push(child); - }); + var newIndex = Object.keys(graph.index); + files.concat(newIndex.filter(function (file) { + return oldIndex.indexOf(file) === -1; + })); + + if (path.basename(absolutePath)[0] === '_') { + graph.visitAncestors(absolutePath, function(parent) { + if (path.basename(parent)[0] !== '_') { + files.push(parent); + } + }); + } return files; }; watcher.removed = function(absolutePath) { - var files = { - added: [], - changed: [], - removed: [], - }; + var files = []; graph.visitAncestors(absolutePath, function(parent) { if (path.basename(parent)[0] !== '_') { - files.changed.push(parent); + files.push(parent); } }); - if (Object.keys(graph.index).indexOf(absolutePath) !== -1) { - files.removed.push(absolutePath); - } - this.reset(); return files; diff --git a/package.json b/package.json index d61efb451..9116f8c54 100644 --- a/package.json +++ b/package.json @@ -55,8 +55,8 @@ "dependencies": { "async-foreach": "^0.1.3", "chalk": "^1.1.1", + "chokidar": "^2.0.3", "cross-spawn": "^3.0.0", - "gaze": "^1.0.0", "get-stdin": "^4.0.1", "glob": "^7.0.3", "in-publish": "^2.0.0", diff --git a/test/watcher.js b/test/watcher.js index dca632fad..742415b1d 100644 --- a/test/watcher.js +++ b/test/watcher.js @@ -30,74 +30,37 @@ describe('watcher', function() { it('should record its ancestors as changed', function() { var file = path.join(main, 'partials', '_one.scss'); var files = watcher.changed(file); - assert.deepEqual(files.changed, [ + assert.deepEqual(files, [ path.join(main, 'one.scss'), ]); }); - - it('should record its decendants as added', function() { - var file = path.join(main, 'partials', '_one.scss'); - var files = watcher.changed(file); - assert.deepEqual(files.added, [ - path.join(main, 'partials', '_three.scss'), - ]); - }); - - it('should record nothing as removed', function() { - var file = path.join(main, 'partials', '_one.scss'); - var files = watcher.changed(file); - assert.deepEqual(files.removed, []); - }); }); describe('if it is not a partial', function() { it('should record itself as changed', function() { var file = path.join(main, 'one.scss'); var files = watcher.changed(file); - assert.deepEqual(files.changed, [ + assert.deepEqual(files, [ file, ]); }); - - it('should record its decendants as added', function() { - var file = path.join(main, 'one.scss'); - var files = watcher.changed(file); - assert.deepEqual(files.added, [ - path.join(main, 'partials', '_one.scss'), - path.join(main, 'partials', '_three.scss'), - ]); - }); - - it('should record nothing as removed', function() { - var file = path.join(main, 'one.scss'); - var files = watcher.changed(file); - assert.deepEqual(files.removed, []); - }); }); }); describe('and is not in the graph', function() { describe('if it is a partial', function() { - it('should not record anything', function() { + it('should record nothing as changed', function() { var file = path.join(sibling, 'partials', '_three.scss'); var files = watcher.changed(file); - assert.deepEqual(files, { - added: [], - changed: [], - removed: [], - }); + assert.deepEqual(files, []); }); }); describe('if it is not a partial', function() { - it('should record itself as changed', function() { + it('should record nothing as changed', function() { var file = path.join(sibling, 'three.scss'); var files = watcher.changed(file); - assert.deepEqual(files, { - added: [], - changed: [file], - removed: [], - }); + assert.deepEqual(files, []); }); }); }); @@ -106,59 +69,38 @@ describe('watcher', function() { describe('when a file is added', function() { describe('and it is in the graph', function() { describe('if it is a partial', function() { - it('should record nothing as added', function() { - var file = path.join(main, 'partials', '_three.scss'); - var files = watcher.added(file); - assert.deepEqual(files.added, []); - }); - - it('should record its decendants as added', function() { + it('should record its ancestors as changed', function() { var file = path.join(main, 'partials', '_one.scss'); var files = watcher.added(file); - assert.deepEqual(files.added, [ - path.join(main, 'partials', '_three.scss') + assert.deepEqual(files, [ + path.join(main, 'one.scss') ]); }); - - it('should record nothing as changed', function() { - var file = path.join(main, 'partials', '_three.scss'); - var files = watcher.added(file); - assert.deepEqual(files.changed, []); - }); - - it('should record nothing as removed', function() { - var file = path.join(main, 'partials', '_three.scss'); - var files = watcher.added(file); - assert.deepEqual(files.removed, []); - }); }); describe('if it is not a partial', function() { - it('should record nothing as added', function() { - var file = path.join(main, 'three.scss'); - var files = watcher.added(file); - assert.deepEqual(files.added, []); - }); - - it('should record its decendants as added', function() { + it('should record nothing as changed', function() { var file = path.join(main, 'one.scss'); var files = watcher.added(file); - assert.deepEqual(files.added, [ - path.join(main, 'partials', '_one.scss'), - path.join(main, 'partials', '_three.scss'), - ]); + assert.deepEqual(files, []); }); + }); + }); + describe('and is not in the graph', function() { + describe('if it is a partial', function() { it('should record nothing as changed', function() { - var file = path.join(main, 'one.scss'); + var file = path.join(sibling, 'partials', '_three.scss'); var files = watcher.added(file); - assert.deepEqual(files.changed, []); + assert.deepEqual(files, []); }); + }); - it('should record nothing as removed', function() { - var file = path.join(main, 'one.scss'); + describe('if it is not a partial', function() { + it('should record nothing as changed', function() { + var file = path.join(sibling, 'three.scss'); var files = watcher.added(file); - assert.deepEqual(files.removed, []); + assert.deepEqual(files, []); }); }); }); @@ -167,44 +109,20 @@ describe('watcher', function() { describe('when a file is removed', function() { describe('and it is in the graph', function() { describe('if it is a partial', function() { - it('should record nothing as added', function() { - var file = path.join(main, 'partials', '_one.scss'); - var files = watcher.removed(file); - assert.deepEqual(files.added, []); - }); - it('should record its ancestors as changed', function() { var file = path.join(main, 'partials', '_one.scss'); var files = watcher.removed(file); - assert.deepEqual(files.changed, [ + assert.deepEqual(files, [ path.join(main, 'one.scss'), ]); }); - - it('should record itself as removed', function() { - var file = path.join(main, 'partials', '_one.scss'); - var files = watcher.removed(file); - assert.deepEqual(files.removed, [file]); - }); }); describe('if it is not a partial', function() { - it('should record nothing as added', function() { - var file = path.join(main, 'one.scss'); - var files = watcher.removed(file); - assert.deepEqual(files.added, []); - }); - it('should record nothing as changed', function() { var file = path.join(main, 'one.scss'); var files = watcher.removed(file); - assert.deepEqual(files.changed, []); - }); - - it('should record itself as removed', function() { - var file = path.join(main, 'one.scss'); - var files = watcher.removed(file); - assert.deepEqual(files.removed, [file]); + assert.deepEqual(files, []); }); }); }); @@ -214,11 +132,7 @@ describe('watcher', function() { it('should record nothing', function() { var file = path.join(sibling, 'partials', '_three.scss'); var files = watcher.removed(file); - assert.deepEqual(files, { - added: [], - changed: [], - removed: [], - }); + assert.deepEqual(files, []); }); }); @@ -226,11 +140,7 @@ describe('watcher', function() { it('should record nothing', function() { var file = path.join(sibling, 'three.scss'); var files = watcher.removed(file); - assert.deepEqual(files, { - added: [], - changed: [], - removed: [], - }); + assert.deepEqual(files, []); }); }); }); @@ -248,83 +158,38 @@ describe('watcher', function() { describe('when a file is changed', function() { describe('and it is in the graph', function() { describe('if it is a partial', function() { - it('should record its decendents as added', function() { - var file = path.join(main, 'partials', '_one.scss'); - var files = watcher.changed(file); - assert.deepEqual(files.added, [ - path.join(main, 'partials', '_three.scss'), - ]); - }); - it('should record its ancenstors as changed', function() { var file = path.join(main, 'partials', '_one.scss'); var files = watcher.changed(file); - assert.deepEqual(files.changed, [ + assert.deepEqual(files, [ path.join(main, 'one.scss'), ]); }); - - it('should record nothing as removed', function() { - var file = path.join(main, 'partials', '_one.scss'); - var files = watcher.changed(file); - assert.deepEqual(files.removed, []); - }); }); describe('if it is not a partial', function() { - it('should record its decendents as added', function() { - var file = path.join(main, 'one.scss'); - var files = watcher.changed(file); - assert.deepEqual(files.added, [ - path.join(main, 'partials', '_one.scss'), - path.join(main, 'partials', '_three.scss'), - ]); - }); - it('should record itself as changed', function() { var file = path.join(main, 'one.scss'); var files = watcher.changed(file); - assert.deepEqual(files.changed, [file]); - }); - - it('should record nothing as removed', function() { - var file = path.join(main, 'one.scss'); - var files = watcher.changed(file); - assert.deepEqual(files.removed, []); + assert.deepEqual(files, [file]); }); }); }); describe('and it is not in the graph', function() { describe('if it is a partial', function() { - it('should record nothing', function() { + it('should record nothing as changed', function() { var file = path.join(sibling, 'partials', '_three.scss'); var files = watcher.changed(file); - assert.deepEqual(files, { - added: [], - changed: [], - removed: [], - }); + assert.deepEqual(files, []); }); }); describe('if it is not a partial', function() { - it('should record nothing as added', function() { - var file = path.join(sibling, 'three.scss'); - var files = watcher.changed(file); - assert.deepEqual(files.added, []); - }); - it('should record itself as changed', function() { var file = path.join(sibling, 'three.scss'); var files = watcher.changed(file); - assert.deepEqual(files.changed, [file]); - }); - - it('should record nothing as removed', function() { - var file = path.join(sibling, 'three.scss'); - var files = watcher.changed(file); - assert.deepEqual(files.removed, []); + assert.deepEqual(files, []); }); }); }); @@ -332,30 +197,23 @@ describe('watcher', function() { describe('when a file is added', function() { describe('and it is in the graph', function() { - it('should record nothing as added', function() { - var file = path.join(main, 'partials', '_three.scss'); - var files = watcher.added(file); - assert.deepEqual(files.added, []); - }); - - it('should record its decendants as added', function() { - var file = path.join(main, 'partials', '_one.scss'); - var files = watcher.added(file); - assert.deepEqual(files.added, [ - path.join(main, 'partials', '_three.scss'), - ]); - }); - - it('should record nothing as changed', function() { - var file = path.join(main, 'partials', '_three.scss'); - var files = watcher.added(file); - assert.deepEqual(files.changed, []); + describe('if it is a partial', function() { + it('should record its ancestors as changed', function() { + var file = path.join(main, 'partials', '_one.scss'); + var files = watcher.added(file); + assert.deepEqual(files, [ + path.join(main, 'one.scss'), + ]); + }); }); - it('should record nothing as removed', function() { - var file = path.join(main, 'partials', '_three.scss'); - var files = watcher.added(file); - assert.deepEqual(files.removed, []); + /* testing that this "impossible" situation is handled gracefully */ + describe('if it is not a partial', function() { + it('should record nothing as changed', function() { + var file = path.join(main, 'one.scss'); + var files = watcher.added(file); + assert.deepEqual(files, []); + }); }); }); @@ -368,54 +226,18 @@ describe('watcher', function() { }); describe('if it is a partial', function() { - it('should record nothing as added', function() { - var file = path.join(main, 'partials', '_three.scss'); - var files = watcher.added(file); - assert.deepEqual(files.added, [ - file, - ]); - }); - - it('should not record its decendants as added', function() { - var file = path.join(main, 'partials', '_one.scss'); - var files = watcher.added(file); - assert.deepEqual(files.added, [ - file, - ]); - }); - it('should record nothing as changed', function() { var file = path.join(main, 'partials', '_three.scss'); var files = watcher.added(file); - assert.deepEqual(files.changed, []); - }); - - it('should record nothing as removed', function() { - var file = path.join(main, 'partials', '_three.scss'); - var files = watcher.added(file); - assert.deepEqual(files.removed, []); + assert.deepEqual(files, []); }); }); describe('if it is not a partial', function() { - it('should record itself as added', function() { - var file = path.join(main, 'three.scss'); - var files = watcher.added(file); - assert.deepEqual(files.added, [ - file, - ]); - }); - it('should record nothing as changed', function() { - var file = path.join(main, 'one.scss'); - var files = watcher.added(file); - assert.deepEqual(files.changed, []); - }); - - it('should record nothing as removed', function() { - var file = path.join(main, 'one.scss'); + var file = path.join(main, 'three.scss'); var files = watcher.added(file); - assert.deepEqual(files.removed, []); + assert.deepEqual(files, []); }); }); }); @@ -424,44 +246,20 @@ describe('watcher', function() { describe('when a file is removed', function() { describe('and it is in the graph', function() { describe('if it is a partial', function() { - it('should record nothing as added', function() { - var file = path.join(main, 'partials', '_one.scss'); - var files = watcher.removed(file); - assert.deepEqual(files.added, []); - }); - it('should record its ancestors as changed', function() { var file = path.join(main, 'partials', '_one.scss'); var files = watcher.removed(file); - assert.deepEqual(files.changed, [ + assert.deepEqual(files, [ path.join(main, 'one.scss'), ]); }); - - it('should record itself as removed', function() { - var file = path.join(main, 'partials', '_one.scss'); - var files = watcher.removed(file); - assert.deepEqual(files.removed, [file]); - }); }); describe('if it is not a partial', function() { - it('should record nothing as added', function() { - var file = path.join(main, 'one.scss'); - var files = watcher.removed(file); - assert.deepEqual(files.added, []); - }); - it('should record nothing as changed', function() { var file = path.join(main, 'one.scss'); var files = watcher.removed(file); - assert.deepEqual(files.changed, []); - }); - - it('should record itself as removed', function() { - var file = path.join(main, 'one.scss'); - var files = watcher.removed(file); - assert.deepEqual(files.removed, [file]); + assert.deepEqual(files, []); }); }); }); @@ -475,26 +273,18 @@ describe('watcher', function() { }); describe('if it is a partial', function() { - it('should record nothing as added', function() { + it('should record nothing as changed', function() { var file = path.join(main, 'partials', '_one.scss'); var files = watcher.removed(file); - assert.deepEqual(files, { - added: [], - changed: [], - removed: [], - }); + assert.deepEqual(files, []); }); }); describe('if it is not a partial', function() { - it('should record nothing', function() { + it('should record nothing as changed', function() { var file = path.join(main, 'one.scss'); var files = watcher.removed(file); - assert.deepEqual(files, { - added: [], - changed: [], - removed: [], - }); + assert.deepEqual(files, []); }); }); });