ttsc 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cmd/ttsc/api_compile.go +134 -0
- package/cmd/ttsc/build.go +1 -7
- package/cmd/ttsc/main.go +2 -2
- package/cmd/ttsc/transform.go +0 -6
- package/driver/program.go +11 -1
- package/lib/TtscCompiler.d.ts +76 -0
- package/lib/TtscCompiler.js +245 -0
- package/lib/TtscCompiler.js.map +1 -0
- package/lib/compiler/internal/buildNativeCompiler.d.ts +6 -0
- package/lib/compiler/internal/buildNativeCompiler.js +72 -0
- package/lib/compiler/internal/buildNativeCompiler.js.map +1 -0
- package/lib/compiler/internal/compileProjectInMemory.d.ts +7 -0
- package/lib/compiler/internal/compileProjectInMemory.js +176 -0
- package/lib/compiler/internal/compileProjectInMemory.js.map +1 -0
- package/lib/compiler/internal/project/readProjectConfig.d.ts +4 -0
- package/lib/{project.js → compiler/internal/project/readProjectConfig.js} +12 -82
- package/lib/compiler/internal/project/readProjectConfig.js.map +1 -0
- package/lib/compiler/internal/project/resolveProjectConfig.d.ts +3 -0
- package/lib/compiler/internal/project/resolveProjectConfig.js +94 -0
- package/lib/compiler/internal/project/resolveProjectConfig.js.map +1 -0
- package/lib/compiler/internal/resolveBinary.d.ts +4 -0
- package/lib/compiler/internal/resolveBinary.js +65 -0
- package/lib/compiler/internal/resolveBinary.js.map +1 -0
- package/lib/compiler/internal/resolveEmittedJavaScript.d.ts +7 -0
- package/lib/compiler/internal/resolveEmittedJavaScript.js +118 -0
- package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -0
- package/lib/compiler/internal/resolveTsgo.d.ts +20 -0
- package/lib/{tsgo.js → compiler/internal/resolveTsgo.js} +5 -10
- package/lib/compiler/internal/resolveTsgo.js.map +1 -0
- package/lib/compiler/internal/runBuild.d.ts +10 -0
- package/lib/compiler/internal/runBuild.js +480 -0
- package/lib/compiler/internal/runBuild.js.map +1 -0
- package/lib/compiler/internal/runTransform.d.ts +3 -0
- package/lib/compiler/internal/runTransform.js +100 -0
- package/lib/compiler/internal/runTransform.js.map +1 -0
- package/lib/index.d.ts +6 -15
- package/lib/index.js +6 -15
- package/lib/index.js.map +1 -1
- package/lib/launcher/internal/getCompilerVersionText.d.ts +3 -0
- package/lib/launcher/internal/getCompilerVersionText.js +71 -0
- package/lib/launcher/internal/getCompilerVersionText.js.map +1 -0
- package/lib/launcher/internal/prepareExecution.d.ts +10 -0
- package/lib/launcher/internal/prepareExecution.js +120 -0
- package/lib/launcher/internal/prepareExecution.js.map +1 -0
- package/lib/launcher/internal/runTtsc.d.ts +1 -0
- package/lib/{cli.js → launcher/internal/runTtsc.js} +48 -47
- package/lib/launcher/internal/runTtsc.js.map +1 -0
- package/lib/launcher/internal/runTtsx.d.ts +1 -0
- package/lib/{runner/cli.js → launcher/internal/runTtsx.js} +13 -35
- package/lib/launcher/internal/runTtsx.js.map +1 -0
- package/lib/launcher/ttsc.js +2 -2
- package/lib/launcher/ttsc.js.map +1 -1
- package/lib/launcher/ttsx.js +2 -2
- package/lib/launcher/ttsx.js.map +1 -1
- package/lib/plugin/internal/buildSourcePlugin.d.ts +12 -0
- package/lib/{source-build.js → plugin/internal/buildSourcePlugin.js} +77 -84
- package/lib/plugin/internal/buildSourcePlugin.js.map +1 -0
- package/lib/plugin/internal/loadProjectPlugins.d.ts +14 -0
- package/lib/{plugin.js → plugin/internal/loadProjectPlugins.js} +38 -76
- package/lib/plugin/internal/loadProjectPlugins.js.map +1 -0
- package/lib/structures/ITtscCompilerContext.d.ts +78 -0
- package/lib/structures/ITtscCompilerContext.js +3 -0
- package/lib/structures/ITtscCompilerContext.js.map +1 -0
- package/lib/structures/ITtscCompilerDiagnostic.d.ts +83 -0
- package/lib/structures/ITtscCompilerDiagnostic.js +3 -0
- package/lib/structures/ITtscCompilerDiagnostic.js.map +1 -0
- package/lib/structures/ITtscCompilerResult.d.ts +83 -0
- package/lib/structures/{ITtscCacheKeyInput.js → ITtscCompilerResult.js} +1 -1
- package/lib/structures/ITtscCompilerResult.js.map +1 -0
- package/lib/structures/ITtscPlugin.d.ts +52 -7
- package/lib/structures/ITtscPluginFactoryContext.d.ts +47 -0
- package/lib/structures/ITtscProjectPluginConfig.d.ts +41 -0
- package/lib/structures/TtscPluginStage.d.ts +15 -0
- package/lib/structures/{ITtscBuildResult.js → TtscPluginStage.js} +1 -1
- package/lib/structures/TtscPluginStage.js.map +1 -0
- package/lib/structures/index.d.ts +7 -0
- package/lib/structures/index.js +24 -0
- package/lib/structures/index.js.map +1 -0
- package/lib/structures/internal/ITtscLoadedNativePlugin.d.ts +13 -0
- package/lib/structures/internal/ITtscLoadedNativePlugin.js.map +1 -0
- package/lib/structures/internal/ITtscParsedProjectConfig.d.ts +15 -0
- package/lib/structures/internal/ITtscParsedProjectConfig.js.map +1 -0
- package/lib/structures/internal/ITtscProjectLocatorOptions.d.ts +9 -0
- package/lib/structures/internal/ITtscProjectLocatorOptions.js.map +1 -0
- package/lib/structures/internal/TtscBuildOptions.d.ts +18 -0
- package/lib/structures/{ITtscBuildOptions.js → internal/TtscBuildOptions.js} +1 -1
- package/lib/structures/internal/TtscBuildOptions.js.map +1 -0
- package/lib/structures/internal/TtscBuildResult.d.ts +14 -0
- package/lib/structures/{ITtscCheckOptions.js → internal/TtscBuildResult.js} +1 -1
- package/lib/structures/internal/TtscBuildResult.js.map +1 -0
- package/lib/structures/internal/TtscCommonOptions.d.ts +27 -0
- package/lib/structures/{ITtscNativeSource.js → internal/TtscCommonOptions.js} +1 -1
- package/lib/structures/internal/TtscCommonOptions.js.map +1 -0
- package/lib/structures/internal/TtscTransformOptions.d.ts +14 -0
- package/lib/structures/internal/TtscTransformOptions.js +3 -0
- package/lib/structures/internal/TtscTransformOptions.js.map +1 -0
- package/package.json +9 -9
- package/src/TtscCompiler.ts +241 -0
- package/src/compiler/internal/buildNativeCompiler.ts +43 -0
- package/src/compiler/internal/compileProjectInMemory.ts +189 -0
- package/src/{project.ts → compiler/internal/project/readProjectConfig.ts} +36 -111
- package/src/compiler/internal/project/resolveProjectConfig.ts +67 -0
- package/src/compiler/internal/resolveBinary.ts +41 -0
- package/src/compiler/internal/resolveEmittedJavaScript.ts +102 -0
- package/src/{tsgo.ts → compiler/internal/resolveTsgo.ts} +20 -23
- package/src/compiler/internal/runBuild.ts +644 -0
- package/src/compiler/internal/runTransform.ts +69 -0
- package/src/index.ts +6 -15
- package/src/launcher/internal/getCompilerVersionText.ts +44 -0
- package/src/launcher/internal/prepareExecution.ts +115 -0
- package/src/{cli.ts → launcher/internal/runTtsc.ts} +61 -70
- package/src/{runner/cli.ts → launcher/internal/runTtsx.ts} +21 -49
- package/src/launcher/ttsc.ts +2 -2
- package/src/launcher/ttsx.ts +2 -2
- package/src/{source-build.ts → plugin/internal/buildSourcePlugin.ts} +121 -115
- package/src/plugin/internal/loadProjectPlugins.ts +214 -0
- package/src/structures/ITtscCompilerContext.ts +84 -0
- package/src/structures/ITtscCompilerDiagnostic.ts +91 -0
- package/src/structures/ITtscCompilerResult.ts +94 -0
- package/src/structures/ITtscPlugin.ts +54 -7
- package/src/structures/ITtscPluginFactoryContext.ts +51 -0
- package/src/structures/ITtscProjectPluginConfig.ts +43 -0
- package/src/structures/TtscPluginStage.ts +15 -0
- package/src/structures/index.ts +7 -0
- package/src/structures/internal/ITtscLoadedNativePlugin.ts +14 -0
- package/src/structures/internal/ITtscParsedProjectConfig.ts +16 -0
- package/src/structures/internal/ITtscProjectLocatorOptions.ts +9 -0
- package/src/structures/internal/TtscBuildOptions.ts +19 -0
- package/src/structures/internal/TtscBuildResult.ts +15 -0
- package/src/structures/internal/TtscCommonOptions.ts +28 -0
- package/src/structures/internal/TtscTransformOptions.ts +15 -0
- package/test/compiler.test.ts +234 -0
- package/test/platform.test.ts +1 -45
- package/test/project.test.ts +3 -1
- package/test/source-build.test.ts +33 -220
- package/test/tsgo.test.ts +1 -8
- package/lib/api.d.ts +0 -40
- package/lib/api.js +0 -680
- package/lib/api.js.map +0 -1
- package/lib/cli.d.ts +0 -1
- package/lib/cli.js.map +0 -1
- package/lib/native.d.ts +0 -8
- package/lib/native.js +0 -42
- package/lib/native.js.map +0 -1
- package/lib/platform.d.ts +0 -52
- package/lib/platform.js +0 -175
- package/lib/platform.js.map +0 -1
- package/lib/plugin.d.ts +0 -13
- package/lib/plugin.js.map +0 -1
- package/lib/project.d.ts +0 -14
- package/lib/project.js.map +0 -1
- package/lib/runner/cli.d.ts +0 -1
- package/lib/runner/cli.js.map +0 -1
- package/lib/runner/register.d.ts +0 -6
- package/lib/runner/register.js +0 -358
- package/lib/runner/register.js.map +0 -1
- package/lib/source-build.d.ts +0 -19
- package/lib/source-build.js.map +0 -1
- package/lib/structures/ITtscBuildOptions.d.ts +0 -14
- package/lib/structures/ITtscBuildOptions.js.map +0 -1
- package/lib/structures/ITtscBuildResult.d.ts +0 -6
- package/lib/structures/ITtscBuildResult.js.map +0 -1
- package/lib/structures/ITtscBuildSourcePluginOptions.d.ts +0 -8
- package/lib/structures/ITtscBuildSourcePluginOptions.js +0 -3
- package/lib/structures/ITtscBuildSourcePluginOptions.js.map +0 -1
- package/lib/structures/ITtscCacheKeyInput.d.ts +0 -7
- package/lib/structures/ITtscCacheKeyInput.js.map +0 -1
- package/lib/structures/ITtscCheckOptions.d.ts +0 -2
- package/lib/structures/ITtscCheckOptions.js.map +0 -1
- package/lib/structures/ITtscCommonOptions.d.ts +0 -22
- package/lib/structures/ITtscCommonOptions.js +0 -3
- package/lib/structures/ITtscCommonOptions.js.map +0 -1
- package/lib/structures/ITtscLoadPluginsOptions.d.ts +0 -8
- package/lib/structures/ITtscLoadPluginsOptions.js +0 -3
- package/lib/structures/ITtscLoadPluginsOptions.js.map +0 -1
- package/lib/structures/ITtscLoadedNativePlugin.d.ts +0 -7
- package/lib/structures/ITtscLoadedNativePlugin.js.map +0 -1
- package/lib/structures/ITtscLoadedPlugins.d.ts +0 -11
- package/lib/structures/ITtscLoadedPlugins.js +0 -3
- package/lib/structures/ITtscLoadedPlugins.js.map +0 -1
- package/lib/structures/ITtscNativeBackend.d.ts +0 -10
- package/lib/structures/ITtscNativeBackend.js +0 -3
- package/lib/structures/ITtscNativeBackend.js.map +0 -1
- package/lib/structures/ITtscNativePluginContractVersion.d.ts +0 -1
- package/lib/structures/ITtscNativePluginContractVersion.js +0 -3
- package/lib/structures/ITtscNativePluginContractVersion.js.map +0 -1
- package/lib/structures/ITtscNativePluginShape.d.ts +0 -8
- package/lib/structures/ITtscNativePluginShape.js +0 -3
- package/lib/structures/ITtscNativePluginShape.js.map +0 -1
- package/lib/structures/ITtscNativeRewriteMode.d.ts +0 -1
- package/lib/structures/ITtscNativeRewriteMode.js +0 -3
- package/lib/structures/ITtscNativeRewriteMode.js.map +0 -1
- package/lib/structures/ITtscNativeSource.d.ts +0 -4
- package/lib/structures/ITtscNativeSource.js.map +0 -1
- package/lib/structures/ITtscParsedProjectConfig.d.ts +0 -9
- package/lib/structures/ITtscParsedProjectConfig.js.map +0 -1
- package/lib/structures/ITtscPluginFactory.d.ts +0 -4
- package/lib/structures/ITtscPluginFactory.js +0 -3
- package/lib/structures/ITtscPluginFactory.js.map +0 -1
- package/lib/structures/ITtscPluginModule.d.ts +0 -3
- package/lib/structures/ITtscPluginModule.js +0 -3
- package/lib/structures/ITtscPluginModule.js.map +0 -1
- package/lib/structures/ITtscPreparedExecution.d.ts +0 -5
- package/lib/structures/ITtscPreparedExecution.js +0 -3
- package/lib/structures/ITtscPreparedExecution.js.map +0 -1
- package/lib/structures/ITtscProjectLocatorOptions.d.ts +0 -5
- package/lib/structures/ITtscProjectLocatorOptions.js.map +0 -1
- package/lib/structures/ITtscRegisterOptions.d.ts +0 -6
- package/lib/structures/ITtscRegisterOptions.js +0 -3
- package/lib/structures/ITtscRegisterOptions.js.map +0 -1
- package/lib/structures/ITtscResolveGoBinaryOptions.d.ts +0 -7
- package/lib/structures/ITtscResolveGoBinaryOptions.js +0 -3
- package/lib/structures/ITtscResolveGoBinaryOptions.js.map +0 -1
- package/lib/structures/ITtscResolveOptions.d.ts +0 -20
- package/lib/structures/ITtscResolveOptions.js +0 -3
- package/lib/structures/ITtscResolveOptions.js.map +0 -1
- package/lib/structures/ITtscResolvedTsgo.d.ts +0 -8
- package/lib/structures/ITtscResolvedTsgo.js +0 -3
- package/lib/structures/ITtscResolvedTsgo.js.map +0 -1
- package/lib/structures/ITtscTransformOptions.d.ts +0 -12
- package/lib/structures/ITtscTransformOptions.js +0 -3
- package/lib/structures/ITtscTransformOptions.js.map +0 -1
- package/lib/structures/ITtscTsgoResolveOptions.d.ts +0 -14
- package/lib/structures/ITtscTsgoResolveOptions.js +0 -3
- package/lib/structures/ITtscTsgoResolveOptions.js.map +0 -1
- package/lib/tsgo.d.ts +0 -6
- package/lib/tsgo.js.map +0 -1
- package/src/api.ts +0 -872
- package/src/native.ts +0 -64
- package/src/platform.ts +0 -151
- package/src/plugin.ts +0 -253
- package/src/runner/register.ts +0 -406
- package/src/structures/ITtscBuildOptions.ts +0 -19
- package/src/structures/ITtscBuildResult.ts +0 -6
- package/src/structures/ITtscBuildSourcePluginOptions.ts +0 -9
- package/src/structures/ITtscCacheKeyInput.ts +0 -7
- package/src/structures/ITtscCheckOptions.ts +0 -3
- package/src/structures/ITtscCommonOptions.ts +0 -23
- package/src/structures/ITtscLoadPluginsOptions.ts +0 -9
- package/src/structures/ITtscLoadedNativePlugin.ts +0 -8
- package/src/structures/ITtscLoadedPlugins.ts +0 -12
- package/src/structures/ITtscNativeBackend.ts +0 -11
- package/src/structures/ITtscNativePluginContractVersion.ts +0 -1
- package/src/structures/ITtscNativePluginShape.ts +0 -9
- package/src/structures/ITtscNativeRewriteMode.ts +0 -1
- package/src/structures/ITtscNativeSource.ts +0 -4
- package/src/structures/ITtscParsedProjectConfig.ts +0 -10
- package/src/structures/ITtscPluginFactory.ts +0 -7
- package/src/structures/ITtscPluginModule.ts +0 -4
- package/src/structures/ITtscPreparedExecution.ts +0 -5
- package/src/structures/ITtscProjectLocatorOptions.ts +0 -5
- package/src/structures/ITtscRegisterOptions.ts +0 -7
- package/src/structures/ITtscResolveGoBinaryOptions.ts +0 -7
- package/src/structures/ITtscResolveOptions.ts +0 -20
- package/src/structures/ITtscResolvedTsgo.ts +0 -8
- package/src/structures/ITtscTransformOptions.ts +0 -13
- package/src/structures/ITtscTsgoResolveOptions.ts +0 -14
- package/test/native.test.ts +0 -103
- /package/lib/structures/{ITtscLoadedNativePlugin.js → internal/ITtscLoadedNativePlugin.js} +0 -0
- /package/lib/structures/{ITtscParsedProjectConfig.js → internal/ITtscParsedProjectConfig.js} +0 -0
- /package/lib/structures/{ITtscProjectLocatorOptions.js → internal/ITtscProjectLocatorOptions.js} +0 -0
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as os from "node:os";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
|
|
6
|
+
import { loadProjectPlugins } from "../../plugin/internal/loadProjectPlugins";
|
|
7
|
+
import type { ITtscCompilerDiagnostic } from "../../structures/ITtscCompilerDiagnostic";
|
|
8
|
+
import type { ITtscLoadedNativePlugin } from "../../structures/internal/ITtscLoadedNativePlugin";
|
|
9
|
+
import type { TtscBuildOptions } from "../../structures/internal/TtscBuildOptions";
|
|
10
|
+
import type { TtscBuildResult } from "../../structures/internal/TtscBuildResult";
|
|
11
|
+
import type { TtscCommonOptions } from "../../structures/internal/TtscCommonOptions";
|
|
12
|
+
import { resolveProjectConfig } from "./project/resolveProjectConfig";
|
|
13
|
+
import { resolveBinary } from "./resolveBinary";
|
|
14
|
+
import { resolveTsgo } from "./resolveTsgo";
|
|
15
|
+
|
|
16
|
+
/** Merge spawn env without clobbering unrelated vars. */
|
|
17
|
+
function mergeEnv(extra?: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
|
|
18
|
+
const base = {
|
|
19
|
+
...process.env,
|
|
20
|
+
TTSC_NODE_BINARY: process.env.TTSC_NODE_BINARY ?? process.execPath,
|
|
21
|
+
};
|
|
22
|
+
if (!extra) return base;
|
|
23
|
+
return { ...base, ...extra };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function nativePluginEnv(
|
|
27
|
+
extra: NodeJS.ProcessEnv | undefined,
|
|
28
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
29
|
+
): NodeJS.ProcessEnv {
|
|
30
|
+
return mergeEnv({
|
|
31
|
+
TTSC_TSGO_BINARY: process.env.TTSC_TSGO_BINARY ?? execution.tsgo.binary,
|
|
32
|
+
TTSC_TTSX_BINARY:
|
|
33
|
+
process.env.TTSC_TTSX_BINARY ??
|
|
34
|
+
path.join(__dirname, "..", "..", "launcher", "ttsx.js"),
|
|
35
|
+
...extra,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function spawnBinary(
|
|
40
|
+
binary: string,
|
|
41
|
+
args: readonly string[],
|
|
42
|
+
options: {
|
|
43
|
+
cwd?: string;
|
|
44
|
+
env?: NodeJS.ProcessEnv;
|
|
45
|
+
encoding?: BufferEncoding;
|
|
46
|
+
},
|
|
47
|
+
) {
|
|
48
|
+
const viaNode = /\.(?:[cm]?js|ts)$/i.test(binary);
|
|
49
|
+
if (!viaNode) {
|
|
50
|
+
ensureExecutable(binary);
|
|
51
|
+
}
|
|
52
|
+
return spawnSync(
|
|
53
|
+
viaNode ? process.execPath : binary,
|
|
54
|
+
viaNode ? [binary, ...args] : [...args],
|
|
55
|
+
{
|
|
56
|
+
cwd: options.cwd,
|
|
57
|
+
env: options.env,
|
|
58
|
+
encoding: options.encoding,
|
|
59
|
+
maxBuffer: 1024 * 1024 * 256,
|
|
60
|
+
windowsHide: true,
|
|
61
|
+
},
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function ensureExecutable(binary: string): void {
|
|
66
|
+
if (process.platform === "win32") {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const mode = fs.statSync(binary).mode & 0o777;
|
|
71
|
+
if ((mode & 0o111) !== 0) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
fs.chmodSync(binary, mode | 0o755);
|
|
75
|
+
} catch {
|
|
76
|
+
/* keep the original spawn error path */
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function outputText(value: string | Buffer | null | undefined): string {
|
|
81
|
+
if (value == null) {
|
|
82
|
+
return "";
|
|
83
|
+
}
|
|
84
|
+
return typeof value === "string" ? value : value.toString("utf8");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Run `ttsc` against a tsconfig. Returns once the binary exits so the CLI can
|
|
89
|
+
* decide how to surface diagnostics. Does not throw on non-zero exit.
|
|
90
|
+
*/
|
|
91
|
+
export function runBuild(
|
|
92
|
+
options: TtscBuildOptions & {
|
|
93
|
+
skipDiagnosticsCheck?: boolean;
|
|
94
|
+
forceListEmittedFiles?: boolean;
|
|
95
|
+
} = {},
|
|
96
|
+
): TtscBuildResult {
|
|
97
|
+
const execution = resolveExecutionContext(options);
|
|
98
|
+
if (execution.nativePlugins.length > 0) {
|
|
99
|
+
const compilers = execution.nativePlugins.filter(
|
|
100
|
+
(plugin) => plugin.stage === "transform",
|
|
101
|
+
);
|
|
102
|
+
const outputs = execution.nativePlugins.filter(
|
|
103
|
+
(plugin) => plugin.stage === "output",
|
|
104
|
+
);
|
|
105
|
+
const checked = runNativeCheckPlugins(options, execution);
|
|
106
|
+
if (checked.status !== 0) {
|
|
107
|
+
return checked;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (options.emit === false) {
|
|
111
|
+
if (compilers.length !== 0) {
|
|
112
|
+
assertSingleCompilerHost(compilers);
|
|
113
|
+
return appendBuildOutput(
|
|
114
|
+
checked,
|
|
115
|
+
buildWithNativeCompilerPlugins(options, execution, compilers),
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
if (checked.stdout !== "" || checked.stderr !== "") {
|
|
119
|
+
return checked;
|
|
120
|
+
}
|
|
121
|
+
return runTsgo(execution, ["--noEmit"], options);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
let result: TtscBuildResult;
|
|
125
|
+
if (compilers.length !== 0) {
|
|
126
|
+
assertSingleCompilerHost(compilers);
|
|
127
|
+
result = appendBuildOutput(
|
|
128
|
+
checked,
|
|
129
|
+
buildWithNativeCompilerPlugins(options, execution, compilers),
|
|
130
|
+
);
|
|
131
|
+
} else {
|
|
132
|
+
if (
|
|
133
|
+
checked.stdout === "" &&
|
|
134
|
+
checked.stderr === "" &&
|
|
135
|
+
options.skipDiagnosticsCheck !== true
|
|
136
|
+
) {
|
|
137
|
+
const tsgoChecked = runTsgo(execution, ["--noEmit"], options);
|
|
138
|
+
if (tsgoChecked.status !== 0) {
|
|
139
|
+
return tsgoChecked;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const args = createTsgoBuildArgs(execution, options, {
|
|
143
|
+
listEmittedFiles:
|
|
144
|
+
outputs.length !== 0 || options.forceListEmittedFiles === true,
|
|
145
|
+
});
|
|
146
|
+
const emitted = runTsgoBuild(execution, options, args);
|
|
147
|
+
result = appendBuildOutput(checked, emitted);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (result.status !== 0 || outputs.length === 0) {
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
return appendBuildOutput(
|
|
154
|
+
result,
|
|
155
|
+
applyOutputPlugins(
|
|
156
|
+
options,
|
|
157
|
+
execution,
|
|
158
|
+
result.emittedFiles ?? [],
|
|
159
|
+
outputs,
|
|
160
|
+
),
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (options.emit !== false && options.skipDiagnosticsCheck !== true) {
|
|
165
|
+
const checked = runTsgo(execution, ["--noEmit"], options);
|
|
166
|
+
if (checked.status !== 0) {
|
|
167
|
+
return checked;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const args = createTsgoBuildArgs(execution, options, {
|
|
172
|
+
listEmittedFiles:
|
|
173
|
+
options.emit !== false && options.forceListEmittedFiles === true,
|
|
174
|
+
});
|
|
175
|
+
return runTsgoBuild(execution, options, args);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function buildWithNativeCompilerPlugins(
|
|
179
|
+
options: TtscBuildOptions,
|
|
180
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
181
|
+
plugins: readonly ITtscLoadedNativePlugin[],
|
|
182
|
+
): TtscBuildResult {
|
|
183
|
+
return runNativePluginCommand(
|
|
184
|
+
plugins[0]!,
|
|
185
|
+
createNativeBuildArgs(execution, options, plugins),
|
|
186
|
+
options,
|
|
187
|
+
execution,
|
|
188
|
+
"ttsc.build",
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function runTsgo(
|
|
193
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
194
|
+
extraArgs: readonly string[],
|
|
195
|
+
options: NonNullable<Parameters<typeof runBuild>[0]>,
|
|
196
|
+
): TtscBuildResult {
|
|
197
|
+
const res = spawnBinary(
|
|
198
|
+
execution.tsgo.binary,
|
|
199
|
+
[
|
|
200
|
+
"-p",
|
|
201
|
+
execution.tsconfig,
|
|
202
|
+
...extraArgs,
|
|
203
|
+
...createTsgoDiagnosticArgs(options),
|
|
204
|
+
],
|
|
205
|
+
{
|
|
206
|
+
cwd: execution.projectRoot,
|
|
207
|
+
env: mergeEnv(options.env),
|
|
208
|
+
encoding: "utf8",
|
|
209
|
+
},
|
|
210
|
+
);
|
|
211
|
+
if (res.error) {
|
|
212
|
+
throw new Error(
|
|
213
|
+
"ttsc: failed to spawn " +
|
|
214
|
+
execution.tsgo.binary +
|
|
215
|
+
": " +
|
|
216
|
+
res.error.message,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
return normalizeFailedDiagnostics(
|
|
220
|
+
{
|
|
221
|
+
status: res.status ?? 1,
|
|
222
|
+
stdout: outputText(res.stdout),
|
|
223
|
+
stderr: outputText(res.stderr),
|
|
224
|
+
},
|
|
225
|
+
execution.projectRoot,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function runTsgoBuild(
|
|
230
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
231
|
+
options: NonNullable<Parameters<typeof runBuild>[0]>,
|
|
232
|
+
args: readonly string[],
|
|
233
|
+
): TtscBuildResult {
|
|
234
|
+
const res = spawnBinary(execution.tsgo.binary, args, {
|
|
235
|
+
cwd: execution.projectRoot,
|
|
236
|
+
env: mergeEnv(options.env),
|
|
237
|
+
encoding: "utf8",
|
|
238
|
+
});
|
|
239
|
+
if (res.error) {
|
|
240
|
+
throw new Error(
|
|
241
|
+
"ttsc.build: failed to spawn " +
|
|
242
|
+
execution.tsgo.binary +
|
|
243
|
+
": " +
|
|
244
|
+
res.error.message,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
const result = {
|
|
248
|
+
status: res.status ?? 1,
|
|
249
|
+
stdout: outputText(res.stdout),
|
|
250
|
+
stderr: outputText(res.stderr),
|
|
251
|
+
};
|
|
252
|
+
const emittedFiles = parseEmittedFiles(result.stdout);
|
|
253
|
+
if (emittedFiles.length !== 0) {
|
|
254
|
+
result.stdout = stripEmittedFileLines(result.stdout);
|
|
255
|
+
}
|
|
256
|
+
return normalizeFailedDiagnostics(
|
|
257
|
+
{ ...result, emittedFiles },
|
|
258
|
+
execution.projectRoot,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function createTsgoBuildArgs(
|
|
263
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
264
|
+
options: NonNullable<Parameters<typeof runBuild>[0]>,
|
|
265
|
+
flags: { listEmittedFiles: boolean },
|
|
266
|
+
): string[] {
|
|
267
|
+
const args = ["-p", execution.tsconfig];
|
|
268
|
+
if (options.emit === true) {
|
|
269
|
+
args.push("--noEmit", "false", "--emitDeclarationOnly", "false");
|
|
270
|
+
} else if (options.emit === false) {
|
|
271
|
+
args.push("--noEmit");
|
|
272
|
+
}
|
|
273
|
+
if (options.outDir) {
|
|
274
|
+
args.push("--outDir", path.resolve(execution.cwd, options.outDir));
|
|
275
|
+
}
|
|
276
|
+
if (flags.listEmittedFiles) {
|
|
277
|
+
args.push("--listEmittedFiles");
|
|
278
|
+
}
|
|
279
|
+
args.push(...createTsgoDiagnosticArgs(options));
|
|
280
|
+
return args;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function createTsgoDiagnosticArgs(options: TtscCommonOptions): string[] {
|
|
284
|
+
return options.structuredDiagnostics === true ? ["--pretty", "false"] : [];
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function createNativeBuildArgs(
|
|
288
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
289
|
+
options: TtscBuildOptions,
|
|
290
|
+
plugins: readonly ITtscLoadedNativePlugin[],
|
|
291
|
+
): string[] {
|
|
292
|
+
const args = [
|
|
293
|
+
options.emit === false ? "check" : "build",
|
|
294
|
+
"--tsconfig=" + execution.tsconfig,
|
|
295
|
+
"--plugins-json=" + serializeNativePlugins(plugins),
|
|
296
|
+
"--cwd=" + execution.projectRoot,
|
|
297
|
+
];
|
|
298
|
+
if (options.emit === true) {
|
|
299
|
+
args.push("--emit");
|
|
300
|
+
} else if (options.emit === false) {
|
|
301
|
+
args.push("--noEmit");
|
|
302
|
+
}
|
|
303
|
+
if (options.outDir) {
|
|
304
|
+
args.push("--outDir=" + path.resolve(execution.cwd, options.outDir));
|
|
305
|
+
}
|
|
306
|
+
if (options.quiet === false) {
|
|
307
|
+
args.push("--verbose");
|
|
308
|
+
} else if (options.quiet === true) {
|
|
309
|
+
args.push("--quiet");
|
|
310
|
+
}
|
|
311
|
+
return args;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function createNativeCheckArgs(
|
|
315
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
316
|
+
options: TtscBuildOptions,
|
|
317
|
+
): string[] {
|
|
318
|
+
const args = [
|
|
319
|
+
"check",
|
|
320
|
+
"--tsconfig=" + execution.tsconfig,
|
|
321
|
+
"--plugins-json=" + serializeNativePlugins(execution.nativePlugins),
|
|
322
|
+
"--cwd=" + execution.projectRoot,
|
|
323
|
+
];
|
|
324
|
+
if (options.outDir) {
|
|
325
|
+
args.push("--outDir=" + path.resolve(execution.cwd, options.outDir));
|
|
326
|
+
}
|
|
327
|
+
if (options.quiet === false) {
|
|
328
|
+
args.push("--verbose");
|
|
329
|
+
} else if (options.quiet === true) {
|
|
330
|
+
args.push("--quiet");
|
|
331
|
+
}
|
|
332
|
+
return args;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function createNativeOutputArgs(
|
|
336
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
337
|
+
options: TtscBuildOptions,
|
|
338
|
+
file: string,
|
|
339
|
+
): string[] {
|
|
340
|
+
const args = [
|
|
341
|
+
"output",
|
|
342
|
+
"--file=" + file,
|
|
343
|
+
"--tsconfig=" + execution.tsconfig,
|
|
344
|
+
"--plugins-json=" + serializeNativePlugins(execution.nativePlugins),
|
|
345
|
+
"--cwd=" + execution.projectRoot,
|
|
346
|
+
];
|
|
347
|
+
if (options.outDir) {
|
|
348
|
+
args.push("--outDir=" + path.resolve(execution.cwd, options.outDir));
|
|
349
|
+
}
|
|
350
|
+
return args;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function serializeNativePlugins(
|
|
354
|
+
plugins: readonly ITtscLoadedNativePlugin[],
|
|
355
|
+
): string {
|
|
356
|
+
return JSON.stringify(
|
|
357
|
+
plugins.map((plugin) => ({
|
|
358
|
+
config: plugin.config,
|
|
359
|
+
name: plugin.name,
|
|
360
|
+
stage: plugin.stage,
|
|
361
|
+
})),
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function runNativeCheckPlugins(
|
|
366
|
+
options: TtscBuildOptions,
|
|
367
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
368
|
+
): TtscBuildResult {
|
|
369
|
+
let out: TtscBuildResult = {
|
|
370
|
+
diagnostics: [],
|
|
371
|
+
status: 0,
|
|
372
|
+
stdout: "",
|
|
373
|
+
stderr: "",
|
|
374
|
+
};
|
|
375
|
+
for (const plugin of execution.nativePlugins.filter(
|
|
376
|
+
(plugin) => plugin.stage === "check",
|
|
377
|
+
)) {
|
|
378
|
+
const result = runNativePluginCommand(
|
|
379
|
+
plugin,
|
|
380
|
+
createNativeCheckArgs(execution, options),
|
|
381
|
+
options,
|
|
382
|
+
execution,
|
|
383
|
+
"ttsc.check",
|
|
384
|
+
);
|
|
385
|
+
out = appendBuildOutput(out, result);
|
|
386
|
+
if (result.status !== 0) {
|
|
387
|
+
return out;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return out;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function applyOutputPlugins(
|
|
394
|
+
options: TtscBuildOptions,
|
|
395
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
396
|
+
emittedFiles: readonly string[],
|
|
397
|
+
plugins: readonly ITtscLoadedNativePlugin[],
|
|
398
|
+
): TtscBuildResult {
|
|
399
|
+
let out: TtscBuildResult = {
|
|
400
|
+
diagnostics: [],
|
|
401
|
+
status: 0,
|
|
402
|
+
stdout: "",
|
|
403
|
+
stderr: "",
|
|
404
|
+
};
|
|
405
|
+
for (const plugin of plugins) {
|
|
406
|
+
for (const file of emittedFiles) {
|
|
407
|
+
if (!fs.existsSync(file)) {
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
const result = runNativeOutputPlugin(options, execution, plugin, file);
|
|
411
|
+
out = appendBuildOutput(out, result);
|
|
412
|
+
if (result.status !== 0) {
|
|
413
|
+
return out;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return out;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function runNativeOutputPlugin(
|
|
421
|
+
options: TtscBuildOptions,
|
|
422
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
423
|
+
plugin: ITtscLoadedNativePlugin,
|
|
424
|
+
file: string,
|
|
425
|
+
): TtscBuildResult {
|
|
426
|
+
return runNativePluginCommand(
|
|
427
|
+
plugin,
|
|
428
|
+
createNativeOutputArgs(execution, options, file),
|
|
429
|
+
options,
|
|
430
|
+
execution,
|
|
431
|
+
"ttsc.output",
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function runNativePluginCommand(
|
|
436
|
+
plugin: ITtscLoadedNativePlugin,
|
|
437
|
+
args: readonly string[],
|
|
438
|
+
options: TtscBuildOptions,
|
|
439
|
+
execution: ReturnType<typeof resolveExecutionContext>,
|
|
440
|
+
label: string,
|
|
441
|
+
): TtscBuildResult {
|
|
442
|
+
const res = spawnBinary(plugin.binary, args, {
|
|
443
|
+
cwd: execution.projectRoot,
|
|
444
|
+
env: nativePluginEnv(options.env, execution),
|
|
445
|
+
encoding: "utf8",
|
|
446
|
+
});
|
|
447
|
+
if (res.error) {
|
|
448
|
+
throw new Error(
|
|
449
|
+
`${label}: failed to spawn ${plugin.binary}: ${res.error.message}`,
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
return normalizeFailedDiagnostics(
|
|
453
|
+
{
|
|
454
|
+
status: res.status ?? 1,
|
|
455
|
+
stdout: outputText(res.stdout),
|
|
456
|
+
stderr: outputText(res.stderr),
|
|
457
|
+
},
|
|
458
|
+
execution.projectRoot,
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function appendBuildOutput(
|
|
463
|
+
left: TtscBuildResult,
|
|
464
|
+
right: TtscBuildResult,
|
|
465
|
+
): TtscBuildResult {
|
|
466
|
+
return normalizeFailedDiagnostics({
|
|
467
|
+
diagnostics: [...left.diagnostics, ...right.diagnostics],
|
|
468
|
+
emittedFiles:
|
|
469
|
+
right.emittedFiles !== undefined ? right.emittedFiles : left.emittedFiles,
|
|
470
|
+
status: right.status !== 0 ? right.status : left.status,
|
|
471
|
+
stdout: left.stdout + right.stdout,
|
|
472
|
+
stderr: left.stderr + right.stderr,
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function assertSingleCompilerHost(
|
|
477
|
+
plugins: readonly ITtscLoadedNativePlugin[],
|
|
478
|
+
): void {
|
|
479
|
+
const binaries = [...new Set(plugins.map((plugin) => plugin.binary))];
|
|
480
|
+
if (binaries.length > 1) {
|
|
481
|
+
throw new Error(
|
|
482
|
+
"ttsc: multiple compiler native backends cannot share one emit pass; " +
|
|
483
|
+
"use output-capability plugins for post-emit transforms",
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function resolveExecutionContext(
|
|
489
|
+
options: TtscCommonOptions & { tsconfig?: string },
|
|
490
|
+
) {
|
|
491
|
+
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
492
|
+
const tsconfig = resolveProjectConfig({
|
|
493
|
+
cwd,
|
|
494
|
+
tsconfig: options.tsconfig,
|
|
495
|
+
});
|
|
496
|
+
const projectRoot = path.dirname(tsconfig);
|
|
497
|
+
const tsgo = resolveTsgo({ ...options, cwd: projectRoot });
|
|
498
|
+
const fallbackBinary = resolveBinary(options);
|
|
499
|
+
const loaded = loadProjectPlugins({
|
|
500
|
+
binary: fallbackBinary ?? "",
|
|
501
|
+
cacheDir: options.cacheDir ?? options.env?.TTSC_CACHE_DIR,
|
|
502
|
+
cwd,
|
|
503
|
+
entries: options.plugins,
|
|
504
|
+
tsconfig,
|
|
505
|
+
});
|
|
506
|
+
return {
|
|
507
|
+
cwd,
|
|
508
|
+
nativePlugins: loaded.nativePlugins,
|
|
509
|
+
projectRoot,
|
|
510
|
+
tsgo,
|
|
511
|
+
tsconfig,
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function parseEmittedFiles(stdout: string): string[] {
|
|
516
|
+
const out: string[] = [];
|
|
517
|
+
for (const line of stdout.split(/\r?\n/)) {
|
|
518
|
+
const match = line.match(/^TSFILE:\s*(.+)$/);
|
|
519
|
+
if (match?.[1]) {
|
|
520
|
+
out.push(path.resolve(match[1].trim()));
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
return out;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function stripEmittedFileLines(stdout: string): string {
|
|
527
|
+
return stdout
|
|
528
|
+
.split(/\r?\n/)
|
|
529
|
+
.filter((line) => !/^TSFILE:\s*/.test(line))
|
|
530
|
+
.join("\n")
|
|
531
|
+
.replace(/\n+$/, "");
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
type PartialBuildResult = Omit<TtscBuildResult, "diagnostics"> & {
|
|
535
|
+
diagnostics?: ITtscCompilerDiagnostic[];
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
function normalizeFailedDiagnostics(
|
|
539
|
+
result: PartialBuildResult,
|
|
540
|
+
cwd?: string,
|
|
541
|
+
): TtscBuildResult {
|
|
542
|
+
const diagnostics =
|
|
543
|
+
result.diagnostics ?? parseCompilerDiagnostics(result, cwd);
|
|
544
|
+
if (result.status === 0 || result.stderr.trim().length !== 0) {
|
|
545
|
+
return { ...result, diagnostics };
|
|
546
|
+
}
|
|
547
|
+
if (result.stdout.trim().length === 0) {
|
|
548
|
+
return { ...result, diagnostics };
|
|
549
|
+
}
|
|
550
|
+
return {
|
|
551
|
+
diagnostics,
|
|
552
|
+
emittedFiles: result.emittedFiles,
|
|
553
|
+
status: result.status,
|
|
554
|
+
stdout: "",
|
|
555
|
+
stderr: result.stdout,
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
function parseCompilerDiagnostics(
|
|
560
|
+
result: Pick<TtscBuildResult, "stderr" | "stdout">,
|
|
561
|
+
cwd: string | undefined,
|
|
562
|
+
): ITtscCompilerDiagnostic[] {
|
|
563
|
+
const lines = stripAnsi(`${result.stderr}\n${result.stdout}`).split(/\r?\n/);
|
|
564
|
+
const out: ITtscCompilerDiagnostic[] = [];
|
|
565
|
+
let current: ITtscCompilerDiagnostic | undefined;
|
|
566
|
+
for (const line of lines) {
|
|
567
|
+
if (line.length === 0 || /^TSFILE:\s*/.test(line)) {
|
|
568
|
+
continue;
|
|
569
|
+
}
|
|
570
|
+
if (/^Found\s+\d+\s+errors?/i.test(line)) {
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const diagnostic = parseDiagnosticLine(line, cwd);
|
|
575
|
+
if (diagnostic !== null) {
|
|
576
|
+
current = diagnostic;
|
|
577
|
+
out.push(current);
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if (current !== undefined && /^\s+/.test(line)) {
|
|
582
|
+
current.messageText += `\n${line.trimEnd()}`;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
return out;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function parseDiagnosticLine(
|
|
589
|
+
line: string,
|
|
590
|
+
cwd: string | undefined,
|
|
591
|
+
): ITtscCompilerDiagnostic | null {
|
|
592
|
+
const fileMatch = line.match(
|
|
593
|
+
/^(.+?)\((\d+),(\d+)\):\s+(error|warning|suggestion|message)\s+([A-Z]+)?(\d+|[A-Z][A-Z0-9_-]*):\s+(.+)$/i,
|
|
594
|
+
);
|
|
595
|
+
if (fileMatch) {
|
|
596
|
+
return {
|
|
597
|
+
category: normalizeDiagnosticCategory(fileMatch[4]!),
|
|
598
|
+
character: Number(fileMatch[3]),
|
|
599
|
+
code: normalizeDiagnosticCode(fileMatch[6]!),
|
|
600
|
+
file: normalizeDiagnosticFile(fileMatch[1]!, cwd),
|
|
601
|
+
line: Number(fileMatch[2]),
|
|
602
|
+
messageText: fileMatch[7]!,
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
const globalMatch = line.match(
|
|
607
|
+
/^(error|warning|suggestion|message)\s+([A-Z]+)?(\d+|[A-Z][A-Z0-9_-]*):\s+(.+)$/i,
|
|
608
|
+
);
|
|
609
|
+
if (!globalMatch) {
|
|
610
|
+
return null;
|
|
611
|
+
}
|
|
612
|
+
return {
|
|
613
|
+
category: normalizeDiagnosticCategory(globalMatch[1]!),
|
|
614
|
+
code: normalizeDiagnosticCode(globalMatch[3]!),
|
|
615
|
+
file: null,
|
|
616
|
+
messageText: globalMatch[4]!,
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function normalizeDiagnosticFile(file: string, cwd: string | undefined): string {
|
|
621
|
+
if (path.isAbsolute(file) || cwd === undefined) {
|
|
622
|
+
return file;
|
|
623
|
+
}
|
|
624
|
+
return path.resolve(cwd, file);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
function normalizeDiagnosticCategory(
|
|
628
|
+
value: string,
|
|
629
|
+
): ITtscCompilerDiagnostic.Category {
|
|
630
|
+
const lowered = value.toLowerCase();
|
|
631
|
+
return lowered === "warning" ||
|
|
632
|
+
lowered === "suggestion" ||
|
|
633
|
+
lowered === "message"
|
|
634
|
+
? lowered
|
|
635
|
+
: "error";
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
function normalizeDiagnosticCode(value: string): number | string {
|
|
639
|
+
return /^\d+$/.test(value) ? Number(value) : value;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
function stripAnsi(text: string): string {
|
|
643
|
+
return text.replace(/\x1b\[[0-9;]*m/g, "");
|
|
644
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as os from "node:os";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
|
|
5
|
+
import type { TtscTransformOptions } from "../../structures/internal/TtscTransformOptions";
|
|
6
|
+
import { resolveProjectConfig } from "./project/resolveProjectConfig";
|
|
7
|
+
import { resolveEmittedJavaScript } from "./resolveEmittedJavaScript";
|
|
8
|
+
import { runBuild } from "./runBuild";
|
|
9
|
+
|
|
10
|
+
/** Transform one source file by emitting its project into a temporary directory. */
|
|
11
|
+
export function runTransform(options: TtscTransformOptions): string {
|
|
12
|
+
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
13
|
+
const sourceFile = realpathIfExists(
|
|
14
|
+
path.isAbsolute(options.file) ? options.file : path.resolve(cwd, options.file),
|
|
15
|
+
);
|
|
16
|
+
const tsconfig = resolveProjectConfig({
|
|
17
|
+
cwd,
|
|
18
|
+
file: sourceFile,
|
|
19
|
+
tsconfig: options.tsconfig,
|
|
20
|
+
});
|
|
21
|
+
const projectRoot = path.dirname(tsconfig);
|
|
22
|
+
const outDir = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-transform-"));
|
|
23
|
+
try {
|
|
24
|
+
const result = runBuild({
|
|
25
|
+
...options,
|
|
26
|
+
cwd,
|
|
27
|
+
emit: true,
|
|
28
|
+
forceListEmittedFiles: true,
|
|
29
|
+
outDir,
|
|
30
|
+
tsconfig,
|
|
31
|
+
});
|
|
32
|
+
if (result.status !== 0) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
"ttsc transform exited " +
|
|
35
|
+
result.status +
|
|
36
|
+
"\n" +
|
|
37
|
+
(result.stderr || result.stdout),
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const emitted = resolveEmittedJavaScript({
|
|
41
|
+
emittedFiles: result.emittedFiles,
|
|
42
|
+
outDir,
|
|
43
|
+
projectRoot,
|
|
44
|
+
sourceFile,
|
|
45
|
+
});
|
|
46
|
+
if (emitted === null) {
|
|
47
|
+
throw new Error(`ttsc transform: no output produced for ${sourceFile}`);
|
|
48
|
+
}
|
|
49
|
+
const transformed = fs.readFileSync(emitted, "utf8");
|
|
50
|
+
if (options.out) {
|
|
51
|
+
const target = path.isAbsolute(options.out)
|
|
52
|
+
? options.out
|
|
53
|
+
: path.resolve(cwd, options.out);
|
|
54
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
55
|
+
fs.writeFileSync(target, transformed, "utf8");
|
|
56
|
+
}
|
|
57
|
+
return transformed;
|
|
58
|
+
} finally {
|
|
59
|
+
fs.rmSync(outDir, { recursive: true, force: true });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function realpathIfExists(file: string): string {
|
|
64
|
+
try {
|
|
65
|
+
return fs.realpathSync(file);
|
|
66
|
+
} catch {
|
|
67
|
+
return file;
|
|
68
|
+
}
|
|
69
|
+
}
|