From d3d58d7505ecf15d137e279409e545fc022054c8 Mon Sep 17 00:00:00 2001 From: xzyfer Date: Mon, 16 Apr 2018 23:24:51 +1000 Subject: [PATCH] Compile on watch --- bin/node-sass | 52 +++++++++++++++++++++++++++++++++++++++------------ lib/render.js | 24 ++++++++++++++---------- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/bin/node-sass b/bin/node-sass index aef5a7e25..763fb496d 100755 --- a/bin/node-sass +++ b/bin/node-sass @@ -12,6 +12,7 @@ var Emitter = require('events').EventEmitter, watcher = require('../lib/watcher'), stdout = require('stdout-stream'), stdin = require('get-stdin'), + chalk = require('chalk'), fs = require('fs'); /** @@ -271,7 +272,11 @@ function watch(options, emitter) { var handler = function(files) { files.forEach(function(file) { if (path.basename(file)[0] !== '_') { - renderFile(file, options, emitter); + renderFile(file, options, emitter, function(err) { + if (err) { + emitter.emit('error', chalk.red(err)); + } + }); } }); }; @@ -344,12 +349,32 @@ function run(options, emitter) { } } - if (options.watch) { - watch(options, emitter); - } else if (options.directory) { - renderDir(options, emitter); + if (options.directory) { + renderDir(options, emitter, function(err) { + if (err) { + emitter.emit('error', chalk.red(err)); + return process.exit(1); + } + + if (options.watch) { + watch(options, emitter); + } else { + process.exit(); + } + }); } else { - render(options, emitter); + render(options, emitter, function(err) { + if (err) { + emitter.emit('error', chalk.red(err)); + return process.exit(1); + } + + if (options.watch) { + watch(options, emitter); + } else { + process.exit(); + } + }); } } @@ -361,12 +386,12 @@ function run(options, emitter) { * @param {Object} emitter * @api private */ -function renderFile(file, options, emitter) { +function renderFile(file, options, emitter, done) { options = getOptions([path.resolve(file)], options); if (options.watch && !options.quiet) { emitter.emit('info', util.format('=> changed: %s', file)); } - render(options, emitter); + render(options, emitter, done); } /** @@ -376,7 +401,7 @@ function renderFile(file, options, emitter) { * @param {Object} emitter * @api private */ -function renderDir(options, emitter) { +function renderDir(options, emitter, done) { var globPath = path.resolve(options.directory, globPattern(options)); glob(globPath, { ignore: '**/_*', follow: options.follow }, function(err, files) { if (err) { @@ -386,14 +411,17 @@ function renderDir(options, emitter) { } forEach(files, function(subject) { - emitter.once('done', this.async()); - renderFile(subject, options, emitter); + renderFile(subject, options, emitter, this.async()); }, function(successful, arr) { var outputDir = path.join(process.cwd(), options.output); if (!options.quiet) { emitter.emit('info', util.format('Wrote %s CSS files to %s', arr.length, outputDir)); } - process.exit(); + if (successful) { + done(); + } else { + done(new Error('Some files failed to compile')); + } }); }); } diff --git a/lib/render.js b/lib/render.js index 858e02e74..a44c7d9d6 100644 --- a/lib/render.js +++ b/lib/render.js @@ -16,7 +16,7 @@ var chalk = require('chalk'), * @api public */ -module.exports = function(options, emitter) { +module.exports = function(options, emitter, fin) { var renderOptions = { includePaths: options.includePath, omitSourceMapUrl: options.omitSourceMapUrl, @@ -50,7 +50,7 @@ module.exports = function(options, emitter) { var todo = 1; var done = function() { if (--todo <= 0) { - emitter.emit('done'); + fin(); } }; @@ -68,12 +68,14 @@ module.exports = function(options, emitter) { mkdirp(path.dirname(destination), function(err) { if (err) { - return emitter.emit('error', chalk.red(err)); + fin(err); + return; } fs.writeFile(destination, result.css.toString(), function(err) { if (err) { - return emitter.emit('error', chalk.red(err)); + fin(err); + return; } emitter.emit('info', chalk.green('Wrote CSS to ' + destination)); @@ -87,11 +89,13 @@ module.exports = function(options, emitter) { mkdirp(path.dirname(sourceMap), function(err) { if (err) { - return emitter.emit('error', chalk.red(err)); + fin(err); + return; } fs.writeFile(sourceMap, result.map, function(err) { if (err) { - return emitter.emit('error', chalk.red('Error' + err)); + fin(err); + return; } emitter.emit('info', chalk.green('Wrote Source Map to ' + sourceMap)); @@ -104,15 +108,15 @@ module.exports = function(options, emitter) { emitter.emit('render', result.css.toString()); }; - var error = function(error) { - emitter.emit('error', chalk.red(JSON.stringify(error, null, 2))); + var error = function(err) { + fin(JSON.stringify(err, null, 2)); + return; }; var renderCallback = function(err, result) { if (err) { error(err); - } - else { + } else { success(result); } };