Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit fb5c2a4

Browse files
committed
fixed watch
1 parent dc237d9 commit fb5c2a4

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

bin/node-sass

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function getEmitter() {
139139
*/
140140

141141
function getOptions(args, options) {
142-
options.src = options.watch ? options.watch : args[0];
142+
options.src = args[0] || options.watch;
143143

144144
if (args[1]) {
145145
options.dest = path.resolve(process.cwd(), args[1]);
@@ -161,25 +161,30 @@ function getOptions(args, options) {
161161
*/
162162

163163
function watch(options, emitter) {
164-
var dir = options.watch;
165-
var gaze = new Gaze();
164+
var watchDir = isSassFile(options.watch) ? path.dirname(path.resolve(options.watch)) : options.watch,
165+
glob = options.recursive ? '**/*.{sass,scss}' : '*.{sass,scss}',
166+
gaze = new Gaze(),
167+
dir;
166168

167-
if (dir === true) {
169+
if (watchDir === true) {
168170
dir = [];
169-
} else if (!Array.isArray(dir)) {
170-
dir = [dir];
171+
} else if (!Array.isArray(watchDir)) {
172+
dir = [watchDir];
171173
}
172174

173-
dir.push(options.src);
175+
// only add the src file if it is not already
176+
// in the array as the watch path
177+
if (options.watch !== options.src) {
178+
dir.push(options.src);
179+
}
174180
dir = dir.map(function(d) {
175-
var glob = options.recursive ? '**/*.{sass,scss}' : '*.{sass,scss}';
176181
return isSassFile(d) ? d : path.join(d, glob);
177182
});
178183

179184
gaze.add(dir);
180185
gaze.on('error', emitter.emit.bind(emitter, 'error'));
181186

182-
var graph = grapher.parseDir(options.src, { loadPaths: options.includePath });
187+
var graph = grapher.parseDir(watchDir, { loadPaths: options.includePath });
183188

184189
gaze.on('changed', function(file) {
185190
var files = [file];

test/cli.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,29 @@ 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+
fs.unlinkSync(fixtures('watching/index.css'));
226+
done();
227+
});
228+
229+
setTimeout(function() {
230+
fs.appendFileSync(foo, 'body{background:white}');
231+
}, 500);
232+
});
210233
});
211234

212235
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)