Skip to content

Instantly share code, notes, and snippets.

@bijoutrouvaille
Created May 22, 2012 11:25
Show Gist options
  • Save bijoutrouvaille/2768461 to your computer and use it in GitHub Desktop.
Save bijoutrouvaille/2768461 to your computer and use it in GitHub Desktop.
jasmine-node autotest fix osx
var walkdir = require('walkdir');
var collection = require('./spec-collection');
var path = require('path');
var fs = require('fs');
var child_process = require('child_process');
var baseArgv = [];
for(var i = 0; i < process.argv.length; i++) {
if(process.argv[i] !== '--autotest') {
baseArgv.push(process.argv[i]);
}
}
var run_external = function(command, args, callback) {
var child = child_process.spawn(command, args);
child.stdout.on('data', function(data) {
process.stdout.write(data);
});
child.stderr.on('data', function(data) {
process.stderr.write(data);
});
if(typeof callback == 'function') {
child.on('exit', callback);
}
}
var run_everything = function() {
// run the suite when it starts
var argv = [].concat(baseArgv);
run_external(argv.shift(), argv, function() { console.log(arguments) });
}
var last_run_succesful = true;
var watches = {};
var watchFile = function(file, stat) {
var file = path.normalize(file)
var prevStats = stat;
var onChange = function(ev, f) {
if(!path.existsSync(file)) {
delete watches[file];
return;
}
var currStats = fs.statSync( file );
if(prevStats.mtime.getTime() != currStats.mtime.getTime()) {
prevStats = currStats;
// narrow down a pattern to reduce the specs runned
var match = path.basename(file, path.extname(file)) + ".*";
match = match.replace(new RegExp("spec", "i"), "");
// so we need to rerun the jasmine suite
var argv = [].concat(baseArgv, ["--match", match]);
run_external(argv.shift(), argv, function(code) {
// run everything if we fixed some bugs
if(code == 0) {
if(!last_run_succesful) {
run_everything();
}
last_run_succesful = true;
} else {
last_run_succesful = false;
}
});
}
};
watches[file] = onChange;
}
exports.start = function(loadpath, pattern) {
var finder = walkdir.find(loadpath);
finder.on('file', function(file, stat) {
var basename = path.basename(file);
if(pattern.test(basename)) {
watchFile(file,stat);
}
});
fs.watch (loadpath, function (ev,file) {
for (var i in watches) {
watches[i] (ev,i)
}
})
run_everything();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment