使用 spawnSync 流式传输子进程的输出,但将输出保存到父变量
Use spawnSync to stream output from child process but save the output to parent variable
我希望能够在子进程 运行 中显示命令输出,然后在我的程序中处理输出。这可能吗?
var result = spawnSync('neo', ['-help'], {
stdio: 'inherit',
encoding: 'utf-8'
});
var savedOutput = result.stdout;
console.log(String(savedOutput));
这就是我最后做的事情。
我将一个很棒的实用程序 cross-spawn 包装到我自己的模块中。
exec.js
var spawn = require('cross-spawn');
var util = require('util');
const Sugar = require('sugar');
// parses the output if it is in JSON format
function parse(str){
var outStr = str, res;
try {
res = JSON.parse(outStr.trim());
} catch (error) {
res = outStr;
}
return res;
}
module.exports = {
spawn() {
var args = Array.prototype.slice.call(arguments);
return new Promise(function (resolve, reject) {
var stdout = '', stderr = '';
var cp = spawn.apply(null, args);
cp.stdout.on('data', function (chunk) {
var res = parse(chunk.toString());
if (!res.result && res.commandOutput) { // pretty log
console.log(util.inspect(Sugar.Object.filter(res, (val, key) => key !== 'commandOutput'),{colors: true, depth:null}));
console.log(res.commandOutput);
} else console.log(res);
stdout += chunk;
});
cp.stderr.on('data', function (chunk) {
console.error(chunk.toString());
stderr += chunk;
});
cp.on('error', (e)=>{
reject((stderr && new Error(stderr)) || e);
})
.on('close', function (code) {
let output = parse(stdout);
if (code === 0) {
resolve(output);
} else {
let err = (output && output.errorMsg && new Error(output.errorMsg)) || new Error(stderr);
console.error(err);
reject(err);
}
});
});
},
};
然后我将它用作任何常规 Promise shell exec:
const exec = require('./lib/exec');
...
function execAndLog (){
...
return exec.spawn(...execArgs, opts).then(processOutput);
}
我希望能够在子进程 运行 中显示命令输出,然后在我的程序中处理输出。这可能吗?
var result = spawnSync('neo', ['-help'], {
stdio: 'inherit',
encoding: 'utf-8'
});
var savedOutput = result.stdout;
console.log(String(savedOutput));
这就是我最后做的事情。
我将一个很棒的实用程序 cross-spawn 包装到我自己的模块中。
exec.js
var spawn = require('cross-spawn');
var util = require('util');
const Sugar = require('sugar');
// parses the output if it is in JSON format
function parse(str){
var outStr = str, res;
try {
res = JSON.parse(outStr.trim());
} catch (error) {
res = outStr;
}
return res;
}
module.exports = {
spawn() {
var args = Array.prototype.slice.call(arguments);
return new Promise(function (resolve, reject) {
var stdout = '', stderr = '';
var cp = spawn.apply(null, args);
cp.stdout.on('data', function (chunk) {
var res = parse(chunk.toString());
if (!res.result && res.commandOutput) { // pretty log
console.log(util.inspect(Sugar.Object.filter(res, (val, key) => key !== 'commandOutput'),{colors: true, depth:null}));
console.log(res.commandOutput);
} else console.log(res);
stdout += chunk;
});
cp.stderr.on('data', function (chunk) {
console.error(chunk.toString());
stderr += chunk;
});
cp.on('error', (e)=>{
reject((stderr && new Error(stderr)) || e);
})
.on('close', function (code) {
let output = parse(stdout);
if (code === 0) {
resolve(output);
} else {
let err = (output && output.errorMsg && new Error(output.errorMsg)) || new Error(stderr);
console.error(err);
reject(err);
}
});
});
},
};
然后我将它用作任何常规 Promise shell exec:
const exec = require('./lib/exec');
...
function execAndLog (){
...
return exec.spawn(...execArgs, opts).then(processOutput);
}