Skip to content

Commit

Permalink
Added: --watch all .js in the CWD. Closes #139
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jan 6, 2012
1 parent 2a90cd4 commit 0b88e4b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ non-tty:
@cat /tmp/spec.out

watch:
watch --interval=1 $(MAKE) mocha.{js,css}
@watch -q $(MAKE) mocha.{js,css}

tm:
mkdir -p $(TM_DEST)/$(TM_BUNDLE)
Expand Down
3 changes: 2 additions & 1 deletion bin/_mocha
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var program = require('commander')
, exec = require('child_process').exec
, path = require('path')
, mocha = require('../')
, utils = mocha.utils
, reporters = mocha.reporters
, interfaces = mocha.interfaces
, Runner = mocha.Runner
Expand Down Expand Up @@ -195,7 +196,7 @@ if (program.watch) {

play(frames);

mocha.watch(files, function(){
utils.watch(utils.files(cwd), function(){
stop()
suite = suite.clone();
ui = interfaces[program.ui](suite);
Expand Down
1 change: 0 additions & 1 deletion lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ exports.Runner = require('./runner');
exports.Suite = require('./suite');
exports.Hook = require('./hook');
exports.Test = require('./test');
exports.watch = require('./watch');
66 changes: 66 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@

/**
* Module dependencies.
*/

var fs = require('fs')
, path = require('path')
, join = path.join
, debug = require('debug')('watch');

/**
* Ignored directories.
*/

var ignore = ['node_modules', '.git'];

/**
* Escape special characters in the given string of html.
*
Expand Down Expand Up @@ -107,3 +122,54 @@ exports.keys = Object.keys || function(obj) {

return keys;
};

/**
* Watch the given `files` for changes
* and invoke `fn(file)` on modification.
*
* @param {Array} files
* @param {Function} fn
* @api private
*/

exports.watch = function(files, fn){
var options = { interval: 100 };
files.forEach(function(file){
debug('file %s', file);
fs.watchFile(file, options, function(curr, prev){
if (prev.mtime < curr.mtime) fn(file);
});
});
};

/**
* Ignored files.
*/

function ignored(path){
return !~ignore.indexOf(path);
}

/**
* Lookup files in the given `dir`.
*
* @return {Array}
* @api public
*/

exports.files = function(dir, ret){
ret = ret || [];

fs.readdirSync(dir)
.filter(ignored)
.forEach(function(path){
path = join(dir, path);
if (fs.statSync(path).isDirectory()) {
exports.files(path, ret);
} else if (path.match(/\.js$/)) {
ret.push(path);
}
});

return ret;
};
17 changes: 0 additions & 17 deletions lib/watch.js

This file was deleted.

0 comments on commit 0b88e4b

Please sign in to comment.