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

Commit b367965

Browse files
committed
Compile on watch
1 parent c93e428 commit b367965

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

bin/node-sass

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var Emitter = require('events').EventEmitter,
1212
watcher = require('../lib/watcher'),
1313
stdout = require('stdout-stream'),
1414
stdin = require('get-stdin'),
15+
chalk = require('chalk'),
1516
fs = require('fs');
1617

1718
/**
@@ -271,7 +272,11 @@ function watch(options, emitter) {
271272
var handler = function(files) {
272273
files.forEach(function(file) {
273274
if (path.basename(file)[0] !== '_') {
274-
renderFile(file, options, emitter);
275+
renderFile(file, options, emitter, function(err) {
276+
if (err) {
277+
emitter.emit('error', chalk.red(err));
278+
}
279+
});
275280
}
276281
});
277282
};
@@ -344,12 +349,32 @@ function run(options, emitter) {
344349
}
345350
}
346351

347-
if (options.watch) {
348-
watch(options, emitter);
349-
} else if (options.directory) {
350-
renderDir(options, emitter);
352+
if (options.directory) {
353+
renderDir(options, emitter, function(err) {
354+
if (err) {
355+
emitter.emit('error', chalk.red(err));
356+
return process.exit(1);
357+
}
358+
359+
if (options.watch) {
360+
watch(options, emitter);
361+
} else {
362+
process.exit();
363+
}
364+
});
351365
} else {
352-
render(options, emitter);
366+
render(options, emitter, function(err) {
367+
if (err) {
368+
emitter.emit('error', chalk.red(err));
369+
return process.exit(1);
370+
}
371+
372+
if (options.watch) {
373+
watch(options, emitter);
374+
} else {
375+
process.exit();
376+
}
377+
});
353378
}
354379
}
355380

@@ -361,12 +386,12 @@ function run(options, emitter) {
361386
* @param {Object} emitter
362387
* @api private
363388
*/
364-
function renderFile(file, options, emitter) {
389+
function renderFile(file, options, emitter, done) {
365390
options = getOptions([path.resolve(file)], options);
366391
if (options.watch && !options.quiet) {
367392
emitter.emit('info', util.format('=> changed: %s', file));
368393
}
369-
render(options, emitter);
394+
render(options, emitter, done);
370395
}
371396

372397
/**
@@ -376,7 +401,7 @@ function renderFile(file, options, emitter) {
376401
* @param {Object} emitter
377402
* @api private
378403
*/
379-
function renderDir(options, emitter) {
404+
function renderDir(options, emitter, done) {
380405
var globPath = path.resolve(options.directory, globPattern(options));
381406
glob(globPath, { ignore: '**/_*', follow: options.follow }, function(err, files) {
382407
if (err) {
@@ -386,14 +411,17 @@ function renderDir(options, emitter) {
386411
}
387412

388413
forEach(files, function(subject) {
389-
emitter.once('done', this.async());
390-
renderFile(subject, options, emitter);
414+
renderFile(subject, options, emitter, this.async());
391415
}, function(successful, arr) {
392416
var outputDir = path.join(process.cwd(), options.output);
393417
if (!options.quiet) {
394418
emitter.emit('info', util.format('Wrote %s CSS files to %s', arr.length, outputDir));
395419
}
396-
process.exit();
420+
if (successful) {
421+
done();
422+
} else {
423+
done(new Error('Some files failed to compile'));
424+
}
397425
});
398426
});
399427
}

lib/render.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var chalk = require('chalk'),
1616
* @api public
1717
*/
1818

19-
module.exports = function(options, emitter) {
19+
module.exports = function(options, emitter, fin) {
2020
var renderOptions = {
2121
includePaths: options.includePath,
2222
omitSourceMapUrl: options.omitSourceMapUrl,
@@ -50,7 +50,7 @@ module.exports = function(options, emitter) {
5050
var todo = 1;
5151
var done = function() {
5252
if (--todo <= 0) {
53-
emitter.emit('done');
53+
fin();
5454
}
5555
};
5656

@@ -68,12 +68,14 @@ module.exports = function(options, emitter) {
6868

6969
mkdirp(path.dirname(destination), function(err) {
7070
if (err) {
71-
return emitter.emit('error', chalk.red(err));
71+
fin(err);
72+
return;
7273
}
7374

7475
fs.writeFile(destination, result.css.toString(), function(err) {
7576
if (err) {
76-
return emitter.emit('error', chalk.red(err));
77+
fin(err);
78+
return;
7779
}
7880

7981
emitter.emit('info', chalk.green('Wrote CSS to ' + destination));
@@ -87,11 +89,13 @@ module.exports = function(options, emitter) {
8789

8890
mkdirp(path.dirname(sourceMap), function(err) {
8991
if (err) {
90-
return emitter.emit('error', chalk.red(err));
92+
fin(err);
93+
return;
9194
}
9295
fs.writeFile(sourceMap, result.map, function(err) {
9396
if (err) {
94-
return emitter.emit('error', chalk.red('Error' + err));
97+
fin(err);
98+
return;
9599
}
96100

97101
emitter.emit('info', chalk.green('Wrote Source Map to ' + sourceMap));
@@ -104,15 +108,15 @@ module.exports = function(options, emitter) {
104108
emitter.emit('render', result.css.toString());
105109
};
106110

107-
var error = function(error) {
108-
emitter.emit('error', chalk.red(JSON.stringify(error, null, 2)));
111+
var error = function(err) {
112+
fin(JSON.stringify(err, null, 2));
113+
return;
109114
};
110115

111116
var renderCallback = function(err, result) {
112117
if (err) {
113118
error(err);
114-
}
115-
else {
119+
} else {
116120
success(result);
117121
}
118122
};

0 commit comments

Comments
 (0)