UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

104 lines (101 loc) 3.82 kB
import { readdirSync, realpathSync, statSync } from 'node:fs'; 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((path) => { const normalizePath = normalize(path); const info = statSync(normalizePath); return { // eslint-disable-next-line @typescript-eslint/unbound-method isDirectory: info.isDirectory, // eslint-disable-next-line @typescript-eslint/unbound-method isFile: info.isFile, // eslint-disable-next-line @typescript-eslint/unbound-method isSymbolicLink: info.isSymbolicLink, name: basename(normalizePath), path: normalizePath }; }, "_createWalkEntry"); function* walkSync(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 _createWalkEntry(directory); } if (maxDepth < 1 || !walkInclude(directory, void 0, void 0, mappedSkip)) { return; } try { for (const entry of readdirSync(directory, { withFileTypes: true })) { let path = join(directory, entry.name); if (entry.isSymbolicLink()) { if (followSymlinks) { path = realpathSync(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: normalize(path) }; } else { continue; } } if (entry.isSymbolicLink() || entry.isDirectory()) { yield* walkSync(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 { // 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: normalize(path) }; } } } catch (error) { if (error instanceof WalkError) { throw error; } throw new WalkError(error, directory); } } __name(walkSync, "walkSync"); export { walkSync as default };