ttsc
Version:
General-purpose TypeScript-Go compiler, runtime, plugin host, and LSP host.
89 lines • 3.51 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runSingleFileEmit = runSingleFileEmit;
const node_fs_1 = __importDefault(require("node:fs"));
const node_os_1 = __importDefault(require("node:os"));
const node_path_1 = __importDefault(require("node:path"));
const readProjectConfig_1 = require("./project/readProjectConfig");
const resolveEmittedJavaScript_1 = require("./resolveEmittedJavaScript");
const runBuild_1 = require("./runBuild");
/**
* Emit one source file by building its project into a temporary directory.
*
* The full project is compiled with `forceListEmittedFiles` so that the emitted
* file list is available for `resolveEmittedJavaScript`. The temp directory is
* always cleaned up in the `finally` block, even on error.
*
* @returns The transformed JavaScript source text.
* @throws When the build exits non-zero or no output is produced for the file.
*/
function runSingleFileEmit(options) {
const cwd = node_path_1.default.resolve(options.cwd ?? process.cwd());
const sourceFile = realpathIfExists(node_path_1.default.isAbsolute(options.file)
? options.file
: node_path_1.default.resolve(cwd, options.file));
const project = (0, readProjectConfig_1.readProjectConfig)({
cwd,
file: options.file,
projectRoot: options.projectRoot,
tsconfig: options.tsconfig,
});
const tsconfig = project.path;
const projectRoot = project.root;
const outDir = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "ttsc-single-file-"));
try {
const result = (0, runBuild_1.runBuild)({
...options,
cwd,
emit: true,
forceListEmittedFiles: true,
isolateOutputsTo: outDir,
outDir,
resolvedProject: project,
tsconfig,
});
if (result.status !== 0) {
throw new Error("ttsc single-file emit exited " +
result.status +
"\n" +
(result.stderr || result.stdout));
}
const emitted = (0, resolveEmittedJavaScript_1.resolveEmittedJavaScript)({
emittedFiles: result.emittedFiles,
outDir,
projectRoot,
sourceFile,
});
if (emitted === null) {
throw new Error(`ttsc single-file emit: no output produced for ${sourceFile}`);
}
const transformed = node_fs_1.default.readFileSync(emitted, "utf8");
if (options.out) {
const target = node_path_1.default.isAbsolute(options.out)
? options.out
: node_path_1.default.resolve(cwd, options.out);
node_fs_1.default.mkdirSync(node_path_1.default.dirname(target), { recursive: true });
node_fs_1.default.writeFileSync(target, transformed, "utf8");
}
return transformed;
}
finally {
node_fs_1.default.rmSync(outDir, { recursive: true, force: true });
}
}
/**
* Resolve symlinks on `file` when it exists; return the original path when the
* file is not yet on disk (e.g. a synthetic path used in tests).
*/
function realpathIfExists(file) {
try {
return node_fs_1.default.realpathSync(file);
}
catch {
return file;
}
}
//# sourceMappingURL=runSingleFileEmit.js.map