javascript - Node : How to make spawn function synchronous instead of using spawnSync?

标签 javascript node.js

我目前正在使用 spawnSync 和 stdio:inherit 将日志打印在我的控制台上。但是,我正在寻找将自定义日志记录到单独的文件,以防万一在生成期间出现任何故障。

我想创建一个包装器

spawn

因此它具有以下属性:

  • 输出存储在变量中的同步函数
  • 在控制台上查看输出
  • 将 stdout 和 stderr 写入文件

例如:

const result = spawnSync('ls', [ '-l', '-a' ], { stdio: 'inherit'}); // will print as it's processing
console.log(result.stdout); // will print null

const result = spawnSync('ls', [ '-l', '-a' ], { encoding: 'utf-8' }); // won't print anything
console.log(result.stdout); // will print ls results only on completion

我需要 result 以便它在处理时打印并同时写入文件

除了 shell 脚本之外,我还在寻找 node.js 方面的一些策略或解决方案

最佳答案

我想由于 node.js 的限制,我们无法使所有这三个成为可能

根据文档 enter image description here

如果我们尝试使用 stdio:'inherit' ,我们可以将输出重定向到父标准输出流或使用

fs.openSync()

由于限制,我们也不能使用自定义流。

关于javascript - Node : How to make spawn function synchronous instead of using spawnSync?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66996109/

相关文章:

javascript - AngularJS 服务注入(inject)时未定义

javascript - 在 View 更改之前, Angular 值不会反射(reflect)在范围中

node.js - JSON 文件不包含使用 NodeJS 的 AWS Lambda 中的所有结果

node.js - Mailchimp错误: -100 when using node-mailchimp API in a Meteor app

javascript - Angular JS 下拉菜单使用 ng-click 和 ng-show 显示动态内容?

javascript - 如何使用 JS 解析带有嵌套对象的 JSON 文件?

javascript - 如果复选框为 true,则在文本区域中显示值

javascript - co 和 await 的区别

javascript - 如何将返回值推送到数组 mongodb 中?

node.js - Node 模块路径执行 Windows 10