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,eAAU,EAAC,IAAI,CAAC;YAChB,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CACrC,IAAA,eAAU,EAAC,GAAG,IAAI,IAAI,SAAS,EAAE,CAAC,CACnC,CACF,CAAC;IACJ,CAAC;CACF;AArHD,8BAqHC","sourcesContent":["/**\n * @file\n *\n * The PathCache class is meant to cache path lookups like\n * exists and getAbsoluteAliasPath.\n */\n\n/** */\nimport { existsSync } from 'fs';\nimport { join } from 'path';\n\nexport class PathCache {\n useCache: boolean;\n existsCache: Map;\n absoluteCache: Map;\n fileExtensions: string[];\n\n constructor(useCache: boolean, fileExtensions?: string[]) {\n this.fileExtensions = fileExtensions || [\n 'js',\n 'json',\n 'jsx',\n 'cjs',\n 'mjs',\n 'd.ts',\n 'd.tsx',\n 'd.cts',\n 'd.mts'\n ];\n this.useCache = useCache;\n if (useCache) {\n this.existsCache = new Map();\n this.absoluteCache = new Map();\n }\n }\n\n /**\n * existsResolvedAlias checks if file exists, uses cache when possible.\n * @param {string} path the filepath to check.\n * @returns {boolean} result of check.\n */\n public existsResolvedAlias(path: string): boolean {\n if (!this.useCache) return this.exists(path);\n if (this.existsCache.has(path)) {\n return this.existsCache.get(path);\n } else {\n const result = this.exists(path);\n this.existsCache.set(path, result);\n return result;\n }\n }\n\n /**\n * getAbsoluteAliasPath finds the absolute alias path, uses cache when possible.\n * @param {string} basePath the basepath of the alias.\n * @param {string} aliasPath the aliaspath of the alias.\n * @returns {string} the absolute alias path.\n */\n public getAbsoluteAliasPath(basePath: string, aliasPath: string): string {\n const request = { basePath, aliasPath };\n if (!this.useCache) return this.getAAP(request);\n if (this.absoluteCache.has(this.getCacheKey(request))) {\n return this.absoluteCache.get(this.getCacheKey(request));\n } else {\n const result = this.getAAP(request);\n this.absoluteCache.set(this.getCacheKey(request), result);\n return result;\n }\n }\n\n private getCacheKey({\n basePath,\n aliasPath\n }: {\n basePath: string;\n aliasPath: string;\n }): string {\n return `${basePath}___${aliasPath}`;\n }\n\n /**\n * getAAP finds the absolute alias path.\n * @param {string} basePath the basepath of the alias.\n * @param {string} aliasPath the aliaspath of the alias.\n * @returns {string} the absolute alias path.\n */\n private getAAP({\n basePath,\n aliasPath\n }: {\n basePath: string;\n aliasPath: string;\n }): string {\n const aliasPathParts = aliasPath\n .split('/')\n .filter((part) => !part.match(/^\\.$|^\\s*$/));\n\n let aliasPathPart = aliasPathParts.shift() || '';\n\n let pathExists = false;\n\n while (\n !(pathExists = this.exists(join(basePath, aliasPathPart))) &&\n aliasPathParts.length\n ) {\n aliasPathPart = aliasPathParts.shift();\n }\n\n if (pathExists) {\n return join(basePath, aliasPathPart, aliasPathParts.join('/'));\n }\n\n return '---' + join(basePath, aliasPathParts.join('/'));\n }\n\n /**\n * exists checks if file exists.\n * @param path the filepath to check.\n * @returns {boolean} result of check.\n */\n private exists(path: string): boolean {\n return (\n existsSync(path) ||\n this.fileExtensions.some((extension) =>\n existsSync(`${path}.${extension}`)\n )\n );\n }\n}\n"]}