Skip to content

Commit

Permalink
Watcher should track newly created files
Browse files Browse the repository at this point in the history
Currently newly created are added to the graph but not added the
watcher.

Fixes sass#1891
  • Loading branch information
xzyfer committed Mar 29, 2017
1 parent ae4f935 commit 2abc8e6
Showing 1 changed file with 41 additions and 38 deletions.
79 changes: 41 additions & 38 deletions bin/node-sass
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
});
}

Expand Down

0 comments on commit 2abc8e6

Please # to comment.