deno.com

子进程:收集输出

在 Github 上编辑

我们不常孤立地编写程序。在很多情况下,我们希望与外部系统交互,而衍生子进程是实现此目的的常用方法。

Deno 命名空间有一个统一的 API 用于与外部系统交互,称为 Deno.Command。 通过它,我们可以初始化有关命令的一些信息,但它不会立即执行。
const command = new Deno.Command("deno", {
  args: [
    "eval",
    "\
    console.log('hello from deno'); \
    console.error('hello from stderr'); \
    ",
  ],
});
在最简单的情况下,我们只想运行进程直到完成。这可以使用 command.output() 来实现
let result = await command.output();
也可以使用 command.outputSync() 同步实现
result = command.outputSync();
我们现在可以与 stdout 和 stderr 交互
const textDecoder = new TextDecoder();
console.log("stdout:", textDecoder.decode(result.stdout));
console.log("stderr:", textDecoder.decode(result.stderr));

使用 Deno CLI 在本地运行此示例

deno run --allow-run https://docs.deno.org.cn/examples/scripts/subprocesses_output.ts

你找到你需要的内容了吗?

隐私政策