页:1
Effection程序的文件系统操作. 此软件包包裹了节点.js
fs/promises API作为效果操作,并有结构化的货币支持.
安装
npm install @effectionx/fs
基本用途
import { main } from "effection";
import { exists, readTextFile, writeTextFile } from "@effectionx/fs";
await main(function* () {
if (yield* exists("./config.json")) {
const config = yield* readTextFile("./config.json");
console.log(JSON.parse(config));
} else {
yield* writeTextFile("./config.json", JSON.stringify({ version: 1 }));
}
});
文件操作
数据( )
获取文件或目录数据.
import { stat } from "@effectionx/fs";
const stats = yield* stat("./file.txt");
console.log(stats.isFile()); // true
console.log(stats.size); // file size in bytes
错误( )
获取文件或目录数据而不跟随同名链接.
import { lstat } from "@effectionx/fs";
const stats = yield* lstat("./symlink");
console.log(stats.isSymbolicLink()); // true
已存在( )
检查文件或目录是否存在 .
import { exists } from "@effectionx/fs";
if (yield* exists("./config.json")) {
console.log("Config file found");
}
读取 TextFile ()
读取文件为文本 .
import { readTextFile } from "@effectionx/fs";
const content = yield* readTextFile("./README.md");
写入 TextFile ()
将文本写入文件 .
import { writeTextFile } from "@effectionx/fs";
yield* writeTextFile("./output.txt", "Hello, World!");
保证文件( )
确保文件存在, 必要时创建父目录和文件 .
import { ensureFile } from "@effectionx/fs";
yield* ensureFile("./data/logs/app.log");
复制文件( )
复制一个文件.
import { copyFile } from "@effectionx/fs";
yield* copyFile("./source.txt", "./backup.txt");
rm( )
删除文件或目录 .
import { rm } from "@effectionx/fs";
// Remove a file
yield* rm("./temp.txt");
// Remove a directory recursively
yield* rm("./temp", { recursive: true });
// Force remove (no error if doesn't exist)
yield* rm("./maybe-exists", { force: true });
目录操作
保证目录 ()
确保存在目录, 必要时可递归创建目录 .
import { ensureDir } from "@effectionx/fs";
yield* ensureDir("./data/cache/images");
已读 dir ()
读取目录的内容 .
import { readdir } from "@effectionx/fs";
const entries = yield* readdir("./src");
console.log(entries); // ["index.ts", "utils.ts", ...]
空目录 ()
删除目录的全部内容以清空目录 。 如果创建目录, 则创建目录 不存在.
import { emptyDir } from "@effectionx/fs";
yield* emptyDir("./dist");
行( )
行走目录树并生成作为流的条目 .
import { walk } from "@effectionx/fs";
import { each } from "effection";
for (const entry of yield* each(walk("./src"))) {
if (entry.isFile && entry.name.endsWith(".ts")) {
console.log(entry.path);
}
yield* each.next();
}
每个条目包括:
path- 进入的完整路径name- 条目的基名isFile- 是否是一个文件isDirectory- 是否是一个目录isSymlink- 这是否是一个象征性的联系
行走选项
walk("./src", {
includeDirs: true, // Include directories (default: true)
includeFiles: true, // Include files (default: true)
includeSymlinks: true, // Include symlinks (default: true)
match: [/\.ts$/], // Only include matching paths
skip: [/node_modules/], // Exclude matching paths
maxDepth: 3, // Maximum traversal depth
followSymlinks: false, // Follow symbolic links (default: false)
});
扩展Glob ()
扩展 glob 模式并生成匹配路径作为流 .
import { expandGlob } from "@effectionx/fs";
import { each } from "effection";
for (const entry of yield* each(expandGlob("./src/**/*.ts"))) {
console.log(entry.path);
yield* each.next();
}
球形选项
expandGlob("**/*.ts", {
root: "./src", // Root directory (default: ".")
exclude: ["**/*.test.ts"], // Patterns to exclude
includeDirs: false, // Include directories (default: true)
followSymlinks: false, // Follow symbolic links (default: false)
});
公用事业
直径( )
将路径或 URL 转换为文件路径字符串 .
import { toPath } from "@effectionx/fs";
toPath("./file.txt"); // "./file.txt"
toPath(new URL("file:///tmp")); // "/tmp"
globTogExp () (中文(简体) )
将 glob 模式转换为 RegExp .
import { globToRegExp } from "@effectionx/fs";
const regex = globToRegExp("*.ts");
regex.test("file.ts"); // true
regex.test("file.js"); // false
// Supports extended glob syntax
globToRegExp("**/*.{ts,js}"); // Match .ts or .js files recursively
globToRegExp("file[0-9].txt"); // Character classes
globToRegExp("src/**/test?.ts"); // ? matches single character
URL 转换
从 node:url 为了方便:
import { fromFileUrl, toFileUrl } from "@effectionx/fs";
fromFileUrl(new URL("file:///tmp/file.txt")); // "/tmp/file.txt"
toFileUrl("/tmp/file.txt"); // URL { href: "file:///tmp/file.txt" }
路径和 URL 支持
所有文件操作都接受字符串路径或一个 URL 对象 :
import { readTextFile } from "@effectionx/fs";
// String path
yield* readTextFile("./config.json");
// URL object
yield* readTextFile(new URL("file:///etc/config.json"));
// import.meta.url based paths
yield* readTextFile(new URL("./data.json", import.meta.url));