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

* - يطابق صفرًا أو أكثر من الأحرف، باستثناء فواصل المسار (/ أو \)

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

التوافق مع fs.glob() في Node.js

يُنفذ Bun أيضًا دوال fs.glob() في Node.js مع ميزات إضافية:

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 بواسطة www.bunjs.com.cn تحرير