update files

This commit is contained in:
nuintun
2016-01-14 09:33:15 +08:00
parent 47a857cd5b
commit bf76db01b9
3 changed files with 175 additions and 45 deletions

View File

@@ -4,7 +4,8 @@
'use strict';
var spawn = require('child_process').spawn;
var spawn = require('./spawn');
var threadKill = require('./thread-kill');
/**
* Emulator
@@ -17,7 +18,7 @@ function Emulator(task){
Emulator.prototype = {
start: function (){
this.thread = this.exec(this.task.command, {
this.thread = spawn(this.task.command, {
env: this.task.env,
cwd: this.task.cwd
});
@@ -26,53 +27,17 @@ Emulator.prototype = {
},
stop: function (){
if (this.thread) {
this.exec('taskkill /t /f /pid ' + this.thread.pid);
var context = this;
['stdin', 'stdout', 'stderr'].forEach(function (stream){
this.thread[stream].removeAllListeners();
}, this);
threadKill(this.thread.pid, function (){
['stdin', 'stdout', 'stderr'].forEach(function (stream){
context.thread[stream].removeAllListeners();
});
this.thread = null;
context.thread = null;
});
}
},
exec: function (){
var parsed = normalizeExecArgs.apply(null, arguments);
return spawn(parsed.shell, parsed.args, parsed.options);
}
};
/**
* normalize exec args
* @param command
* @param options
* @returns {{cmd: *, shell: *, args: *, options: *}}
*/
function normalizeExecArgs(command, options){
var shell, args;
// Make a shallow copy before patching so we don't clobber the user's
// options object.
options = options || {};
if (process.platform === 'win32') {
shell = process.env.comspec || 'cmd.exe';
args = ['/s', '/c', '"' + command + '"'];
options.windowsVerbatimArguments = true;
} else {
shell = '/bin/sh';
args = ['-c', command];
}
if (options.shell) {
shell = options.shell;
}
return {
shell: shell,
args: args,
options: options
};
}
module.exports = Emulator;