pipe - 如何将文件通过管道传输到 Deno 的子进程中?
问题描述
我正在尝试将 SQL 文件的内容通过管道传输到 mysql 进程中以在 Deno 中导入转储,如下所示:
const mysql = Deno.run({
cmd: ["mysql", "--defaults-file=my.cnf", "mydatabase"],
cwd,
stdin: "piped"
});
await mysql.stdin.write(
Deno.readFile("data.sql")
);
await mysql.status();
不幸的是,我得到了错误:
error: Uncaught (in promise) TypeError: Error parsing args: serde_v8 error: ExpectedArray
await mysql.stdin.write(
^
at deno:core/core.js:86:46
at unwrapOpResult (deno:core/core.js:106:13)
at Object.opAsync (deno:core/core.js:115:28)
at write (deno:runtime/js/12_io.js:107:23)
at File.write (deno:runtime/js/40_files.js:84:14)
如何修复错误,以便我能够将文件的内容提供给子流程?
解决方案
Deno.readFile
返回Promise<Uint8Array>
,因此await
在将其传递给mysql.stdin.write
.
您还需要关闭stdin
子流程(请参阅此问题)。
这是一个完整的例子:
piped.ts
:
import {writeAll} from 'https://deno.land/std@0.101.0/io/mod.ts';
const filePath = 'hello.txt';
// create example text file
await Deno.writeTextFile(filePath, 'hello world\n');
try {
// create subprocess
const subprocess = Deno.run({cmd: ['cat'], stdin: 'piped'});
// write Uint8Array to stdin
await writeAll(subprocess.stdin, await Deno.readFile(filePath));
// close stdin (see https://github.com/denoland/deno/issues/7727)
subprocess.stdin.close();
await subprocess.status();
subprocess.close();
}
finally {
// remove example text file
await Deno.remove(filePath);
}
$ deno run --allow-read=hello.txt --allow-run=cat --allow-write=hello.txt piped.ts
hello world
推荐阅读
- angular - Angular 6,加载 svg 不会出现在 chrome 中
- serverless-framework - Swagger 定义的 API 无法在 Serverless 框架上本地执行
- angular - 所需的角度 IOS 年历,如 [https://github.com/MariemChaabeni/angular-calendar-year-view] 用于 angular6
- node.js - 使用 Mongoose 在 MongoDB 中设置的时间后文档不会过期
- mysql - 当 authentication_string 正确时,用户 'root'@'localhost' 的访问被拒绝(使用密码:YES)
- testing - 如何在 JMeter 的单个线程中调用的循环控制器之间添加计时器
- android - 在 android 中获取联系人列表需要时间和崩溃的应用程序
- git - Google Cloud Build - 触发器仅调用分支构建
- mockito - Mockito 为 Future 返回空值
- xml - EditText 在选择时失去背景