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aACV;SACF;QACD,IAAI,WAAW,GAAG,KAAK,CAAC;QACxB,IAAI;YACF,MAAM,KAAK,GAAG,IAAA,cAAS,EAAC,UAAU,CAAC,CAAC;YACpC,WAAW,GAAG,KAAK,CAAC,WAAW,EAAE,IAAI,KAAK,CAAC,cAAc,EAAE,CAAC;SAC7D;QAAC,OAAO,GAAG,EAAE,GAAE;QAChB,IAAI,WAAW,EAAE;YACf,OAAO,IAAA,WAAI,EAAC,UAAU,EAAE,eAAe,CAAC,CAAC;SAC1C;aAAM;YAEL,IAAI,IAAA,eAAU,EAAC,GAAG,UAAU,OAAO,CAAC,EAAE;gBACpC,OAAO,GAAG,UAAU,OAAO,CAAC;aAC7B;SACF;KACF;AACH,CAAC;AA7BD,gEA6BC","sourcesContent":["/**\n * @file\n *\n * This file has all helperfunctions related to configuration.\n */\n\n/** */\nimport { existsSync, lstatSync } from 'fs';\nimport { Dir, Json } from 'mylas';\nimport { basename, dirname, isAbsolute, join, resolve } from 'path';\nimport {\n IConfig,\n IOutput,\n IProjectConfig,\n IRawTSConfig,\n ITSConfig,\n ReplaceTscAliasPathsOptions\n} from '../interfaces';\nimport { Output, PathCache, TrieNode } from '../utils';\nimport { importReplacers } from './replacers';\nimport normalizePath = require('normalize-path');\n\n/**\n * prepareConfig prepares a IConfig object for tsc-alias to be used.\n * @param {ReplaceTscAliasPathsOptions} options options that are used to prepare a config object.\n * @returns {Promise} a promise of a IConfig object.\n */\nexport async function prepareConfig(\n options: ReplaceTscAliasPathsOptions\n): Promise {\n const output = options.output ?? new Output(options.verbose, options.debug);\n\n const configFile = !options.configFile\n ? resolve(process.cwd(), 'tsconfig.json')\n : !isAbsolute(options.configFile)\n ? resolve(process.cwd(), options.configFile)\n : options.configFile;\n\n output.assert(existsSync(configFile), `Invalid file path => ${configFile}`);\n\n const {\n baseUrl = '',\n outDir,\n declarationDir,\n paths,\n replacers,\n resolveFullPaths,\n verbose,\n fileExtensions\n } = loadConfig(configFile, output);\n\n if (options?.fileExtensions?.inputGlob) {\n fileExtensions.inputGlob = options.fileExtensions.inputGlob;\n }\n if (options?.fileExtensions?.outputCheck) {\n fileExtensions.outputCheck = options.fileExtensions.outputCheck;\n }\n\n output.verbose = verbose;\n\n if (options.resolveFullPaths || resolveFullPaths) {\n output.debug('resolveFullPaths is active');\n options.resolveFullPaths = true;\n }\n\n const _outDir = options.outDir ?? outDir;\n if (declarationDir && _outDir !== declarationDir) {\n options.declarationDir ??= declarationDir;\n }\n\n output.assert(_outDir, 'compilerOptions.outDir is not set');\n\n const configDir: string = normalizePath(dirname(configFile));\n\n // config with project details and paths\n const projectConfig: IProjectConfig = {\n configFile: configFile,\n baseUrl: baseUrl,\n outDir: _outDir,\n configDir: configDir,\n outPath: _outDir,\n confDirParentFolderName: basename(configDir),\n hasExtraModule: false,\n configDirInOutPath: null,\n relConfDirPathInOutPath: null,\n pathCache: new PathCache(!options.watch, fileExtensions?.outputCheck),\n inputGlob:\n fileExtensions?.inputGlob || '{mjs,cjs,js,jsx,d.{mts,cts,ts,tsx}}'\n };\n output.debug('loaded project config:', projectConfig);\n\n const config: IConfig = {\n ...projectConfig,\n output: output,\n aliasTrie:\n options.aliasTrie ?? TrieNode.buildAliasTrie(projectConfig, paths),\n replacers: []\n };\n output.debug('loaded full config:', config);\n\n // Import replacers.\n await importReplacers(config, replacers, options.replacers);\n return config;\n}\n\n/**\n * loadConfig loads a config file from fs.\n * @param {string} file file path to the config file that will be loaded.\n * @param {IOutput} output the output instance to log error to.\n * @returns {ITSConfig} a ITSConfig object\n */\nexport const loadConfig = (file: string, output: IOutput): ITSConfig => {\n if (!existsSync(file)) {\n output.error(`File ${file} not found`, true);\n }\n output.debug('Loading config file:', file);\n const {\n extends: ext,\n compilerOptions: { baseUrl, outDir, declarationDir, paths } = {\n baseUrl: undefined,\n outDir: undefined,\n declarationDir: undefined,\n paths: undefined\n },\n 'tsc-alias': TSCAliasConfig\n } = Json.loadS(file, true);\n\n const configDir = dirname(file);\n output.debug('configDir', configDir);\n const config: ITSConfig = {};\n\n if (baseUrl) config.baseUrl = baseUrl;\n if (outDir) {\n config.outDir = isAbsolute(outDir) ? outDir : join(configDir, outDir);\n }\n if (paths) config.paths = paths;\n if (declarationDir) {\n config.declarationDir = isAbsolute(declarationDir)\n ? declarationDir\n : join(configDir, declarationDir);\n }\n if (TSCAliasConfig?.replacers) {\n config.replacers = TSCAliasConfig.replacers;\n }\n if (TSCAliasConfig?.resolveFullPaths) {\n config.resolveFullPaths = TSCAliasConfig.resolveFullPaths;\n }\n if (TSCAliasConfig?.verbose) {\n config.verbose = TSCAliasConfig.verbose;\n }\n config.fileExtensions = TSCAliasConfig?.fileExtensions ?? {};\n\n const replacerFile = config.replacers?.pathReplacer?.file;\n\n if (replacerFile) {\n config.replacers.pathReplacer.file = join(configDir, replacerFile);\n }\n\n output.debug('loaded config (from file):', config);\n if (ext) {\n return {\n ...normalizeTsConfigExtendsOption(ext, file).reduce(\n (pre, ext) => ({\n ...pre,\n ...loadConfig(ext, output)\n }),\n {}\n ),\n ...config\n };\n }\n\n return config;\n};\n\n/**\n * normalizeTsConfigExtendsOption normalizes tsconfig extends option to a directly loadable path array\n * @param { string|string[] } ext\n * @param { string } file\n * @returns {string[]}\n */\nexport function normalizeTsConfigExtendsOption(\n ext: string | string[],\n file: string\n): string[] {\n if (!ext) return [];\n const configDir = dirname(file);\n const normExts = (Array.isArray(ext) ? ext : [ext]).map((e) =>\n e.startsWith('.')\n ? join(configDir, e.endsWith('.json') ? e : `${e}.json`)\n : resolveTsConfigExtendsPath(e, file)\n );\n return normExts;\n}\n\n/**\n * resolveTsConfigExtendsPath resolves the path to the config file that is being inherited.\n * @param {string} ext the value of the extends field in the loaded config file.\n * @param {string} file file path to the config file that was loaded.\n * @returns {string} a file path to the config file that is being inherited.\n */\nexport function resolveTsConfigExtendsPath(ext: string, file: string): string {\n const tsConfigDir = dirname(file);\n const node_modules: string[] = Dir.nodeModules({ cwd: tsConfigDir }); // Getting all node_modules directories.\n const targetPaths = node_modules.map((v) => join(tsConfigDir, v, ext)); // Mapping node_modules to target paths.\n\n // Recursively checking ancestor directories for tsconfig.\n for (const targetPath of targetPaths) {\n if (ext.endsWith('.json')) {\n // Check if the file exists.\n if (existsSync(targetPath)) {\n return targetPath;\n } else {\n continue; // Continue checking when ext is a file but not yet found.\n }\n }\n let isDirectory = false;\n try {\n const stats = lstatSync(targetPath);\n isDirectory = stats.isDirectory() || stats.isSymbolicLink();\n } catch (err) {}\n if (isDirectory) {\n return join(targetPath, 'tsconfig.json');\n } else {\n // When target is not a file nor directory check with '.json' extension.\n if (existsSync(`${targetPath}.json`)) {\n return `${targetPath}.json`;\n }\n }\n }\n}\n"]}