From 4ce47c42298da3a51f13879e21c122f0eb02cdf5 Mon Sep 17 00:00:00 2001 From: xzyfer Date: Mon, 13 Feb 2017 16:33:34 +1100 Subject: [PATCH] Watcher should track newly created files Currently newly created are added to the graph but not added the watcher. Fixes #1891 --- bin/node-sass | 79 ++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/bin/node-sass b/bin/node-sass index e94c12c77..e494fe770 100755 --- a/bin/node-sass +++ b/bin/node-sass @@ -229,22 +229,6 @@ function getOptions(args, options) { */ function watch(options, emitter) { - var buildGraph = function(options) { - var graph; - var graphOptions = { - loadPaths: options.includePath, - extensions: ['scss', 'sass', 'css'] - }; - - if (options.directory) { - graph = grapher.parseDir(options.directory, graphOptions); - } else { - graph = grapher.parseFile(options.src, graphOptions); - } - - return graph; - }; - var watch = []; var graph = buildGraph(options); @@ -258,34 +242,53 @@ function watch(options, emitter) { gaze.on('error', emitter.emit.bind(emitter, 'error')); gaze.on('changed', function(file) { - var files = [file]; + updateWatcher(file, graph, gaze, options); + }); + gaze.on('added', function(file) { + updateWatcher(file, graph, gaze, options); + }); - // descendents may be added, so we need a new graph + gaze.on('deleted', function() { graph = buildGraph(options); - graph.visitAncestors(file, function(parent) { - files.push(parent); - }); - - // Add children to watcher - graph.visitDescendents(file, function(child) { - if (watch.indexOf(child) === -1) { - watch.push(child); - gaze.add(child); - } - }); - files.forEach(function(file) { - if (path.basename(file)[0] !== '_') { - renderFile(file, options, emitter); - } - }); }); +} - gaze.on('added', function() { - graph = buildGraph(options); +function buildGraph (options) { + var graph; + var graphOptions = { + loadPaths: options.includePath, + extensions: ['scss', 'sass', 'css'] + }; + + if (options.directory) { + graph = grapher.parseDir(options.directory, graphOptions); + } else { + graph = grapher.parseFile(options.src, graphOptions); + } + + return graph; +} + +function updateWatcher(file, graph, gaze) { + var files = [file]; + + // descendents may be added, so we need a new graph + graph = buildGraph(options); + graph.visitAncestors(file, function(parent) { + files.push(parent); }); - gaze.on('deleted', function() { - graph = buildGraph(options); + // Add children to watcher + graph.visitDescendents(file, function(child) { + if (watch.indexOf(child) === -1) { + watch.push(child); + gaze.add(child); + } + }); + files.forEach(function(file) { + if (path.basename(file)[0] !== '_') { + renderFile(file, options, emitter); + } }); }