-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a spawn method for logging stdout and stderr. #2
base: master
Are you sure you want to change the base?
Changes from 6 commits
09a7158
f5cfb2e
28516cc
96e0dcd
e8a16bb
e88af72
6e235bf
0cdee59
abd5ce7
0306d0e
2bf17b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
var _ = require('lodash'); | ||
var exec = require('child_process').exec; | ||
var spawn = require('child_process').spawn; | ||
var Deferred = require('promised-io/promise').Deferred; | ||
|
||
var Drush = { | ||
execOptions: { | ||
maxBuffer: 256 * 1024 * 1024 | ||
log: false | ||
} | ||
}; | ||
|
||
|
@@ -53,10 +54,12 @@ Drush.version = function () { | |
/** | ||
* Execute a drush command. | ||
* | ||
* @param {string} command | ||
* The drush command to execute. | ||
* @param {string|array} args | ||
* The drush command to execute either as a string or an array of arguments, | ||
* e.g. 'cc drush' or ['cc', 'drush']. | ||
* @param {object} options | ||
* A hash of options to add to the command, can contain: | ||
* - log: flag to log the output of the drush command. | ||
* - alias: the drush alias, e.g. "@self" to execute the command with. | ||
* - simulate: boolean, simulates all relevant actions. | ||
* - uri: the URI of the drupal site to use. | ||
|
@@ -65,40 +68,78 @@ Drush.version = function () { | |
* | ||
* @return Promise | ||
*/ | ||
Drush.exec = function (command, options) { | ||
Drush.exec = function (args, options) { | ||
options = options || {}; | ||
if (typeof args === 'string') { | ||
args = args.split(' '); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want to use a library, like yargs to parse this out since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hooray for those looking the same... the point is different whitespace would render different results. Also something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good point. I'm fixing this now. |
||
} | ||
args = args || []; | ||
var log = typeof options.log !== 'undefined' ? options.log : this.execOptions.log; | ||
var def = new Deferred(); | ||
var cmd = Drush.command; | ||
var prop = ''; | ||
var output = ''; | ||
|
||
// Prepend the alias argument. | ||
if (options.alias) { | ||
cmd += ' ' + options.alias; | ||
args = [options.alias].concat(args); | ||
} | ||
|
||
// Add simulate flag. | ||
if (options.simulate) { | ||
cmd += ' -s'; | ||
args.push('-s'); | ||
} | ||
|
||
// Add uri arguments. | ||
if (options.uri) { | ||
cmd += ' -l ' + options.uri; | ||
args.push('-l'); | ||
args.push(options.uri); | ||
} | ||
|
||
cmd += ' ' + command + ' -y'; | ||
// Add -y flag to prevent prompts from hanging. | ||
args.push('-y'); | ||
|
||
if (options.echo) { | ||
cmd = 'echo ' + options.echo + ' | ' + cmd; | ||
} | ||
// Initialize drush child process. | ||
var drush = spawn(Drush.command, args); | ||
|
||
// Handle echo and cat options. | ||
if (options.echo || options.cat) { | ||
var command = options.echo ? 'echo' : 'cat'; | ||
var cmd = spawn(command, options[command].split(' ')); | ||
|
||
if (options.cat) { | ||
cmd = 'cat ' + options.cat + ' | ' + cmd; | ||
cmd.stdout.on('data', function (data) { | ||
drush.stdin.write(data); | ||
}); | ||
cmd.stderr.on('data', function (data) { | ||
if (log) { | ||
console.log('' + data); | ||
} | ||
}); | ||
cmd.on('close', function (code) { | ||
if (code !== 0) { | ||
return def.reject(command + ' process exited with code ' + code); | ||
} | ||
drush.stdin.end(); | ||
}); | ||
} | ||
|
||
exec(cmd, this.execOptions, function (err, stdout, stderr) { | ||
if (err) { | ||
return def.reject(err); | ||
// Listen to stdout and stderr streams and resolve the promise when the drush | ||
// process closes. | ||
drush.stdout.on('data', function (data) { | ||
output += data; | ||
if (log) { | ||
console.log('' + data); | ||
} | ||
|
||
def.resolve(stdout); | ||
}); | ||
drush.stderr.on('data', function (data) { | ||
output += data; | ||
if (log) { | ||
console.log('' + data); | ||
} | ||
}); | ||
drush.on('close', function (code) { | ||
if (code !== 0) { | ||
return def.reject('drush process exited with code ' + code); | ||
} | ||
def.resolve(output); | ||
}); | ||
|
||
return def.promise; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,4 +49,3 @@ exports['drush'] = { | |
}); | ||
} | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/\]//
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, how did I not catch that. Sorry!