The Standard Library has been moved to JSR. See the blog post for details.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188// Documentation and interface for walk were adapted from Go// https://golang.org/pkg/path/filepath/#Walk// Copyright 2009 The Go Authors. All rights reserved. BSD license.import { assert } from "../_util/assert.ts";import { basename, join, normalize } from "../path/mod.ts";
export function createWalkEntrySync(path: string): WalkEntry { path = normalize(path); const name = basename(path); const info = Deno.statSync(path); return { path, name, isFile: info.isFile, isDirectory: info.isDirectory, isSymlink: info.isSymlink, };}
export async function createWalkEntry(path: string): Promise<WalkEntry> { path = normalize(path); const name = basename(path); const info = await Deno.stat(path); return { path, name, isFile: info.isFile, isDirectory: info.isDirectory, isSymlink: info.isSymlink, };}
export interface WalkOptions { maxDepth?: number; includeFiles?: boolean; includeDirs?: boolean; followSymlinks?: boolean; exts?: string[]; match?: RegExp[]; skip?: RegExp[];}
function include( path: string, exts?: string[], match?: RegExp[], skip?: RegExp[],): boolean { if (exts && !exts.some((ext): boolean => path.endsWith(ext))) { return false; } if (match && !match.some((pattern): boolean => !!path.match(pattern))) { return false; } if (skip && skip.some((pattern): boolean => !!path.match(pattern))) { return false; } return true;}
export interface WalkEntry extends Deno.DirEntry { path: string;}
/** Walks the file tree rooted at root, yielding each file or directory in the * tree filtered according to the given options. The files are walked in lexical * order, which makes the output deterministic but means that for very large * directories walk() can be inefficient. * * Options: * - maxDepth?: number = Infinity; * - includeFiles?: boolean = true; * - includeDirs?: boolean = true; * - followSymlinks?: boolean = false; * - exts?: string[]; * - match?: RegExp[]; * - skip?: RegExp[]; * * * for await (const entry of walk(".")) { * console.log(entry.path); * assert(entry.isFile); * } */export async function* walk( root: string, { maxDepth = Infinity, includeFiles = true, includeDirs = true, followSymlinks = false, exts = undefined, match = undefined, skip = undefined, }: WalkOptions = {},): AsyncIterableIterator<WalkEntry> { if (maxDepth < 0) { return; } if (includeDirs && include(root, exts, match, skip)) { yield await createWalkEntry(root); } if (maxDepth < 1 || !include(root, undefined, undefined, skip)) { return; } for await (const entry of Deno.readDir(root)) { if (entry.isSymlink) { if (followSymlinks) { // TODO(ry) Re-enable followSymlinks. throw new Error("unimplemented"); } else { continue; } }
assert(entry.name != null); const path = join(root, entry.name);
if (entry.isFile) { if (includeFiles && include(path, exts, match, skip)) { yield { path, ...entry }; } } else { yield* walk(path, { maxDepth: maxDepth - 1, includeFiles, includeDirs, followSymlinks, exts, match, skip, }); } }}
/** Same as walk() but uses synchronous ops */export function* walkSync( root: string, { maxDepth = Infinity, includeFiles = true, includeDirs = true, followSymlinks = false, exts = undefined, match = undefined, skip = undefined, }: WalkOptions = {},): IterableIterator<WalkEntry> { if (maxDepth < 0) { return; } if (includeDirs && include(root, exts, match, skip)) { yield createWalkEntrySync(root); } if (maxDepth < 1 || !include(root, undefined, undefined, skip)) { return; } for (const entry of Deno.readDirSync(root)) { if (entry.isSymlink) { if (followSymlinks) { throw new Error("unimplemented"); } else { continue; } }
assert(entry.name != null); const path = join(root, entry.name);
if (entry.isFile) { if (includeFiles && include(path, exts, match, skip)) { yield { path, ...entry }; } } else { yield* walkSync(path, { maxDepth: maxDepth - 1, includeFiles, includeDirs, followSymlinks, exts, match, skip, }); } }}