Last active
December 14, 2015 10:18
-
-
Save BoyCook/5070828 to your computer and use it in GitHub Desktop.
Node.js wrapper for starting web app and spawning multiple subprocesses
This file contains 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
var spawn = require('child_process').spawn; | |
var server = require('./server'); | |
var spawns = {}; | |
server.start({port: 8080}, function () { | |
createSpawn('jasmine-node', [ 'test/spec', '--junitreport', '--forceexit' ], logToConsole, logToConsole); | |
createSpawn('casperjs', [ 'test', 'test/ui' ], logToConsole, logToConsole); | |
}); | |
function createSpawn(name, args, stdout, stderr) { | |
spawns[name] = true; | |
var spawned = spawn(name, args); | |
spawned.stdout.on('data', stdout); | |
spawned.stderr.on('data', stderr); | |
spawned.on('exit', function (code) { | |
spawns[name] = false; | |
safeStop(code); | |
}); | |
} | |
// logs process stdout/stderr to the console | |
function logToFile(data) { | |
fs.appendFileSync(output, data, function (err) { | |
console.log('Error writing to file'); | |
if (err) throw err; | |
}); | |
} | |
function logToConsole(data) { | |
console.log(String(data)); | |
} | |
// Hack - Shutdown when all processes are done | |
function safeStop(code) { | |
var running = false; | |
for (var key in spawns) { | |
if (spawns[key] == true) { | |
running = true; | |
} | |
} | |
if (!running) { | |
server.shutDown(); | |
process.exit(code); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment