@effectionx/fs

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

File system operations wrapped as Effection operations

原始使用文档
文档目录22 节
  1. Installation
  2. Basic Usage
  3. File Operations
  4. stat()
  5. lstat()
  6. exists()
  7. readTextFile()
  8. writeTextFile()
  9. ensureFile()
  10. copyFile()
  11. rm()
  12. Directory Operations
  13. ensureDir()
  14. readdir()
  15. emptyDir()
  16. walk()
  17. expandGlob()
  18. Utilities
  19. toPath()
  20. globToRegExp()
  21. URL Conversion
  22. Path and URL Support

FS

File system operations for Effection programs. This package wraps Node.js fs/promises APIs as Effection Operations with structured concurrency support.


Installation

npm install @effectionx/fs

Basic Usage

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

File Operations

stat()

Get file or directory stats.

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

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

lstat()

Get file or directory stats without following symlinks.

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

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

exists()

Check if a file or directory exists.

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

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

readTextFile()

Read a file as text.

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

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

writeTextFile()

Write text to a file.

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

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

ensureFile()

Ensure a file exists, creating parent directories and the file if needed.

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

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

copyFile()

Copy a file.

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

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

rm()

Remove a file or directory.

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

Directory Operations

ensureDir()

Ensure a directory exists, creating it recursively if needed.

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

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

readdir()

Read the contents of a directory.

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

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

emptyDir()

Empty a directory by removing all its contents. Creates the directory if it doesn't exist.

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

yield* emptyDir("./dist");

walk()

Walk a directory tree and yield entries as a Stream.

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

Each entry includes:

  • path - Full path to the entry
  • name - Basename of the entry
  • isFile - Whether it's a file
  • isDirectory - Whether it's a directory
  • isSymlink - Whether it's a symbolic link

Walk Options

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

expandGlob()

Expand glob patterns and yield matching paths as a Stream.

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

Glob Options

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

Utilities

toPath()

Convert a path or URL to a file path string.

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

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

globToRegExp()

Convert a glob pattern to a 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 Conversion

Re-exported from node:url for convenience:

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" }

Path and URL Support

All file operations accept either a string path or a URL object:

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 是什么?

File system operations wrapped as Effection operations

如何安装 @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 和提交记录判断。