expandGlob

import { expandGlob } from "jsr:@ende/fs";

function

async function* expandGlob(glob: string | URL, options?: ExpandGlobOptions): AsyncIterableIterator<WalkEntry>

Returns an async iterator that yields each file path matching the given glob pattern.

The file paths are absolute paths. If root is not provided, the current working directory is used. The root directory is not included in the yielded file paths.

Requires --allow-read permission.

glob: string | URL

The glob pattern to expand.

options?: ExpandGlobOptions

Additional options for the expansion.

Returns

An async iterator that yields each walk entry matching the glob pattern.

Example: Basic usage

File structure:

folder
├── script.ts
└── foo.ts
// script.ts
import { expandGlob } from "@ende/fs/expand-glob";

await Array.fromAsync(expandGlob("*.ts"));
// [
//   {
//     path: "/Users/user/folder/script.ts",
//     name: "script.ts",
//     isFile: true,
//     isDirectory: false,
//     isSymlink: false,
//   },
//   {
//     path: "/Users/user/folder/foo.ts",
//     name: "foo.ts",
//     isFile: true,
//     isDirectory: false,
//     isSymlink: false,
//   },
// ]

Example: Define root directory

Setting the root option to /folder will expand the glob pattern from the /folder directory.

File structure:

folder
├── subdir
│   └── bar.ts
├── script.ts
└── foo.ts
// script.ts
import { expandGlob } from "@ende/fs/expand-glob";

await Array.fromAsync(expandGlob("*.ts", { root: "./subdir" }));
// [
//   {
//     path: "/Users/user/folder/subdir/bar.ts",
//     name: "bar.ts",
//     isFile: true,
//     isDirectory: false,
//     isSymlink: false,
//   },
// ]

Example: Exclude files

Setting the exclude option to ["foo.ts"] will exclude the foo.ts file from the expansion.

File structure:

folder
├── script.ts
└── foo.ts
// script.ts
import { expandGlob } from "@ende/fs/expand-glob";

await Array.fromAsync(expandGlob("*.ts", { exclude: ["foo.ts"] }));
// [
//   {
//     path: "/Users/user/folder/script.ts",
//     name: "true.ts",
//     isFile: false,
//     isDirectory: false,
//     isSymlink: false,
//   },
// ]

Example: Exclude directories

Setting the includeDirs option to false will exclude directories from the expansion.

File structure:

folder
├── subdir
│   └── bar.ts
├── script.ts
└── foo.ts
// script.ts
import { expandGlob } from "@ende/fs/expand-glob";

await Array.fromAsync(expandGlob("*", { includeDirs: false }));
// [
//   {
//     path: "/Users/user/folder/script.ts",
//     name: "script.ts",
//     isFile: true,
//     isDirectory: false,
//     isSymlink: false,
//   },
//   {
//     path: "/Users/user/folder/foo.ts",
//     name: "foo.ts",
//     isFile: true,
//     isDirectory: false,
//     isSymlink: false,
//   },
// ]

Example: Follow symbolic links

Setting the followSymlinks option to true will follow symbolic links.

File structure:

folder
├── script.ts
└── link.ts -> script.ts (symbolic link)
// script.ts
import { expandGlob } from "@ende/fs/expand-glob";

await Array.fromAsync(expandGlob("*.ts", { followSymlinks: true }));
// [
//   {
//     path: "/Users/user/folder/script.ts",
//     name: "script.ts",
//     isFile: true,
//     isDirectory: false,
//     isSymlink: false,
//   },
//   {
//     path: "/Users/user/folder/symlink",
//     name: "symlink",
//     isFile: true,
//     isDirectory: false,
//     isSymlink: true,
//   },
// ]

See also

Source