Skip to content

Commit 4ce47c4

Browse files
committed
Watcher should track newly created files
Currently newly created are added to the graph but not added the watcher. Fixes sass#1891
1 parent cc7c3b3 commit 4ce47c4

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

bin/node-sass

+41-38
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,6 @@ function getOptions(args, options) {
229229
*/
230230

231231
function watch(options, emitter) {
232-
var buildGraph = function(options) {
233-
var graph;
234-
var graphOptions = {
235-
loadPaths: options.includePath,
236-
extensions: ['scss', 'sass', 'css']
237-
};
238-
239-
if (options.directory) {
240-
graph = grapher.parseDir(options.directory, graphOptions);
241-
} else {
242-
graph = grapher.parseFile(options.src, graphOptions);
243-
}
244-
245-
return graph;
246-
};
247-
248232
var watch = [];
249233
var graph = buildGraph(options);
250234

@@ -258,34 +242,53 @@ function watch(options, emitter) {
258242
gaze.on('error', emitter.emit.bind(emitter, 'error'));
259243

260244
gaze.on('changed', function(file) {
261-
var files = [file];
245+
updateWatcher(file, graph, gaze, options);
246+
});
247+
gaze.on('added', function(file) {
248+
updateWatcher(file, graph, gaze, options);
249+
});
262250

263-
// descendents may be added, so we need a new graph
251+
gaze.on('deleted', function() {
264252
graph = buildGraph(options);
265-
graph.visitAncestors(file, function(parent) {
266-
files.push(parent);
267-
});
268-
269-
// Add children to watcher
270-
graph.visitDescendents(file, function(child) {
271-
if (watch.indexOf(child) === -1) {
272-
watch.push(child);
273-
gaze.add(child);
274-
}
275-
});
276-
files.forEach(function(file) {
277-
if (path.basename(file)[0] !== '_') {
278-
renderFile(file, options, emitter);
279-
}
280-
});
281253
});
254+
}
282255

283-
gaze.on('added', function() {
284-
graph = buildGraph(options);
256+
function buildGraph (options) {
257+
var graph;
258+
var graphOptions = {
259+
loadPaths: options.includePath,
260+
extensions: ['scss', 'sass', 'css']
261+
};
262+
263+
if (options.directory) {
264+
graph = grapher.parseDir(options.directory, graphOptions);
265+
} else {
266+
graph = grapher.parseFile(options.src, graphOptions);
267+
}
268+
269+
return graph;
270+
}
271+
272+
function updateWatcher(file, graph, gaze) {
273+
var files = [file];
274+
275+
// descendents may be added, so we need a new graph
276+
graph = buildGraph(options);
277+
graph.visitAncestors(file, function(parent) {
278+
files.push(parent);
285279
});
286280

287-
gaze.on('deleted', function() {
288-
graph = buildGraph(options);
281+
// Add children to watcher
282+
graph.visitDescendents(file, function(child) {
283+
if (watch.indexOf(child) === -1) {
284+
watch.push(child);
285+
gaze.add(child);
286+
}
287+
});
288+
files.forEach(function(file) {
289+
if (path.basename(file)[0] !== '_') {
290+
renderFile(file, options, emitter);
291+
}
289292
});
290293
}
291294

0 commit comments

Comments
 (0)