首页 > 解决方案 > 有没有办法从节点中的 child_process.spawn 方法获得完整的输出

问题描述

我正在尝试ng lint --format json使用节点的child_processspawn 方法运行命令。代码如下所示:

const spawn = require('child_process').spawn;
const fs = require('fs-extra');
const jsonLog = fs.createWriteStream('test.json', { flags: 'a' });
const linter = spawn('ng', ['lint', '--format', 'json']);
linter.stdout.pipe(jsonLog);

如您所料,我正在尝试将 JSON 输出到文件中。我面临的问题是我没有得到完整的 JSON 输出。它在某些时候被截断,使其成为无效的 JSON。我认为它必须与缓冲区大小有关。

现在你们中的大多数人会建议我这样做:

ng lint --format json > jsonFile.json

我已经意识到了。我想以编程方式进行,因为我正在尝试为它构建一个实用程序。如果您有任何想法或解决方案,请告诉我。

标签: node.jsangularchild-processstdiospawn

解决方案


推荐阅读