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

문자열을 glob 패턴과 일치시키기:

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

지원되는 Glob 패턴

Bun 은 다음 glob 패턴을 지원합니다:

? - 단일 문자 일치

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

* - 경로 구분자 (/ 또는 \) 를 제외한 0 개 이상의 문자 일치

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] - 대괄호 안에 포함된 문자 중 하나 또는 문자 범위 일치

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.*"],
});

세 함수 모두 (fs.glob(), fs.globSync(), fs.promises.glob()) 다음을 지원합니다:

  • 첫 번째 인수로 패턴 배열
  • 결과를 필터링하기 위한 exclude 옵션

Bun by www.bunjs.com.cn 편집