UNPKG

@openinc/parse-server-opendash

Version:
74 lines (73 loc) 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TreeNormalizer = void 0; /** Handles path normalization, root trimming and initial file object creation + filtering. */ class TreeNormalizer { static normalize(treeData, rootPath, fileFilter) { const normalizedRootPath = rootPath ? rootPath.replace(/^\/+/g, "").replace(/\/+$/g, "") : ""; const rootPrefix = normalizedRootPath ? normalizedRootPath + "/" : ""; const files = []; for (const item of treeData.tree) { if (item.type !== "blob") continue; if (normalizedRootPath && !item.path.startsWith(rootPrefix)) continue; let relativePath = item.path; if (normalizedRootPath) relativePath = item.path.substring(rootPrefix.length); if (!relativePath) continue; const pathParts = relativePath.split("/"); const fileName = pathParts[pathParts.length - 1]; const extension = fileName.includes(".") ? (fileName.split(".").pop() || "").toLowerCase() : ""; const baseName = extension ? fileName.slice(0, fileName.lastIndexOf(".")) : fileName; const file = { name: baseName, originalFilename: fileName, path: relativePath, sha: item.sha, size: item.size, extension, url: item.url, }; if (fileFilter && !this.shouldIncludeFile(file, fileFilter)) continue; files.push(file); } return { files, normalizedRootPath }; } static shouldIncludeFile(file, filter) { if (filter.excludeExtensions?.includes(file.extension)) return false; if (filter.includeExtensions?.length && !filter.includeExtensions.includes(file.extension)) return false; if (filter.excludePaths?.length) { for (const p of filter.excludePaths) if (this.matchesPattern(file.path, p)) return false; } if (filter.includePaths?.length) { let inc = false; for (const p of filter.includePaths) if (this.matchesPattern(file.path, p)) { inc = true; break; } if (!inc) return false; } return true; } static matchesPattern(path, pattern) { const regexPattern = pattern.replace(/\*/g, ".*").replace(/\?/g, "."); return new RegExp(`^${regexPattern}$`, "i").test(path); } } exports.TreeNormalizer = TreeNormalizer;