bun-file-io

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Use this when

使用场景

  • Editing file I/O or scans in
    packages/opencode
  • Handling directory operations or external tools
  • packages/opencode
    中编辑文件I/O或扫描操作时
  • 处理目录操作或外部工具时

Bun file APIs (from Bun docs)

Bun文件API(来自Bun文档)

  • Bun.file(path)
    is lazy; call
    text
    ,
    json
    ,
    stream
    ,
    arrayBuffer
    ,
    bytes
    ,
    exists
    to read.
  • Metadata:
    file.size
    ,
    file.type
    ,
    file.name
    .
  • Bun.write(dest, input)
    writes strings, buffers, Blobs, Responses, or files.
  • Bun.file(...).delete()
    deletes a file.
  • file.writer()
    returns a FileSink for incremental writes.
  • Bun.Glob
    +
    Array.fromAsync(glob.scan({ cwd, absolute, onlyFiles, dot }))
    for scans.
  • Use
    Bun.which
    to find a binary, then
    Bun.spawn
    to run it.
  • Bun.readableStreamToText/Bytes/JSON
    for stream output.
  • Bun.file(path)
    是惰性加载的;调用
    text
    json
    stream
    arrayBuffer
    bytes
    exists
    方法来读取内容。
  • 元数据:
    file.size
    file.type
    file.name
  • Bun.write(dest, input)
    可写入字符串、缓冲区、Blob、响应或文件。
  • Bun.file(...).delete()
    用于删除文件。
  • file.writer()
    返回一个FileSink用于增量写入。
  • 扫描操作使用
    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
    node:fs/promises
    for directories (
    mkdir
    ,
    readdir
    , recursive operations).
  • 处理目录操作(
    mkdir
    readdir
    、递归操作)时使用
    node:fs/promises

Repo patterns

仓库使用模式

  • Prefer Bun APIs over Node
    fs
    for file access.
  • Check
    Bun.file(...).exists()
    before reading.
  • For binary/large files use
    arrayBuffer()
    and MIME checks via
    file.type
    .
  • Use
    Bun.Glob
    +
    Array.fromAsync
    for scans.
  • Decode tool stderr with
    Bun.readableStreamToText
    .
  • For large writes, use
    Bun.write(Bun.file(path), text)
    .
  • 文件访问优先使用Bun API而非Node
    fs
  • 读取文件前先检查
    Bun.file(...).exists()
  • 处理二进制/大文件时使用
    arrayBuffer()
    ,并通过
    file.type
    进行MIME类型检查。
  • 扫描操作使用
    Bun.Glob
    +
    Array.fromAsync
  • 使用
    Bun.readableStreamToText
    解码工具的标准错误输出。
  • 大文件写入使用
    Bun.write(Bun.file(path), text)

Quick checklist

快速检查清单

  • Use Bun APIs first.
  • Use
    path.join
    /
    path.resolve
    for paths.
  • Prefer promise
    .catch(...)
    over
    try/catch
    when possible.
  • 优先使用Bun API。
  • 路径处理使用
    path.join
    /
    path.resolve
  • 尽可能优先使用Promise的
    .catch(...)
    而非
    try/catch