【发布时间】:2016-12-03 06:04:14
【问题描述】:
我正在编写一个电子应用程序,这个应用程序处理从电子内部执行的终端命令。
我在执行npm ls 命令时遇到了麻烦。从 cli 运行它时,依赖关系树会打印到 stdout,最后可能会出现一些来自 stderr 的警告。
请看下面的截图。
我稍微挖掘了npm 源代码,它首先记录结果,然后打印错误。所以它就像我在终端中看到的一样。
但是,当我对 child_process spawn (或 exec 没关系)执行相同操作时,顺序不同。
看起来由于stdout 数据的大块,stderr 打印在所有stdout 的中间。
下面我写的代码:
// this is mapped to require( 'child_process' ).spawn
this.$set( 'process', this.spawn(
'npm',
[ 'ls' ],
{
cwd : options.cwd,
// following are only my tryouts - nothing helped :(
// some npm ls command destroy kill the scripts
// with too big buffers in stdout
// maxBuffer : 1024 * 5000
// shell : true
}
) );
// this.handleData is only printing out for nwo
this.process.stdout.on( 'data', this.handleData );
this.process.stderr.on( 'data', this.handleData );
当大数据来自 stdout 和来自 stderr 的小数据时,stderr 会在中间被调用。
这是预期的行为吗?我可以以某种方式解决此问题以检索与终端中相同的行为吗?
谢谢。 :)
【问题讨论】: