On this page
@std/fs
Overview Jump to heading
Helpers for working with the filesystem.
import { ensureFile, copy, ensureDir, move } from "@std/fs";
await ensureFile("example.txt");
await copy("example.txt", "example_copy.txt");
await ensureDir("subdir");
await move("example_copy.txt", "subdir/example_copy.txt");
Add to your project Jump to heading
deno add jsr:@std/fs
为什么使用 @std/fs? Jump to heading
当你需要比基本的 Deno API 提供的更高层次文件系统操作(例如确保文件/目录存在、复制、移动、遍历目录)时,可以使用它。
示例 Jump to heading
import { ensureDir, expandGlob, walk } from "@std/fs";
await ensureDir("./out/assets");
for await (const entry of expandGlob("src/**/*.{ts,tsx}")) {
console.log(entry.path);
}
for await (
const f of walk("./content", { includeDirs: false, exts: [".md"] })
) {
console.log(f.path);
}
小贴士 Jump to heading
- 大多数辅助函数是异步的,别忘了使用
await! - 结合使用
@std/path以构建跨平台路径。 - 在替换文件时,使用
copy时显式设置overwrite: true。