# @effectionx/fs 中文文档

规范地址：https://npmcn.com/package/%40effectionx/fs/

版本：0\.3\.0
npm 元数据更新时间：2026-09-30T21:08:26.889Z
中文译文更新时间：2026-11-13T08:27:41.568Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40effectionx/fs
原文对照：https://npmcn.com/package/%40effectionx/fs/?lang=original
项目主页：https://github.com/thefrontside/effectionx#readme
源码仓库：https://github.com/thefrontside/effectionx

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：102 kB，15 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：1 个
- peer 依赖：effection ^3 \|\| ^4
- 首次发布：2026-01-18T19:47:15.450Z
- 最新版本发布：2026-04-16T18:52:16.077Z
- 近 30 天下载量：47518（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：5（近 12 个月 5 个）

## 中文简介

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

## 安装

```sh
npm install @effectionx/fs
```

也可以使用：`pnpm add @effectionx/fs`、`yarn add @effectionx/fs`、`bun add @effectionx/fs`

## README

# 页:1

Effection程序的文件系统操作. 此软件包包裹了节点.js `fs/promises` API作为效果操作,并有结构化的货币支持.

* * *

## 安装

```bash
npm install @effectionx/fs
```

## 基本用途

```typescript
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 }));
  }
});
```

## 文件操作

### 数据( )

获取文件或目录数据.

```typescript
import { stat } from "@effectionx/fs";

const stats = yield* stat("./file.txt");
console.log(stats.isFile()); // true
console.log(stats.size); // file size in bytes
```

### 错误( )

获取文件或目录数据而不跟随同名链接.

```typescript
import { lstat } from "@effectionx/fs";

const stats = yield* lstat("./symlink");
console.log(stats.isSymbolicLink()); // true
```

### 已存在( )

检查文件或目录是否存在 .

```typescript
import { exists } from "@effectionx/fs";

if (yield* exists("./config.json")) {
  console.log("Config file found");
}
```

### 读取 TextFile ()

读取文件为文本 .

```typescript
import { readTextFile } from "@effectionx/fs";

const content = yield* readTextFile("./README.md");
```

### 写入 TextFile ()

将文本写入文件 .

```typescript
import { writeTextFile } from "@effectionx/fs";

yield* writeTextFile("./output.txt", "Hello, World!");
```

### 保证文件( )

确保文件存在, 必要时创建父目录和文件 .

```typescript
import { ensureFile } from "@effectionx/fs";

yield* ensureFile("./data/logs/app.log");
```

### 复制文件( )

复制一个文件.

```typescript
import { copyFile } from "@effectionx/fs";

yield* copyFile("./source.txt", "./backup.txt");
```

### rm( )

删除文件或目录 .

```typescript
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 });
```

## 目录操作

### 保证目录 ()

确保存在目录, 必要时可递归创建目录 .

```typescript
import { ensureDir } from "@effectionx/fs";

yield* ensureDir("./data/cache/images");
```

### 已读 dir ()

读取目录的内容 .

```typescript
import { readdir } from "@effectionx/fs";

const entries = yield* readdir("./src");
console.log(entries); // ["index.ts", "utils.ts", ...]
```

### 空目录 ()

删除目录的全部内容以清空目录 。 如果创建目录, 则创建目录 不存在.

```typescript
import { emptyDir } from "@effectionx/fs";

yield* emptyDir("./dist");
```

### 行( )

行走目录树并生成作为流的条目 .

```typescript
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` - 这是否是一个象征性的联系

#### 行走选项

```typescript
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 模式并生成匹配路径作为流 .

```typescript
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();
}
```

#### 球形选项

```typescript
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 转换为文件路径字符串 .

```typescript
import { toPath } from "@effectionx/fs";

toPath("./file.txt");           // "./file.txt"
toPath(new URL("file:///tmp")); // "/tmp"
```

### globTogExp () (中文(简体) )

将 glob 模式转换为 RegExp .

```typescript
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` 为了方便:

```typescript
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` 对象 :

```typescript
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 是什么？

文件系统操作作为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 和提交记录判断。

