ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

如何用 Bun Glob 扫描目录文件并匹配 glob 模式

如何用 Bun Glob 扫描目录文件并匹配 glob 模式 如何用 Bun Glob 扫描目录文件并匹配 glob 模式【免费下载链接】bunIncredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one项目地址: https://gitcode.com/GitHub_Trending/bu/bun如果你的任务是遍历一个目录找出符合条件的文件例如项目里所有.ts文件或者判断某个路径是否符合给定的通配规则可以用 Bun 内置的原生 glob 实现Glob类完成它对目录做的是真正的扫描scan对字符串做的是纯匹配match两者共用同一套 glob 模式语法。本文的前提是 Bun 已安装并可在PATH中找到参见 安装文档脚本用bun run运行即可无需额外安装依赖。完整 API 说明见 docs/runtime/glob.mdx行为对应的测试在 test/js/bun/glob/scan.test.ts 和 test/js/bun/glob/match.test.ts。准备条件Bun 已安装并在PATH中Quickstart 的前置要求。一个包含待扫描文件的目录。下文示例以当前工作目录.作为扫描根目录。新建一个 TypeScript 或 JavaScript 脚本文件例如scan.ts用bun run scan.ts执行。扫描目录文件Glob的scan方法返回可迭代对象逐条产出匹配到的路径scanSync是同步版本。下面的示例递归扫描当前工作目录及其所有子目录找出*.ts文件import { Glob } from bun; const glob new Glob(**/*.ts); // Scans the current working directory and each of its sub-directories recursively for await (const file of glob.scan(.)) { console.log(file); // 文档示例输出如 index.ts }同步遍历改用scanSync返回值为Iterablestringimport { Glob } from bun; const glob new Glob(**/*.ts); for (const file of glob.scanSync(.)) { console.log(file); }scan/scanSync的root参数可以传目录路径字符串也可以传一个ScanOptions对象来控制扫描行为。文档给出的接口与选项如下各选项的default即默认值class Glob { scan(root: string | ScanOptions): AsyncIterablestring; scanSync(root: string | ScanOptions): Iterablestring; match(path: string): boolean; } interface ScanOptions { cwd?: string; // 扫描根目录默认 process.cwd() dot?: boolean; // 是否允许匹配以 . 开头的条目默认 false absolute?: boolean; // 是否返回绝对路径默认 false followSymlinks?: boolean; // 是否遍历符号链接目录默认 false throwErrorOnBrokenSymlink?: boolean; // 遇到断裂符号链接是否抛错默认 false onlyFiles?: boolean; // 只返回文件默认 true }几个常用组合// 只扫描指定目录不递归进入符号链接目录 const glob new Glob(**/*.ts); for await (const file of glob.scan({ cwd: ./src })) { console.log(file); } // 返回绝对路径 const glob2 new Glob(**/*.js, { absolute: true }); for await (const file of glob2.scan(.)) { console.log(file); } // 默认只返回文件onlyFiles 默认 true要连目录一起列出显式关掉它 const glob3 new Glob(*, { onlyFiles: false }); for (const file of glob3.scanSync(.)) { console.log(file); }用 glob.match 匹配字符串不在文件系统中扫描而只是判断一个路径是否符合模式时用match方法返回boolean。文档给出的示例返回值均为文档示例import { Glob } from bun; const glob new Glob(*.ts); glob.match(index.ts); // true glob.match(index.js); // falseBun 支持的 glob 模式及文档示例如下可直接用于设计匹配规则?— 匹配任意单个字符const glob new Glob(???.ts); glob.match(foo.ts); // true glob.match(foobar.ts); // false*— 匹配零个或多个字符但不跨路径分隔符/或\const glob new Glob(*.ts); glob.match(index.ts); // true glob.match(src/index.ts); // false**— 匹配任意数量的字符包括/const glob new Glob(**/*.ts); glob.match(index.ts); // true glob.match(src/index.ts); // true glob.match(src/index.js); // false[ab]— 匹配括号内的字符之一也支持字符区间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]。文档示例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}— 匹配给定模式中的任意一个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 层且内部可以包含前面介绍过的任意通配符。!— 在模式开头取反结果const glob new Glob(!index.ts); glob.match(index.ts); // false glob.match(foo.ts); // true\— 转义上述特殊字符const glob new Glob(\\!index.ts); glob.match(!index.ts); // true glob.match(index.ts); // false这里\\!index.ts是 TypeScript 字符串字面量实际模式是\!index.ts匹配的文件名以字面!开头。可选分支Node.jsfs.glob()兼容写法如果代码原本按 Node.js 的fs.glob()编写Bun 也实现了这组函数可以不改写法直接使用。第一参数支持传入模式数组选项支持exclude过滤结果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.*], }), );文档说明fs.glob()、fs.globSync()、fs.promises.glob()三个函数都支持第一参数传模式数组exclude选项过滤结果。这条路径适合把 Node 项目里的 glob 调用平移到 Bun 环境。运行与验证在一个包含若干.ts文件的目录中把上面「扫描目录文件」一节的示例存为scan.ts然后执行bun run scan.ts预期现象以当前目录实际存在的文件为准脚本会逐行打印匹配到的路径。文档 Quickstart 中给出的console.log(file); // index.ts是文档示例输出表示工作目录下存在index.ts时的单行打印效果不是固定预期结果。判断匹配规则是否生效用match断言最直观对同一模式分别传入应命中与不应命中的路径返回true/false与文档示例一致即说明模式写对了例如*.ts对index.ts返回true对index.js返回false。限制与边界dot默认false以.开头的条目默认不会匹配需要时显式传dot: true。onlyFiles默认truescan默认只返回文件不返回目录。followSymlinks默认false不会遍历符号链接指向的子目录throwErrorOnBrokenSymlink默认false遇到断裂符号链接不抛错需要抛错时显式开启。cwd默认process.cwd()不传ScanOptions时扫描根目录就是进程当前工作目录。花括号模式{a,b,c}嵌套上限为 10 层。!取反只作用于模式开头*不跨路径分隔符跨目录递归必须用**。如果项目里需要把 glob 用于测试文件的并行分组等场景可参考 docs/guides/test/concurrent-test-glob.mdx 中的 glob 用法说明。【免费下载链接】bunIncredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one项目地址: https://gitcode.com/GitHub_Trending/bu/bun创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表