Created
April 1, 2014 20:30
-
-
Save thomasboyt/9922492 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* jshint node: true */ | |
/** | |
* This is a hack-ish way of triggering Karma runs on broccoli builds. | |
*/ | |
var path = require('path'); | |
var spawn = require('child_process').spawn; | |
var broccoli = require('broccoli'); | |
var util = require('broccoli/lib/util'); | |
var Watcher = broccoli.Watcher; | |
var runner = require('karma').runner; | |
function watch(builder, outputDir, options) { | |
options = options || {}; | |
var watcher = new Watcher(builder); | |
var currentCopy = null; | |
var didInitialBuild = false; | |
// We register these so the 'exit' handler removing temp dirs is called | |
process.on('SIGINT', function () { | |
process.exit(1); | |
}); | |
process.on('SIGTERM', function () { | |
process.exit(1); | |
}); | |
watcher.on('change', function(dir) { | |
// Ignore intiial build | |
if ( !didInitialBuild ) { | |
didInitialBuild = true; | |
return; | |
} | |
console.log('Rebuilt tree'); | |
// Don't allow two copies to happen simultaneously - chain off of existing copy | |
if (currentCopy) { | |
currentCopy = currentCopy.finally(function() { | |
currentCopy = cleanAndCopy(dir, outputDir); | |
}); | |
} else { | |
currentCopy = cleanAndCopy(dir, outputDir); | |
} | |
currentCopy.then(function() { | |
console.log('running karma'); | |
// empty function is passed so that it doesn't exit after running | |
runner.run({ | |
port: 9876, | |
silent: true, | |
configFile: path.resolve('karma.conf.js') | |
}, function() {}); | |
currentCopy = null; | |
}); | |
}); | |
watcher.on('error', function(err) { | |
console.log('Built with error:'); | |
// Should also show file and line/col if present; see cli.js | |
console.log(err.stack); | |
console.log(''); | |
}); | |
} | |
function cleanAndCopy (dir, outputDir) { | |
return util.rmDir(outputDir).then(function() { | |
return util.copyDir(dir, outputDir); | |
}).then(function() { | |
console.log('Built tree to', outputDir); | |
}, function(errors) { | |
if (errors[0].code !== 'ENOENT') { | |
// ENOENT is allowed, as it means that the temp dir was cleared before it had a chance to | |
// copy. | |
throw errors[0]; | |
} | |
}); | |
} | |
var tree = broccoli.loadBrocfile(); | |
watch(new broccoli.Builder(tree), 'out'); | |
var karma = spawn('karma', ['start']); | |
karma.stdout.on('data', function(data) { | |
process.stdout.write(data); | |
}); | |
karma.stderr.on('data', function(data) { | |
process.stdout.write(data); | |
}); | |
process.on('exit', function() { | |
karma.kill(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment