运行时包管理器打包器测试运行器代码检查格式化指南参考博客反馈

Glob

使用 Bun 的快速本地文件匹配实现

快速开始

扫描目录中匹配 *.ts 的文件:

TypeScript
import { Glob } from "bun";

const glob = new Glob("**/*.ts");

// 递归扫描当前工作目录及其所有子目录
for await (const file of glob.scan(".")) {
  console.log(file); // => "index.ts"
}

根据 glob 模式匹配字符串:

TypeScript
import { Glob } from "bun";

const glob = new Glob("*.ts");

glob.match("index.ts"); // => true
glob.match("index.js"); // => false

Glob 类实现了以下接口:

TypeScript
class Glob {
  scan(root: string | ScanOptions): AsyncIterable<string>;
  scanSync(root: string | ScanOptions): Iterable<string>;

  match(path: string): boolean;
}

interface ScanOptions {
  /**
   * 匹配起始的根目录。默认为 `process.cwd()`
   */
  cwd?: string;

  /**
   * 允许模式匹配以点 (`.`) 开头的条目。
   *
   * @default false
   */
  dot?: boolean;

  /**
   * 返回条目的绝对路径。
   *
   * @default false
   */
  absolute?: boolean;

  /**
   * 指示是否遍历符号链接目录的子孙目录。
   *
   * @default false
   */
  followSymlinks?: boolean;

  /**
   * 当符号链接损坏时抛出错误。
   *
   * @default false
   */
  throwErrorOnBrokenSymlink?: boolean;

  /**
   * 只返回文件。
   *
   * @default true
   */
  onlyFiles?: boolean;
}

支持的 Glob 模式

Bun 支持以下 glob 模式:

? - 匹配任意单个字符

TypeScript
const glob = new Glob("???.ts");
glob.match("foo.ts"); // => true
glob.match("foobar.ts"); // => false

* - 匹配零个或多个字符,但不包括路径分隔符(/ 或 \)

TypeScript
const glob = new Glob("*.ts");
glob.match("index.ts"); // => true
glob.match("src/index.ts"); // => false

** - 匹配任意数量的字符,包括路径分隔符 /

TypeScript
const glob = new Glob("**/*.ts");
glob.match("index.ts"); // => true
glob.match("src/index.ts"); // => true
glob.match("src/index.js"); // => false

[ab] - 匹配括号中包含的任意字符,以及字符范围

TypeScript
const glob = new Glob("ba[rz].ts");
glob.match("bar.ts"); // => true
glob.match("baz.ts"); // => true
glob.match("bat.ts"); // => false

可以使用字符范围(例如 [0-9]、[a-z])。取反运算符 ^ 或 ! 用于匹配括号中字符以外的任意内容(例如 [^ab]、[!a-z])。

TypeScript
const glob = new Glob("ba[a-z][0-9][^4-9].ts");
glob.match("bar01.ts"); // => true
glob.match("baz83.ts"); // => true
glob.match("bat22.ts"); // => true
glob.match("bat24.ts"); // => false
glob.match("ba0a8.ts"); // => false

{a,b,c} - 匹配任意给定的模式

TypeScript
const glob = new Glob("{a,b,c}.ts");
glob.match("a.ts"); // => true
glob.match("b.ts"); // => true
glob.match("c.ts"); // => true
glob.match("d.ts"); // => false

这些模式最多可以嵌套 10 层,并且可以包含前面介绍的任意通配符。

! - 在模式开头表示取反

TypeScript
const glob = new Glob("!index.ts");
glob.match("index.ts"); // => false
glob.match("foo.ts"); // => true

\ - 转义上述任意特殊字符

TypeScript
const glob = new Glob("\\!index.ts");
glob.match("!index.ts"); // => true
glob.match("index.ts"); // => false

Node.js 的 fs.glob() 兼容性

Bun 还实现了 Node.js 的 fs.glob() 函数:

TypeScript
import { glob, globSync, promises } from "node:fs";

// Array of patterns
const files = await Array.fromAsync(promises.glob(["**/*.ts", "**/*.js"]));

// Exclude patterns
const filtered = await Array.fromAsync(
  promises.glob("**/*", {
    exclude: ["node_modules/**", "**/*.test.*"],
  }),
);

这三个函数均支持:

  • 作为第一个参数的模式数组
  • 用于过滤结果的 exclude 选项。