Skip to content

クイックスタート

*.ts に一致するファイルのディレクトリをスキャン

ts
import { Glob } from "bun";

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

// 現在の作業ディレクトリとそのすべてのサブディレクトリを再帰的にスキャン
for await (const file of glob.scan(".")) {
  console.log(file); // => "index.ts"
}

文字列をグロブパターンと照合

ts
import { Glob } from "bun";

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

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

Glob は以下のインターフェースを実装するクラスです。

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

サポートされているグロブパターン

Bun は以下のグロブパターンをサポートしています。

? - 任意の単一文字に一致

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

* - パス区切り文字(/ または \)を除くゼロ個以上の文字に一致

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

** - / を含む任意の数の文字に一致

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

[ab] - 括弧内に含まれる文字の 1 つ、および文字範囲に一致

ts
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])。

ts
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} - 指定されたパターンのいずれかに一致

ts
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 レベル)、上記のワイルドカードを含めることができます。

! - パターンの先頭で結果を否定

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

\ - 上記の特殊文字をエスケープ

ts
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() 関数も実装しています。

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

// パターンの配列
const files = await promises.glob(["**/*.ts", "**/*.js"]);

// 除外パターン
const filtered = await promises.glob("**/*", {
  exclude: ["node_modules/**", "*.test.*"],
});

3 つの関数すべて(fs.glob()fs.globSync()fs.promises.glob())が以下をサポートしています。

  • 最初の引数としてのパターンの配列
  • 結果をフィルタリングするための exclude オプション

Bun by www.bunjs.com.cn 編集