UNPKG

ttsc

Version:

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

101 lines 4.13 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveTsgo = resolveTsgo; const node_fs_1 = __importDefault(require("node:fs")); const node_module_1 = require("node:module"); const node_path_1 = __importDefault(require("node:path")); /** * Resolve the consumer project's native TypeScript (`tsc`) binary and metadata. * * Resolution order: * * 1. `opts.binary` or `TTSC_TSGO_BINARY` env var — must be an existing absolute * path; returns `{ binary, packageJson: "", packageRoot, version: "custom" * }`. * 2. `typescript` resolved from the project `cwd`. * 3. `typescript` resolved from `opts.resolveFrom` (for test harnesses and * embedders that anchor to a different directory). * * Throws a descriptive error when the package or platform binary is missing so * callers never have to reason about undefined binary paths. */ function resolveTsgo(opts = {}) { const env = opts.env ?? process.env; const explicit = opts.binary ?? env.TTSC_TSGO_BINARY; if (explicit) { if (!node_path_1.default.isAbsolute(explicit) || !node_fs_1.default.existsSync(explicit)) { throw new Error(`ttsc: explicit tsgo binary must be an existing absolute path: ${explicit}`); } return { binary: explicit, packageJson: "", packageRoot: node_path_1.default.dirname(explicit), version: "custom", }; } const cwd = node_path_1.default.resolve(opts.cwd ?? process.cwd()); // Resolve the package.json of typescript. // Try cwd first, then the optional resolveFrom anchor. let packageJson; packageJson = resolveTypeScriptPackageJson(node_path_1.default.join(cwd, "package.json")) ?? (opts.resolveFrom ? resolveTypeScriptPackageJson(opts.resolveFrom) : undefined) ?? ""; if (!packageJson) { throw new Error([ "ttsc: typescript is required.", "Install the native TypeScript compiler in the consuming project:", " npm i -D typescript", ].join("\n")); } const manifest = readPackageJson(packageJson); const packageRoot = node_path_1.default.dirname(packageJson); const platformPackage = `@typescript/typescript-${process.platform}-${process.arch}`; const platformResolver = (0, node_module_1.createRequire)(packageJson); let platformPackageJson; try { platformPackageJson = platformResolver.resolve(`${platformPackage}/package.json`); } catch { throw new Error([ `ttsc: platform-specific TypeScript binary not found (${platformPackage}).`, "Reinstall typescript with optional dependencies enabled.", ].join("\n")); } const platformRoot = node_path_1.default.dirname(platformPackageJson); const binary = node_path_1.default.join(platformRoot, "lib", process.platform === "win32" ? "tsc.exe" : "tsc"); if (!node_fs_1.default.existsSync(binary)) { throw new Error(`ttsc: TypeScript executable not found: ${binary}`); } return { binary, gitHead: typeof manifest.gitHead === "string" ? manifest.gitHead : undefined, packageJson, packageRoot, platformPackageJson, version: typeof manifest.version === "string" ? manifest.version : "unknown", }; } /** * Attempt to resolve the `package.json` of `typescript` starting from `from` (a * package.json path or directory). Returns `undefined` when the package is not * resolvable from that location. */ function resolveTypeScriptPackageJson(from) { try { return (0, node_module_1.createRequire)(from).resolve("typescript/package.json"); } catch { return undefined; } } /** Parse a JSON file and return the result as a plain object. */ function readPackageJson(file) { return JSON.parse(node_fs_1.default.readFileSync(file, "utf8")); } //# sourceMappingURL=resolveTsgo.js.map