UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

38 lines (37 loc) 2.16 kB
import type { WalkEntry, WalkOptions } from "../types.d.ts"; /** * Asynchronously walks the file tree rooted at `directory`, yielding each file or directory that matches the criteria specified in `options`. * @param directory The root directory to start walking from. * @param options Optional configuration to control the walking process. See {@link WalkOptions}. * @param options.extensions List of file extensions used to filter entries. * @param options.followSymlinks Indicates whether symlinks should be resolved or not. * @param options.includeDirs Indicates whether directory entries should be included or not. * @param options.includeFiles Indicates whether file entries should be included or not. * @param options.includeSymlinks Indicates whether symlink entries should be included or not. * @param options.match List of regular expression or glob patterns used to filter entries. * @param options.maxDepth Maximum depth to walk. Defaults to infinity. * @param options.skip List of regular expression or glob patterns used to skip entries. * @returns An async iterable iterator yielding {@link WalkEntry} objects for each matching file or directory. * @example * ```javascript * import { walk } from "@visulima/fs"; * import { join } from "node:path"; * * const printEntries = async () => { * // Walk through /tmp/my-project, looking for .ts files, max depth 2 * for await (const entry of walk(join("/tmp", "my-project"), { extensions: ["ts"], maxDepth: 2 })) { * console.log(`Found: ${entry.path} (Type: ${entry.isFile() ? 'file' : 'directory'})`); * } * * // Walk, including only directories, and skip any node_modules folders * for await (const entry of walk(join("/tmp", "another-project"), { includeFiles: false, skip: [/node_modules/] })) { * if (entry.isDirectory()) { * console.log(`Directory: ${entry.path}`); * } * } * }; * * printEntries(); * ``` */ export default function walk(directory: URL | string, { extensions, followSymlinks, includeDirs: includeDirectories, includeFiles, includeSymlinks, match, maxDepth, skip, }?: WalkOptions): AsyncIterableIterator<WalkEntry>;