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

Commit

Permalink
Compile on watch
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyfer committed Jul 16, 2018
1 parent c93e428 commit b367965
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
52 changes: 40 additions & 12 deletions bin/node-sass
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
Expand Down Expand Up @@ -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));
}
});
}
});
};
Expand Down Expand Up @@ -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();
}
});
}
}

Expand All @@ -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);
}

/**
Expand All @@ -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) {
Expand All @@ -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'));
}
});
});
}
Expand Down
24 changes: 14 additions & 10 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -50,7 +50,7 @@ module.exports = function(options, emitter) {
var todo = 1;
var done = function() {
if (--todo <= 0) {
emitter.emit('done');
fin();
}
};

Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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);
}
};
Expand Down

0 comments on commit b367965

Please sign in to comment.