Skip to content

Commit

Permalink
Merge 58f69a6 into a4be152
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva committed Nov 5, 2014
2 parents a4be152 + 58f69a6 commit 86be1f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 44 deletions.
77 changes: 34 additions & 43 deletions bin/node-sass
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node
var Emitter = require('events').EventEmitter,
path = require('path'),
Gaze = require('gaze'),
meow = require('meow'),
stdin = require('get-stdin'),
watch = require('node-watch'),
render = require('../lib/render');

/**
Expand Down Expand Up @@ -40,7 +40,6 @@ var cli = meow({
'indented-syntax',
'omit-source-map-url',
'stdout',
'watch',
'source-comments'
],
string: [
Expand All @@ -65,27 +64,6 @@ var cli = meow({
}
});

/**
* Throttle
*
* @param {Function} fn
* @api private
*/

function throttle(fn) {
var timer;
var args = [].slice.call(arguments, 1);

return function() {
var self = this;
clearTimeout(timer);

timer = setTimeout(function() {
fn.apply(self, args);
}, 20);
};
}

/**
* Check if file is a Sass file
*
Expand Down Expand Up @@ -147,6 +125,38 @@ function getOptions(args, options) {
return options;
}

/**
* Watch
*
* @param {Object} options
* @param {Object} emitter
* @api private
*/

function watch(options, emitter) {
var dir = options.watch;
var gaze = new Gaze();

if (dir === true) {
dir = [];
} else if (!Array.isArray(dir)) {
dir = [dir];
}

dir.push(options.src);
dir = dir.map(function(d) {
return isSassFile(d) ? d : path.join(d, '*.{sass,scss}');
});

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

gaze.on('changed', function(file) {
emitter.emit('warn', '=> changed: ' + file);
render(options, emitter);
});
}

/**
* Run
*
Expand All @@ -169,26 +179,7 @@ function run(options, emitter) {
}

if (options.watch) {
var throttledRender = throttle(render, options, emitter);
var watchDir = options.watch;

if (watchDir === true) {
watchDir = [];
} else if (!Array.isArray(watchDir)) {
watchDir = [watchDir];
}

watchDir.push(options.src);

watch(watchDir, function(file) {
emitter.emit('warn', '=> changed: '.grey + file.blue);

if (isSassFile(file)) {
throttledRender();
}
});

throttledRender();
watch(options, emitter);
} else {
render(options, emitter);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
"cross-spawn": "^0.2.3",
"download": "^3.1.2",
"download-status": "^2.1.0",
"gaze": "^0.6.4",
"get-stdin": "^3.0.0",
"meow": "^2.0.0",
"mkdirp": "^0.5.0",
"mocha": "^2.0.1",
"nan": "^1.3.0",
"node-watch": "^0.3.4",
"object-assign": "^1.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit 86be1f5

Please sign in to comment.