bun-file-io
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUse this when
使用场景
- Editing file I/O or scans in
packages/opencode - Handling directory operations or external tools
- 在中编辑文件I/O或扫描操作时
packages/opencode - 处理目录操作或外部工具时
Bun file APIs (from Bun docs)
Bun文件API(来自Bun文档)
- is lazy; call
Bun.file(path),text,json,stream,arrayBuffer,bytesto read.exists - Metadata: ,
file.size,file.type.file.name - writes strings, buffers, Blobs, Responses, or files.
Bun.write(dest, input) - deletes a file.
Bun.file(...).delete() - returns a FileSink for incremental writes.
file.writer() - +
Bun.Globfor scans.Array.fromAsync(glob.scan({ cwd, absolute, onlyFiles, dot })) - Use to find a binary, then
Bun.whichto run it.Bun.spawn - for stream output.
Bun.readableStreamToText/Bytes/JSON
- 是惰性加载的;调用
Bun.file(path)、text、json、stream、arrayBuffer、bytes方法来读取内容。exists - 元数据:、
file.size、file.type。file.name - 可写入字符串、缓冲区、Blob、响应或文件。
Bun.write(dest, input) - 用于删除文件。
Bun.file(...).delete() - 返回一个FileSink用于增量写入。
file.writer() - 扫描操作使用+
Bun.Glob。Array.fromAsync(glob.scan({ cwd, absolute, onlyFiles, dot })) - 使用查找二进制文件,然后用
Bun.which运行它。Bun.spawn - 对于流输出,使用。
Bun.readableStreamToText/Bytes/JSON
When to use node:fs
何时使用node:fs
- Use for directories (
node:fs/promises,mkdir, recursive operations).readdir
- 处理目录操作(、
mkdir、递归操作)时使用readdir。node:fs/promises
Repo patterns
仓库使用模式
- Prefer Bun APIs over Node for file access.
fs - Check before reading.
Bun.file(...).exists() - For binary/large files use and MIME checks via
arrayBuffer().file.type - Use +
Bun.Globfor scans.Array.fromAsync - Decode tool stderr with .
Bun.readableStreamToText - For large writes, use .
Bun.write(Bun.file(path), text)
- 文件访问优先使用Bun API而非Node 。
fs - 读取文件前先检查。
Bun.file(...).exists() - 处理二进制/大文件时使用,并通过
arrayBuffer()进行MIME类型检查。file.type - 扫描操作使用+
Bun.Glob。Array.fromAsync - 使用解码工具的标准错误输出。
Bun.readableStreamToText - 大文件写入使用。
Bun.write(Bun.file(path), text)
Quick checklist
快速检查清单
- Use Bun APIs first.
- Use /
path.joinfor paths.path.resolve - Prefer promise over
.catch(...)when possible.try/catch
- 优先使用Bun API。
- 路径处理使用/
path.join。path.resolve - 尽可能优先使用Promise的而非
.catch(...)。try/catch