Skip to content

Commit 33b0b60

Browse files
committed
Merge pull request sass#782 from wesleytodd/watch-fix
Fixed watch behavior
2 parents dc237d9 + 4ed6de2 commit 33b0b60

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

bin/node-sass

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ var cli = meow({
5353
'recursive',
5454
'source-map-embed',
5555
'source-map-contents',
56-
'source-comments'
56+
'source-comments',
57+
'watch'
5758
],
5859
string: [
5960
'functions',
@@ -64,8 +65,7 @@ var cli = meow({
6465
'output',
6566
'output-style',
6667
'precision',
67-
'source-map-root',
68-
'watch'
68+
'source-map-root'
6969
],
7070
alias: {
7171
c: 'source-comments',
@@ -82,7 +82,8 @@ var cli = meow({
8282
'indent-width': 2,
8383
linefeed: 'lf',
8484
'output-style': 'nested',
85-
precision: 5
85+
precision: 5,
86+
recursive: true
8687
}
8788
});
8889

@@ -139,7 +140,7 @@ function getEmitter() {
139140
*/
140141

141142
function getOptions(args, options) {
142-
options.src = options.watch ? options.watch : args[0];
143+
options.src = args[0];
143144

144145
if (args[1]) {
145146
options.dest = path.resolve(process.cwd(), args[1]);
@@ -161,26 +162,20 @@ function getOptions(args, options) {
161162
*/
162163

163164
function watch(options, emitter) {
164-
var dir = options.watch;
165-
var gaze = new Gaze();
166-
167-
if (dir === true) {
168-
dir = [];
169-
} else if (!Array.isArray(dir)) {
170-
dir = [dir];
165+
var glob = options.recursive ? '**/*.{sass,scss}' : '*.{sass,scss}';
166+
var src = isSassFile(options.src) ? options.src : path.join(options.src, glob);
167+
var graph = grapher.parseDir(path.resolve(path.dirname(src)), { loadPaths: options.includePath });
168+
var watch = [];
169+
170+
// Add all files to watch list
171+
for (var i in graph.index) {
172+
watch.push(i);
171173
}
172174

173-
dir.push(options.src);
174-
dir = dir.map(function(d) {
175-
var glob = options.recursive ? '**/*.{sass,scss}' : '*.{sass,scss}';
176-
return isSassFile(d) ? d : path.join(d, glob);
177-
});
178-
179-
gaze.add(dir);
175+
var gaze = new Gaze();
176+
gaze.add(watch);
180177
gaze.on('error', emitter.emit.bind(emitter, 'error'));
181178

182-
var graph = grapher.parseDir(options.src, { loadPaths: options.includePath });
183-
184179
gaze.on('changed', function(file) {
185180
var files = [file];
186181
graph.visitAncestors(file, function(parent) {

test/cli.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,28 @@ describe('cli', function() {
207207
fs.appendFileSync(src, 'body{background:white}');
208208
}, 500);
209209
});
210+
211+
it('should watch the full sass dep tree for a single file', function(done) {
212+
var src = fixture('watching/index.scss');
213+
var foo = fixture('watching/foo.scss');
214+
215+
fs.writeFileSync(foo, '');
216+
217+
var bin = spawn(cli, [
218+
'--output-style', 'compressed',
219+
'--watch', src
220+
]);
221+
222+
bin.stdout.setEncoding('utf8');
223+
bin.stdout.once('data', function(data) {
224+
assert(data.trim() === 'body{background:white}');
225+
done();
226+
});
227+
228+
setTimeout(function() {
229+
fs.appendFileSync(foo, 'body{background:white}');
230+
}, 500);
231+
});
210232
});
211233

212234
describe('node-sass in.scss --output out.css', function() {

test/fixtures/watching/foo.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body{background:white}

test/fixtures/watching/index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import './foo';

0 commit comments

Comments
 (0)