forked from sass/node-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple the graph and render logic from the fs watcher
This logic is all tightly coupled in the bin. This logic has been notoriously impossible to test due to weird fs timing issues. By extracting the actual logic we're now able to test it in isolation. With this in place replacing Gaze (sass#636) becomes a viable option. This PR not only massively increases our test coverage for the watcher but also address a bunch of known edge cases i.e. orphaned imports when a files is deleted. Closes sass#1896 Fixes sass#1891
- Loading branch information
Showing
11 changed files
with
328 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
var grapher = require('sass-graph'), | ||
clonedeep = require('lodash.clonedeep'), | ||
watcher = { | ||
opts: {} | ||
}, | ||
graph = null; | ||
|
||
watcher.reset = function(opts) { | ||
this.opts = clonedeep(opts || this.opts || {}); | ||
var options = { | ||
loadPaths: this.opts.includePath, | ||
extensions: ['scss', 'sass', 'css'] | ||
}; | ||
|
||
if (this.opts.directory) { | ||
graph = grapher.parseDir(this.opts.directory, options); | ||
} else { | ||
graph = grapher.parseFile(this.opts.src, options); | ||
} | ||
|
||
return Object.keys(graph.index); | ||
}; | ||
|
||
watcher.changed = function(absolutePath) { | ||
var files = { | ||
added: [], | ||
changed: [], | ||
removed: [], | ||
}; | ||
|
||
this.reset(); | ||
|
||
graph.visitAncestors(absolutePath, function(parent) { | ||
files.changed.push(parent); | ||
}); | ||
|
||
graph.visitDescendents(absolutePath, function(child) { | ||
files.added.push(child); | ||
}); | ||
|
||
return files; | ||
}; | ||
|
||
watcher.added = function(absolutePath) { | ||
var files = { | ||
added: [], | ||
changed: [], | ||
removed: [], | ||
}; | ||
|
||
this.reset(); | ||
|
||
if (Object.keys(graph.index).indexOf(absolutePath) !== -1) { | ||
files.added.push(absolutePath); | ||
} | ||
|
||
graph.visitDescendents(absolutePath, function(child) { | ||
files.added.push(child); | ||
}); | ||
|
||
return files; | ||
}; | ||
|
||
watcher.removed = function(absolutePath) { | ||
var files = { | ||
added: [], | ||
changed: [], | ||
removed: [], | ||
}; | ||
|
||
graph.visitAncestors(absolutePath, function(parent) { | ||
files.changed.push(parent); | ||
}); | ||
|
||
if (Object.keys(graph.index).indexOf(absolutePath) !== -1) { | ||
files.removed.push(absolutePath); | ||
} | ||
|
||
this.reset(); | ||
|
||
return files; | ||
}; | ||
|
||
module.exports = watcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@import "partials/one"; | ||
|
||
.one { | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.one { | ||
color: darkred; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.three { | ||
color: darkgreen; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.two { | ||
color: darkblue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@import "partials/two"; | ||
|
||
.two { | ||
color: blue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.three { | ||
color: darkgreen; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@import "partials/three"; | ||
|
||
.three { | ||
color: green; | ||
} |
Oops, something went wrong.