SuperAPI AI 工具集

bun-file-io

编程与开发

查看项目

更新于 10-15 18:45

技能介绍

用途:在 packages/opencode 中编辑文件 I/O 或目录扫描相关代码时,提供统一偏好的文件 API 与写法约定。

主要能力与用法:优先使用 Bun API。用 Bun.file(path) 读取(惰性,需调用 text、json、stream、arrayBuffer、bytes、exists),可通过 file.size、file.type、file.name 获取元数据;用 Bun.write(dest, input) 写入字符串、缓冲区、Blob、Response 或文件,大文件写入建议 Bun.write(Bun.file(path), text);用 Bun.file(...).delete() 删除文件;用 file.writer() 返回 FileSink 做增量写入。扫描使用 Bun.Glob 配合 Array.fromAsync(glob.scan({ cwd, absolute, onlyFiles, dot }))。查找二进制用 Bun.which,运行用 Bun.spawn;流输出用 Bun.readableStreamToText/Bytes/JSON 解码,工具 stderr 也用 Bun.readableStreamToText。

仓库约定:文件访问优先 Bun 而非 Node fs;读取前先检查 Bun.file(...).exists();二进制或大文件用 arrayBuffer() 并结合 file.type 做 MIME 检查;路径使用 path.join/path.resolve;尽可能用 promise 的 .catch(...) 替代 try/catch。

限制与注意:Bun.file(...).exists() 在路径为目录时返回 false,若路径可能是文件或目录,应改用 Filesystem.exists(...)。目录操作(mkdir、readdir、递归操作)仍需使用 node:fs/promises。