Skip to content

Instantly share code, notes, and snippets.

@polastre
Created August 20, 2013 23:49
Show Gist options
  • Save polastre/6288814 to your computer and use it in GitHub Desktop.
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.
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