UNPKG

@plugjs/plug

Version:
99 lines (97 loc) 3.41 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // utils/walk.ts var walk_exports = {}; __export(walk_exports, { walk: () => walk }); module.exports = __toCommonJS(walk_exports); var import_node_path = require("node:path"); var import_fs = require("../fs.cjs"); var import_logging = require("../logging.cjs"); var import_paths = require("../paths.cjs"); var import_match = require("./match.cjs"); function walk(directory, globs, options = {}) { const { maxDepth = Infinity, followSymlinks = true, allowNodeModules = false, ...opts } = options; const onDirectory = (dir) => { if (dir === directory) return true; const name = (0, import_node_path.basename)(dir); if (name === "node_modules") return !!allowNodeModules; if (name.startsWith(".")) return !!opts.dot; return true; }; const positiveMatcher = (0, import_match.match)(globs, opts); import_logging.log.debug("Walking directory", (0, import_logging.$p)(directory), { globs, options }); return walker({ directory, relative: "", matcher: positiveMatcher, onDirectory, followSymlinks, maxDepth, depth: 0 }); } async function* walker(args) { const { directory, relative, matcher: positiveMatcher, onDirectory, followSymlinks, maxDepth, depth } = args; const dir = (0, import_paths.resolveAbsolutePath)(directory, relative); if (!onDirectory(dir)) return; import_logging.log.trace("Reading directory", (0, import_logging.$p)(dir)); let dirents; try { dirents = await (0, import_fs.opendir)(dir); } catch (error) { if (error.code !== "ENOENT") throw error; import_logging.log.warn("Directory", (0, import_logging.$p)(dir), "not found"); return; } for await (const dirent of dirents) { const path = (0, import_node_path.join)(relative, dirent.name); if (dirent.isFile() && positiveMatcher(path)) yield path; else if (dirent.isDirectory() && depth < maxDepth) { const children = walker({ ...args, relative: path, depth: depth + 1 }); for await (const child of children) yield child; } else if (dirent.isSymbolicLink() && followSymlinks) { const info = await (0, import_fs.stat)((0, import_node_path.join)(directory, path)); if (info.isFile() && positiveMatcher(path)) yield path; else if (info.isDirectory() && depth < maxDepth) { const children = walker({ ...args, relative: path, depth: depth + 1 }); for await (const child of children) yield child; } } } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { walk }); //# sourceMappingURL=walk.cjs.map