UNPKG

ttsc

Version:

General-purpose TypeScript-Go compiler, runtime, plugin host, and LSP host.

103 lines 4.22 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveSingleFileOutput = resolveSingleFileOutput; const node_path_1 = __importDefault(require("node:path")); const readProjectConfig_1 = require("../../compiler/internal/project/readProjectConfig"); const schema_1 = require("../../flags/schema"); /** * Resolves the only file that positional `ttsc <source>` can materialize in the * user's tree. * * The compiler itself emits into a private temporary directory. The launcher * then copies one transformed JavaScript file to this path, so project-mode * declaration, map, build-info, outFile, and broad outDir products are not * positional outputs. */ function resolveSingleFileOutput(options) { const project = readProjectSettings(options); const extension = singleFileJavaScriptExtension(options.file, passthroughStringOption(options.passthrough, "--jsx") ?? project?.jsx); const jsBasename = node_path_1.default.basename(options.file).replace(/\.(?:[cm]?tsx?|jsx)$/i, "") + extension; if (options.cliOutDir) { const relative = node_path_1.default.relative(options.cwd, options.file); const jsRelative = relative.slice(0, relative.length - node_path_1.default.extname(relative).length) + extension; return node_path_1.default.resolve(options.cwd, options.cliOutDir, jsRelative); } if (project?.outDir !== undefined) { const fromRoot = node_path_1.default.relative(project.rootDir, options.file); if (fromRoot !== "" && !isOutsideSingleFileLayout(fromRoot)) { const jsRelative = fromRoot.slice(0, fromRoot.length - node_path_1.default.extname(fromRoot).length) + extension; return node_path_1.default.resolve(project.outDir, jsRelative); } return node_path_1.default.resolve(project.outDir, jsBasename); } return options.file.replace(/\.(?:[cm]?tsx?|jsx)$/i, extension); } function readProjectSettings(options) { try { const project = (0, readProjectConfig_1.readProjectConfig)({ cwd: options.cwd, file: options.file, tsconfig: options.tsconfig, }); const outDir = project.compilerOptions.outDir; const rawRoot = project.compilerOptions.rootDir; const rootDir = typeof rawRoot === "string" && rawRoot.length !== 0 ? node_path_1.default.isAbsolute(rawRoot) ? rawRoot : node_path_1.default.resolve(project.root, rawRoot) : project.root; const rawJsx = project.compilerOptions.jsx; return { jsx: typeof rawJsx === "string" && rawJsx.length !== 0 ? rawJsx.toLowerCase() : undefined, outDir: typeof outDir === "string" && outDir.length !== 0 ? outDir : undefined, rootDir, }; } catch { return null; } } function isOutsideSingleFileLayout(relative) { return (relative === ".." || relative.startsWith(`..${node_path_1.default.sep}`) || node_path_1.default.isAbsolute(relative)); } function singleFileJavaScriptExtension(file, jsx) { switch (node_path_1.default.extname(file).toLowerCase()) { case ".mts": return ".mjs"; case ".cts": return ".cjs"; case ".tsx": case ".jsx": return jsx === "preserve" ? ".jsx" : ".js"; default: return ".js"; } } function passthroughStringOption(tokens, name) { let value; for (let index = 0; index < (tokens?.length ?? 0); index++) { const token = tokens?.[index]; if (token === undefined || token.includes("=") || (0, schema_1.resolveFlagSpec)(token)?.name !== (0, schema_1.resolveFlagSpec)(name)?.name) { continue; } const next = tokens?.[index + 1]; if (next !== undefined) { value = next.toLowerCase(); index++; } } return value; } //# sourceMappingURL=singleFileOutput.js.map