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ACrB,CAAC;QAEF,IAAI,IAAI,KAAK,QAAQ,EAAE;YACrB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,6BAA6B,EAAE,IAAI,CAAC,CAAC;YACzD,MAAM,aAAG,CAAC,SAAS,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;YAC5C,OAAO,IAAI,CAAC;SACb;QACD,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,gCAAgC,EAAE,IAAI,CAAC,CAAC;QAC5D,OAAO,KAAK,CAAC;IACf,CAAC;CAAA;AAvBD,oCAuBC;AAUD,SAAgB,kBAAkB,CAChC,MAAe,EACf,IAAY,EACZ,IAAY,EACZ,eAAyB,EACzB,oBAA6B;IAE7B,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;QACpC,IAAI,GAAG,IAAA,gCAAwB,EAAC,IAAI,EAAE,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CACnD,QAAQ,CAAC;YACP,IAAI;YACJ,IAAI;YACJ,MAAM;SACP,CAAC,CACH,CAAC;IACJ,CAAC,CAAC,CAAC;IAIH,IAAI,eAAe,EAAE;QACnB,IAAI,GAAG,IAAA,8BAAsB,EAAC,IAAI,EAAE,IAAI,EAAE,oBAAoB,CAAC,CAAC;KACjE;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAxBD,gDAwBC","sourcesContent":["/**\n * @file\n *\n * This file has all helperfunctions related to replacing.\n */\n\n/** */\nimport { existsSync, promises as fsp } from 'fs';\nimport { Dir } from 'mylas';\nimport { isAbsolute, join } from 'path';\nimport { IConfig, ReplacerOptions } from '../interfaces';\nimport { replaceSourceImportPaths, resolveFullImportPaths } from '../utils';\nimport normalizePath = require('normalize-path');\n\n/**\n * importReplacers imports replacers for tsc-alias to use.\n * @param {IConfig} config the tsc-alias config object.\n * @param {ReplacerOptions} replacers the tsc-alias replacer options.\n * @param {string[]} cmdReplacers array of filepaths to replacers from command-line.\n */\nexport async function importReplacers(\n config: IConfig,\n replacers: ReplacerOptions,\n cmdReplacers?: string[]\n) {\n config.output.debug('Started loading replacers');\n const dir = process.cwd();\n const node_modules: string[] = Dir.nodeModules({ cwd: dir });\n config.output.debug('Found node_modules:', node_modules);\n\n // List of default replacers.\n const defaultReplacers: ReplacerOptions = {\n default: {\n enabled: true\n },\n 'base-url': {\n enabled: !!config.baseUrl\n }\n };\n\n // List of all replacers.\n let merged: ReplacerOptions = {\n ...defaultReplacers,\n ...replacers\n };\n\n // Added replacers to list from command-line filepaths.\n config.output.debug(\n 'Added replacers to list from command-line filepaths:',\n cmdReplacers\n );\n cmdReplacers?.forEach((v) => {\n merged[v] = {\n enabled: true,\n file: v\n };\n });\n\n config.output.debug('Reading replacers config');\n const entries = Object.entries(merged);\n for await (const replacer of entries) {\n if (replacer[1].enabled) {\n // Importing default replacers.\n if (Object.keys(defaultReplacers).includes(replacer[0])) {\n config.output.debug('Loading default replacer:', replacer);\n const replacerModule = await import(\n `../replacers/${replacer[0]}.replacer`\n );\n config.replacers.push(replacerModule.default);\n }\n\n const file = replacer[1]?.file;\n if (!file) {\n config.output.debug('Replacer has no file:', replacer);\n continue; // When file is undefined don't try to import.\n }\n // Try to import replacer.\n const tryImportReplacer = async (targetPath: string) => {\n const replacerModule = await import(targetPath);\n config.output.debug('Imported replacerModule:', replacerModule);\n const replacerFunction = replacerModule.default;\n if (typeof replacerFunction == 'function') {\n config.replacers.push(replacerFunction);\n config.output.info(`Added replacer \"${file}\"`);\n } else {\n config.output.error(\n `Failed to import replacer \"${file}\", not in replacer format.`\n );\n }\n };\n\n // Look for replacer in cwd.\n const isRelativePath = !isAbsolute(file);\n const path = isRelativePath ? normalizePath(join(dir, file)) : file;\n\n if (existsSync(path)) {\n try {\n await tryImportReplacer(path);\n config.output.debug('Imported replacer:', path);\n continue;\n } catch {}\n }\n\n // Look for replacer in node_modules.\n if (isRelativePath) {\n for (const targetPath of node_modules.map((v) => join(dir, v, file))) {\n try {\n await tryImportReplacer(targetPath);\n config.output.debug('Imported replacer:', targetPath);\n continue;\n } catch {}\n }\n }\n\n config.output.error(`Failed to import replacer \"${file}\"`);\n }\n }\n config.output.debug('Loaded replacers:', config.replacers);\n}\n\n/**\n * replaceAlias replaces aliases in file.\n * @param {IConfig} config configuration\n * @param {string} file file to replace aliases in.\n * @param {boolean} resolveFullPath if tsc-alias should resolve the full path\n * @returns {Promise} if something has been replaced.\n */\nexport async function replaceAlias(\n config: IConfig,\n file: string,\n resolveFullPath?: boolean,\n resolveFullExtension?: string\n): Promise {\n config.output.debug('Starting to replace file:', file);\n const code = await fsp.readFile(file, 'utf8');\n const tempCode = replaceAliasString(\n config,\n file,\n code,\n resolveFullPath,\n resolveFullExtension\n );\n\n if (code !== tempCode) {\n config.output.debug('replaced file with changes:', file);\n await fsp.writeFile(file, tempCode, 'utf8');\n return true;\n }\n config.output.debug('replaced file without changes:', file);\n return false;\n}\n\n/**\n * replaceAliasString replaces aliases in the given code content and returns the changed code.\n * @param {IConfig} config configuration\n * @param {string} file path of the file to replace aliases in.\n * @param {string} code contents of the file to replace aliases in.\n * @param {boolean} resolveFullPath if tsc-alias should resolve the full path\n * @returns {string} content of the file with any replacements possible applied.\n */\nexport function replaceAliasString(\n config: IConfig,\n file: string,\n code: string,\n resolveFullPath?: boolean,\n resolveFullExtension?: string\n): string {\n config.replacers.forEach((replacer) => {\n code = replaceSourceImportPaths(code, file, (orig) =>\n replacer({\n orig,\n file,\n config\n })\n );\n });\n\n // Fully resolve all import paths (not just aliased ones)\n // *after* the aliases are resolved\n if (resolveFullPath) {\n code = resolveFullImportPaths(code, file, resolveFullExtension);\n }\n\n return code;\n}\n"]}