UNPKG

@bleed-believer/path-alias

Version:
51 lines (50 loc) 1.72 kB
import { join } from "path"; import { ExtParser } from "./tool/ext-parser/index.js"; import { TsConfig } from "./tool/ts-config/index.js"; /** * Determines whether the provided URL corresponds to a TypeScript source file. * * @param url - The URL or file path to check. * @returns `true` if the URL corresponds to a TypeScript file, `false` otherwise. * * @example * ```ts * // In a TypeScript file * const isTs = isSourceCode('/path/to/file.ts'); * console.log(isTs); // Output: true * * // In a JavaScript file * const isTs = isSourceCode('/path/to/file.js'); * console.log(isTs); // Output: false * ``` */ export function isSourceCode(url) { const extParser = new ExtParser(url); return extParser.isTs(); } let tsConfig; export function pathResolve(input, options) { if (!tsConfig) { tsConfig = TsConfig.load(); } const rootDir = join(process.cwd(), tsConfig.rootDir); const outDir = join(process.cwd(), tsConfig.outDir); const isTs = typeof options?.url == 'string' ? isSourceCode(options.url) : true; const result = tsConfig.resolveAll(input).map((x)=>{ if (typeof options?.url == 'string' && !isSourceCode(options?.url)) { const especifier = new ExtParser(x).toJs(); return especifier.replace(rootDir, outDir); } else { return x; } }); if (result.length === 0) { const specifier = new ExtParser(input); const basePath = isTs ? join(process.cwd(), tsConfig.rootDir, specifier.toTs()) : join(process.cwd(), tsConfig.outDir, specifier.toJs()); result.push(basePath); } if (!options?.multi) { return result[0]; } else { return result; } }