Created
August 20, 2013 23:49
-
-
Save polastre/6288814 to your computer and use it in GitHub Desktop.
A test snipped of launching sails using spawn, then killing it and waiting for the 'exit event.
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 wrench = require('wrench'); | |
var sailsBin = './node_modules/sails/bin/sails.js'; | |
var _kill = function(sailsServer, cb) { | |
sailsServer.on('exit', function(code, signal){ | |
cb(); | |
}); | |
sailsServer.kill('SIGINT'); | |
}; | |
var _spawn = function(cb) { | |
// clean out the database directories | |
wrench.rmdirSyncRecursive('./.tmp', true); | |
var sailsServer = spawn(sailsBin, ['lift']); | |
var lifted = false; | |
var dataString = ''; | |
sailsServer.stdout.on('data', function(data) { | |
if (lifted) { return; } | |
dataString = dataString + data; | |
// If the server lifted, it passed | |
if (dataString.indexOf('Server lifted') !== -1) { | |
lifted = true; | |
return cb(sailsServer, dataString); | |
} | |
// Otherwise check for an error message | |
if (dataString.toLowerCase().indexOf('error') !== -1) { | |
lifted = true; | |
kill(sailsServer, function() { | |
return cb(null, dataString); | |
}); | |
} | |
}); | |
}; | |
_spawn(function (sailsServer, data) { | |
console.log('Sails launched'); | |
console.log(data); | |
_kill(sailsServer, function() { | |
console.log('Process finished.'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment