ttsc
Version:
General-purpose TypeScript-Go compiler, runtime, plugin host, and LSP host.
51 lines • 2.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveBinary = resolveBinary;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
/**
* Resolve the ttsc helper binary path, or null when unavailable.
*
* Resolution order:
*
* 1. `TTSC_BINARY` env var — must be an absolute path when set.
* 2. The per-platform npm package `@ttsc/<platform>-<arch>/bin/ttsc[.exe]`.
* 3. A `ttsc-native[.exe]` sibling of this package's root (dev/CI layout).
*/
function resolveBinary(opts = {}) {
const env = opts.env ?? process.env;
if (env.TTSC_BINARY && node_path_1.default.isAbsolute(env.TTSC_BINARY)) {
return env.TTSC_BINARY;
}
try {
return require.resolve(`@ttsc/${process.platform}-${process.arch}/bin/${process.platform === "win32" ? "ttsc.exe" : "ttsc"}`);
}
catch {
/* fall through */
}
const local = defaultLocalBinaryPath();
if (local)
return local;
return null;
}
/**
* Return the path to the dev-layout `ttsc-native[.exe]` binary adjacent to the
* package root, or null when the file does not exist.
*/
function defaultLocalBinaryPath() {
const root = packageRootDir();
const candidate = node_path_1.default.resolve(root, "..", "native", process.platform === "win32" ? "ttsc-native.exe" : "ttsc-native");
return node_fs_1.default.existsSync(candidate) ? candidate : null;
}
/**
* Return the real, absolute path of the `packages/ttsc` package root by walking
* two levels up from `__dirname` (which lives in `src/compiler/internal`).
*/
function packageRootDir() {
const moduleDir = node_path_1.default.resolve(__dirname, "..", "..");
return node_fs_1.default.realpathSync.native?.(moduleDir) ?? node_fs_1.default.realpathSync(moduleDir);
}
//# sourceMappingURL=resolveBinary.js.map