UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

99 lines (96 loc) 3.67 kB
import { readdir, realpath, stat } from 'node:fs/promises'; import { resolve, join, normalize, basename } from '@visulima/path'; import { toPath } from '@visulima/path/utils'; import WalkError from './WalkError-H9EuRv7O.mjs'; import assertValidFileOrDirectoryPath from './assertValidFileOrDirectoryPath-BWWgA1wj.mjs'; import { g as globToRegExp, w as walkInclude } from './walk-include-CMUSRa86.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const _createWalkEntry = /* @__PURE__ */ __name(async (path) => { const normalizePath = normalize(path); const name = basename(normalizePath); const info = await stat(normalizePath); return { isDirectory: /* @__PURE__ */ __name(() => info.isDirectory(), "isDirectory"), isFile: /* @__PURE__ */ __name(() => info.isFile(), "isFile"), isSymbolicLink: /* @__PURE__ */ __name(() => info.isSymbolicLink(), "isSymbolicLink"), name, path: normalizePath }; }, "_createWalkEntry"); async function* walk(directory, { extensions, followSymlinks = false, includeDirs: includeDirectories = true, includeFiles = true, includeSymlinks = true, match, maxDepth = Number.POSITIVE_INFINITY, skip } = {}) { assertValidFileOrDirectoryPath(directory); if (maxDepth < 0) { return; } const mappedMatch = match ? match.map((pattern) => typeof pattern === "string" ? globToRegExp(pattern) : pattern) : void 0; const mappedSkip = skip ? skip.map((pattern) => typeof pattern === "string" ? globToRegExp(pattern) : pattern) : void 0; directory = resolve(toPath(directory)); if (includeDirectories && walkInclude(directory, extensions, mappedMatch, mappedSkip)) { yield await _createWalkEntry(directory); } if (maxDepth < 1 || !walkInclude(directory, void 0, void 0, mappedSkip)) { return; } try { for await (const entry of await readdir(directory, { withFileTypes: true })) { let path = join(directory, entry.name); if (entry.isSymbolicLink()) { if (followSymlinks) { path = await realpath(path); } else if (includeSymlinks && walkInclude(path, extensions, mappedMatch, mappedSkip)) { yield { // eslint-disable-next-line @typescript-eslint/unbound-method isDirectory: entry.isDirectory, // eslint-disable-next-line @typescript-eslint/unbound-method isFile: entry.isFile, // eslint-disable-next-line @typescript-eslint/unbound-method isSymbolicLink: entry.isSymbolicLink, name: entry.name, path }; } else { continue; } } if (entry.isSymbolicLink() || entry.isDirectory()) { yield* walk(path, { extensions, followSymlinks, includeDirs: includeDirectories, includeFiles, includeSymlinks, match: mappedMatch, maxDepth: maxDepth - 1, skip: mappedSkip }); } else if (entry.isFile() && includeFiles && walkInclude(path, extensions, mappedMatch, mappedSkip)) { yield { isDirectory: /* @__PURE__ */ __name(() => entry.isDirectory(), "isDirectory"), isFile: /* @__PURE__ */ __name(() => entry.isFile(), "isFile"), isSymbolicLink: /* @__PURE__ */ __name(() => entry.isSymbolicLink(), "isSymbolicLink"), name: entry.name, path }; } } } catch (error) { if (error instanceof WalkError) { throw error; } throw new WalkError(error, directory); } } __name(walk, "walk"); export { walk as default };