typescript - 你如何连接两个(管道标准输入和标准输出)Deno 子进程?

标签 typescript deno

API docs 来看,一个 Deno 子进程(Deno.Process 的一个实例)可以接收四种标准输入类型之一,标准输出也是如此。但是,文档中没有提到如何将一个子进程的输出通过管道传输到另一个子进程的输入。我想要实现的是类似于基本的 UNIX 管道( oneProcess | another ),然后读取管道中第二个进程的输出。简单地运行

const someProcess = Deno.run({
  cmd: ["oneProcess firstParameter | another 2ndParameter"]
});

失败并出现以下错误:

error: Uncaught NotFound: No such file or directory (os error 2)



因为第一个参数(字符串)应该是一个可执行文件。

那么,Deno 将如何实现这一点,我们是否需要设置 "piped"作为子进程的输出和输入(分别),然后手动从一个到另一个读取和写入数据?

最佳答案

您收到 NotFound: no such file or directory因为传递给 cmd 的值必须是二进制文件的路径。

the first element needs to be a path to the binary


onProcess | another不是二进制。

使用 unix 管道 |你可以运行 bash然后写信给 stdin .
const p = Deno.run({
  cmd: ["bash"],
  stdout: "piped",
  stdin: "piped"
});

const encoder = new TextEncoder();
const decoder = new TextDecoder();

const command = "echo -n yes | md5sum";
await p.stdin.write(encoder.encode(command));

await p.stdin.close();
const output = await p.output()
p.close();

console.log(decoder.decode(output)); // a6105c0a611b41b08f1209506350279e

关于typescript - 你如何连接两个(管道标准输入和标准输出)Deno 子进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62088065/

相关文章:

javascript - 将反向属性和反向类选择器与现有选择器相结合

angular - 为什么添加关键字 `let` 或 `var` 会使代码无法编译

angular - 如何访问 http post 响应并解析为 json?

deno - 测试用例在 deno 上泄漏异步操作

firebase - 在 Deno 中使用 firebase-admin 的可能方法

jquery - SPFx 不能使用 JQuery?

angular - 使用 spyon 时的代码覆盖率问题

deno - 将脚本通过管道传输到 deno run

javascript - 在 Deno 中创建 Rest API

google-cloud-storage - 使用 Deno 将文件上传到 Google 存储