Function expandGlob

  • 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.

    Parameters

    • glob: string | URL

      The glob pattern to expand.

    • Optionaloptions: ExpandGlobOptions

      Additional options for the expansion.

    Returns AsyncIterableIterator<WalkEntry>

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

    https://docs.deno.com/runtime/manual/basics/permissions#file-system-access for more information on Deno's permissions system.

    File structure:

    folder
    ├── script.ts
    └── foo.ts
    // script.ts
    import { expandGlob } from "@std/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,
    // },
    // ]

    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 "@std/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,
    // },
    // ]

    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 "@std/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,
    // },
    // ]

    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 "@std/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,
    // },
    // ]