@effectionx/fs

中文文档
0.3.0发布于 2026/04/17MITTypeScriptES Module构建来源已验证近 30 天下载 4.8万

文件系统操作作为Evition操作包裹

中文使用文档
文档目录22 节
  1. 安装
  2. 基本用途
  3. 文件操作
  4. 数据( )
  5. 错误( )
  6. 已存在( )
  7. 读取 TextFile ()
  8. 写入 TextFile ()
  9. 保证文件( )
  10. 复制文件( )
  11. rm( )
  12. 目录操作
  13. 保证目录 ()
  14. 已读 dir ()
  15. 空目录 ()
  16. 行( )
  17. 扩展Glob ()
  18. 公用事业
  19. 直径( )
  20. globTogExp () (中文(简体) )
  21. URL 转换
  22. 路径和 URL 支持

页: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));

版本与发布记录

@effectionx/fs 共发布 5 个版本,首次发布于 2026/01/19;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.3.0latest2026/04/176 个月前
0.2.32026/03/237 个月前
0.2.22026/02/248 个月前
0.2.12026/02/129 个月前
0.2.02026/01/199 个月前

关于 @effectionx/fs 的常见问题

@effectionx/fs 是什么?

文件系统操作作为Evition操作包裹

如何安装 @effectionx/fs?

在项目目录运行 npm install @effectionx/fs,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:effection(^3 || ^4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@effectionx/fs 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@effectionx/fs 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@effectionx/fs 可以商用吗?

@effectionx/fs 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@effectionx/fs 的下载量有多少?

根据 npm 官方下载统计,@effectionx/fs 在 2026/10/13 – 2026/11/11 这 30 天内被下载 47,518 次,日均约 1,584 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@effectionx/fs 还在维护吗?

最新版本 0.3.0 发布于 2026/04/17(6 个月前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/01/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。