Skip to content

Instantly share code, notes, and snippets.

@pward123
Last active August 29, 2015 13:56
Show Gist options
  • Save pward123/9274325 to your computer and use it in GitHub Desktop.
Save pward123/9274325 to your computer and use it in GitHub Desktop.
Flambe: auto build/serve on .hx change
#!/usr/bin/env node
var spawn = require('child_process').spawn
,StringDecoder = require('string_decoder').StringDecoder
,build,serve
;
function logData() {
var i,len;
for (i = 0, len = arguments.length; i < len; i++) {
var decoder = new StringDecoder('utf8');
console.log(decoder.write(arguments[i]));
}
}
// When we're called from supervisor (see below),
// just spawn 'flambe run html --debug'
if ((process.argv.length === 3) && (process.argv[2] === '-supervisor')) {
spawn('flambe',['run','html','--debug']);
// Try to keep from exiting
process.stdin.resume();
} else {
// If we're not being called from supervisor, do an initial build
console.log('building...');
build = spawn('flambe', ['build','html','--debug'])
build.stdout.on('data', logData);
build.stderr.on('data', logData)
build.on('close', function (code) {
if (code !== 0) {
console.log('build process exited with code ' + code);
process.exit(code);
}
build = null;
// And when that finishes, launch the server
console.log('serving...')
serve = spawn('flambe', ['serve'])
serve.stdout.on('data', logData);
serve.stderr.on('data', logData)
serve.on('close', function(code) {
console.log('serve process exited with code ' + code);
process.exit(code);
serve = null;
})
// Now that we've launched the server, call supervisor
// with ourselves as the node file to run when things
// change
console.log('watching...')
supervisor = spawn('supervisor', ['-w','src','-e','hx','--','bin/start','-supervisor'])
supervisor.stdout.on('data', logData);
supervisor.stderr.on('data', logData)
});
process.on('SIGTERM', function() {
if (build) {
console.log('Killing flambe build');
build.kill();
}
if (serve) {
console.log('Killing flambe serve');
serve.kill();
}
process.exit();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment