UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

115 lines (108 loc) 3.87 kB
import { createRequire as __cjs_createRequire } from "node:module"; const __cjs_require = __cjs_createRequire(import.meta.url); const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process; const __cjs_getBuiltinModule = (module) => { // Check if we're in Node.js and version supports getBuiltinModule if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) { const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number); // Node.js 20.16.0+ and 22.3.0+ if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) { return __cjs_getProcess.getBuiltinModule(module); } } // Fallback to createRequire return __cjs_require(module); }; const { readdirSync, realpathSync, statSync } = __cjs_getBuiltinModule("node:fs"); import { resolve, join, normalize, basename } from '@visulima/path'; import { toPath } from '@visulima/path/utils'; import WalkError from './WalkError-DUdQd6FT.js'; import assertValidFileOrDirectoryPath from './assertValidFileOrDirectoryPath-8HANmVjk.js'; import { g as globToRegExp, w as walkInclude } from './walk-include-CZco7BvN.js'; const _createWalkEntry = (path) => { const normalizePath = normalize(path); const info = statSync(normalizePath); return { isDirectory: info.isDirectory, isFile: info.isFile, isSymbolicLink: info.isSymbolicLink, name: basename(normalizePath), path: normalizePath }; }; 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 { isDirectory: entry.isDirectory, isFile: entry.isFile, 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 { isDirectory: entry.isDirectory, isFile: entry.isFile, isSymbolicLink: entry.isSymbolicLink, name: entry.name, path: normalize(path) }; } } } catch (error) { if (error instanceof WalkError) { throw error; } throw new WalkError(error, directory); } } export { walkSync as default };