ttsc 0.6.0 → 0.7.1

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.
Files changed (271) hide show
  1. package/cmd/platform/main.go +1 -1
  2. package/cmd/ttsc/api_compile.go +134 -0
  3. package/cmd/ttsc/api_transform.go +64 -0
  4. package/cmd/ttsc/build.go +1 -7
  5. package/cmd/ttsc/main.go +6 -20
  6. package/driver/program.go +13 -3
  7. package/lib/TtscCompiler.d.ts +96 -0
  8. package/lib/TtscCompiler.js +294 -0
  9. package/lib/TtscCompiler.js.map +1 -0
  10. package/lib/compiler/internal/buildNativeCompiler.d.ts +6 -0
  11. package/lib/compiler/internal/buildNativeCompiler.js +72 -0
  12. package/lib/compiler/internal/buildNativeCompiler.js.map +1 -0
  13. package/lib/compiler/internal/compileProjectInMemory.d.ts +7 -0
  14. package/lib/compiler/internal/compileProjectInMemory.js +176 -0
  15. package/lib/compiler/internal/compileProjectInMemory.js.map +1 -0
  16. package/lib/compiler/internal/project/readProjectConfig.d.ts +4 -0
  17. package/lib/{project.js → compiler/internal/project/readProjectConfig.js} +12 -82
  18. package/lib/compiler/internal/project/readProjectConfig.js.map +1 -0
  19. package/lib/compiler/internal/project/resolveProjectConfig.d.ts +3 -0
  20. package/lib/compiler/internal/project/resolveProjectConfig.js +94 -0
  21. package/lib/compiler/internal/project/resolveProjectConfig.js.map +1 -0
  22. package/lib/compiler/internal/resolveBinary.d.ts +4 -0
  23. package/lib/compiler/internal/resolveBinary.js +65 -0
  24. package/lib/compiler/internal/resolveBinary.js.map +1 -0
  25. package/lib/compiler/internal/resolveEmittedJavaScript.d.ts +7 -0
  26. package/lib/compiler/internal/resolveEmittedJavaScript.js +118 -0
  27. package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -0
  28. package/lib/compiler/internal/resolveTsgo.d.ts +20 -0
  29. package/lib/{tsgo.js → compiler/internal/resolveTsgo.js} +5 -10
  30. package/lib/compiler/internal/resolveTsgo.js.map +1 -0
  31. package/lib/compiler/internal/runBuild.d.ts +10 -0
  32. package/lib/compiler/internal/runBuild.js +480 -0
  33. package/lib/compiler/internal/runBuild.js.map +1 -0
  34. package/lib/compiler/internal/runSingleFileEmit.d.ts +3 -0
  35. package/lib/compiler/internal/runSingleFileEmit.js +102 -0
  36. package/lib/compiler/internal/runSingleFileEmit.js.map +1 -0
  37. package/lib/compiler/internal/transformProjectInMemory.d.ts +7 -0
  38. package/lib/compiler/internal/transformProjectInMemory.js +275 -0
  39. package/lib/compiler/internal/transformProjectInMemory.js.map +1 -0
  40. package/lib/index.d.ts +6 -15
  41. package/lib/index.js +6 -15
  42. package/lib/index.js.map +1 -1
  43. package/lib/launcher/internal/getCompilerVersionText.d.ts +3 -0
  44. package/lib/launcher/internal/getCompilerVersionText.js +71 -0
  45. package/lib/launcher/internal/getCompilerVersionText.js.map +1 -0
  46. package/lib/launcher/internal/prepareExecution.d.ts +10 -0
  47. package/lib/launcher/internal/prepareExecution.js +120 -0
  48. package/lib/launcher/internal/prepareExecution.js.map +1 -0
  49. package/lib/launcher/internal/runTtsc.d.ts +1 -0
  50. package/lib/{cli.js → launcher/internal/runTtsc.js} +45 -118
  51. package/lib/launcher/internal/runTtsc.js.map +1 -0
  52. package/lib/launcher/internal/runTtsx.d.ts +1 -0
  53. package/lib/{runner/cli.js → launcher/internal/runTtsx.js} +13 -35
  54. package/lib/launcher/internal/runTtsx.js.map +1 -0
  55. package/lib/launcher/ttsc.js +2 -2
  56. package/lib/launcher/ttsc.js.map +1 -1
  57. package/lib/launcher/ttsx.js +2 -2
  58. package/lib/launcher/ttsx.js.map +1 -1
  59. package/lib/plugin/internal/buildSourcePlugin.d.ts +12 -0
  60. package/lib/{source-build.js → plugin/internal/buildSourcePlugin.js} +77 -84
  61. package/lib/plugin/internal/buildSourcePlugin.js.map +1 -0
  62. package/lib/plugin/internal/loadProjectPlugins.d.ts +14 -0
  63. package/lib/{plugin.js → plugin/internal/loadProjectPlugins.js} +38 -76
  64. package/lib/plugin/internal/loadProjectPlugins.js.map +1 -0
  65. package/lib/structures/ITtscCompilerContext.d.ts +78 -0
  66. package/lib/structures/ITtscCompilerContext.js +3 -0
  67. package/lib/structures/ITtscCompilerContext.js.map +1 -0
  68. package/lib/structures/ITtscCompilerDiagnostic.d.ts +83 -0
  69. package/lib/structures/ITtscCompilerDiagnostic.js +3 -0
  70. package/lib/structures/ITtscCompilerDiagnostic.js.map +1 -0
  71. package/lib/structures/ITtscCompilerResult.d.ts +83 -0
  72. package/lib/structures/{ITtscCacheKeyInput.js → ITtscCompilerResult.js} +1 -1
  73. package/lib/structures/ITtscCompilerResult.js.map +1 -0
  74. package/lib/structures/ITtscCompilerTransformation.d.ts +35 -0
  75. package/lib/structures/ITtscCompilerTransformation.js +3 -0
  76. package/lib/structures/ITtscCompilerTransformation.js.map +1 -0
  77. package/lib/structures/ITtscPlugin.d.ts +52 -7
  78. package/lib/structures/ITtscPluginFactoryContext.d.ts +47 -0
  79. package/lib/structures/ITtscProjectPluginConfig.d.ts +41 -0
  80. package/lib/structures/TtscPluginStage.d.ts +18 -0
  81. package/lib/structures/{ITtscBuildResult.js → TtscPluginStage.js} +1 -1
  82. package/lib/structures/TtscPluginStage.js.map +1 -0
  83. package/lib/structures/index.d.ts +8 -0
  84. package/lib/structures/index.js +25 -0
  85. package/lib/structures/index.js.map +1 -0
  86. package/lib/structures/internal/ITtscLoadedNativePlugin.d.ts +13 -0
  87. package/lib/structures/internal/ITtscLoadedNativePlugin.js.map +1 -0
  88. package/lib/structures/internal/ITtscParsedProjectConfig.d.ts +15 -0
  89. package/lib/structures/internal/ITtscParsedProjectConfig.js.map +1 -0
  90. package/lib/structures/internal/ITtscProjectLocatorOptions.d.ts +9 -0
  91. package/lib/structures/internal/ITtscProjectLocatorOptions.js.map +1 -0
  92. package/lib/structures/internal/TtscBuildOptions.d.ts +18 -0
  93. package/lib/structures/{ITtscBuildOptions.js → internal/TtscBuildOptions.js} +1 -1
  94. package/lib/structures/internal/TtscBuildOptions.js.map +1 -0
  95. package/lib/structures/internal/TtscBuildResult.d.ts +14 -0
  96. package/lib/structures/{ITtscCheckOptions.js → internal/TtscBuildResult.js} +1 -1
  97. package/lib/structures/internal/TtscBuildResult.js.map +1 -0
  98. package/lib/structures/internal/TtscCommonOptions.d.ts +27 -0
  99. package/lib/structures/{ITtscNativeSource.js → internal/TtscCommonOptions.js} +1 -1
  100. package/lib/structures/internal/TtscCommonOptions.js.map +1 -0
  101. package/lib/structures/internal/TtscSingleFileEmitOptions.d.ts +14 -0
  102. package/lib/structures/internal/TtscSingleFileEmitOptions.js +3 -0
  103. package/lib/structures/internal/TtscSingleFileEmitOptions.js.map +1 -0
  104. package/package.json +9 -9
  105. package/src/TtscCompiler.ts +305 -0
  106. package/src/compiler/internal/buildNativeCompiler.ts +43 -0
  107. package/src/compiler/internal/compileProjectInMemory.ts +189 -0
  108. package/src/{project.ts → compiler/internal/project/readProjectConfig.ts} +36 -111
  109. package/src/compiler/internal/project/resolveProjectConfig.ts +67 -0
  110. package/src/compiler/internal/resolveBinary.ts +41 -0
  111. package/src/compiler/internal/resolveEmittedJavaScript.ts +102 -0
  112. package/src/{tsgo.ts → compiler/internal/resolveTsgo.ts} +20 -23
  113. package/src/compiler/internal/runBuild.ts +644 -0
  114. package/src/compiler/internal/runSingleFileEmit.ts +73 -0
  115. package/src/compiler/internal/transformProjectInMemory.ts +368 -0
  116. package/src/index.ts +6 -15
  117. package/src/launcher/internal/getCompilerVersionText.ts +44 -0
  118. package/src/launcher/internal/prepareExecution.ts +115 -0
  119. package/src/{cli.ts → launcher/internal/runTtsc.ts} +58 -140
  120. package/src/{runner/cli.ts → launcher/internal/runTtsx.ts} +21 -49
  121. package/src/launcher/ttsc.ts +2 -2
  122. package/src/launcher/ttsx.ts +2 -2
  123. package/src/{source-build.ts → plugin/internal/buildSourcePlugin.ts} +121 -115
  124. package/src/plugin/internal/loadProjectPlugins.ts +214 -0
  125. package/src/structures/ITtscCompilerContext.ts +84 -0
  126. package/src/structures/ITtscCompilerDiagnostic.ts +91 -0
  127. package/src/structures/ITtscCompilerResult.ts +94 -0
  128. package/src/structures/ITtscCompilerTransformation.ts +46 -0
  129. package/src/structures/ITtscPlugin.ts +54 -7
  130. package/src/structures/ITtscPluginFactoryContext.ts +51 -0
  131. package/src/structures/ITtscProjectPluginConfig.ts +43 -0
  132. package/src/structures/TtscPluginStage.ts +18 -0
  133. package/src/structures/index.ts +8 -0
  134. package/src/structures/internal/ITtscLoadedNativePlugin.ts +14 -0
  135. package/src/structures/internal/ITtscParsedProjectConfig.ts +16 -0
  136. package/src/structures/internal/ITtscProjectLocatorOptions.ts +9 -0
  137. package/src/structures/internal/TtscBuildOptions.ts +19 -0
  138. package/src/structures/internal/TtscBuildResult.ts +15 -0
  139. package/src/structures/internal/TtscCommonOptions.ts +28 -0
  140. package/src/structures/internal/TtscSingleFileEmitOptions.ts +15 -0
  141. package/test/compiler.test.ts +422 -0
  142. package/test/platform.test.ts +1 -45
  143. package/test/project.test.ts +3 -1
  144. package/test/source-build.test.ts +33 -220
  145. package/test/tsgo.test.ts +1 -8
  146. package/cmd/ttsc/transform.go +0 -166
  147. package/lib/api.d.ts +0 -40
  148. package/lib/api.js +0 -680
  149. package/lib/api.js.map +0 -1
  150. package/lib/cli.d.ts +0 -1
  151. package/lib/cli.js.map +0 -1
  152. package/lib/native.d.ts +0 -8
  153. package/lib/native.js +0 -42
  154. package/lib/native.js.map +0 -1
  155. package/lib/platform.d.ts +0 -52
  156. package/lib/platform.js +0 -175
  157. package/lib/platform.js.map +0 -1
  158. package/lib/plugin.d.ts +0 -13
  159. package/lib/plugin.js.map +0 -1
  160. package/lib/project.d.ts +0 -14
  161. package/lib/project.js.map +0 -1
  162. package/lib/runner/cli.d.ts +0 -1
  163. package/lib/runner/cli.js.map +0 -1
  164. package/lib/runner/register.d.ts +0 -6
  165. package/lib/runner/register.js +0 -358
  166. package/lib/runner/register.js.map +0 -1
  167. package/lib/source-build.d.ts +0 -19
  168. package/lib/source-build.js.map +0 -1
  169. package/lib/structures/ITtscBuildOptions.d.ts +0 -14
  170. package/lib/structures/ITtscBuildOptions.js.map +0 -1
  171. package/lib/structures/ITtscBuildResult.d.ts +0 -6
  172. package/lib/structures/ITtscBuildResult.js.map +0 -1
  173. package/lib/structures/ITtscBuildSourcePluginOptions.d.ts +0 -8
  174. package/lib/structures/ITtscBuildSourcePluginOptions.js +0 -3
  175. package/lib/structures/ITtscBuildSourcePluginOptions.js.map +0 -1
  176. package/lib/structures/ITtscCacheKeyInput.d.ts +0 -7
  177. package/lib/structures/ITtscCacheKeyInput.js.map +0 -1
  178. package/lib/structures/ITtscCheckOptions.d.ts +0 -2
  179. package/lib/structures/ITtscCheckOptions.js.map +0 -1
  180. package/lib/structures/ITtscCommonOptions.d.ts +0 -22
  181. package/lib/structures/ITtscCommonOptions.js +0 -3
  182. package/lib/structures/ITtscCommonOptions.js.map +0 -1
  183. package/lib/structures/ITtscLoadPluginsOptions.d.ts +0 -8
  184. package/lib/structures/ITtscLoadPluginsOptions.js +0 -3
  185. package/lib/structures/ITtscLoadPluginsOptions.js.map +0 -1
  186. package/lib/structures/ITtscLoadedNativePlugin.d.ts +0 -7
  187. package/lib/structures/ITtscLoadedNativePlugin.js.map +0 -1
  188. package/lib/structures/ITtscLoadedPlugins.d.ts +0 -11
  189. package/lib/structures/ITtscLoadedPlugins.js +0 -3
  190. package/lib/structures/ITtscLoadedPlugins.js.map +0 -1
  191. package/lib/structures/ITtscNativeBackend.d.ts +0 -10
  192. package/lib/structures/ITtscNativeBackend.js +0 -3
  193. package/lib/structures/ITtscNativeBackend.js.map +0 -1
  194. package/lib/structures/ITtscNativePluginContractVersion.d.ts +0 -1
  195. package/lib/structures/ITtscNativePluginContractVersion.js +0 -3
  196. package/lib/structures/ITtscNativePluginContractVersion.js.map +0 -1
  197. package/lib/structures/ITtscNativePluginShape.d.ts +0 -8
  198. package/lib/structures/ITtscNativePluginShape.js +0 -3
  199. package/lib/structures/ITtscNativePluginShape.js.map +0 -1
  200. package/lib/structures/ITtscNativeRewriteMode.d.ts +0 -1
  201. package/lib/structures/ITtscNativeRewriteMode.js +0 -3
  202. package/lib/structures/ITtscNativeRewriteMode.js.map +0 -1
  203. package/lib/structures/ITtscNativeSource.d.ts +0 -4
  204. package/lib/structures/ITtscNativeSource.js.map +0 -1
  205. package/lib/structures/ITtscParsedProjectConfig.d.ts +0 -9
  206. package/lib/structures/ITtscParsedProjectConfig.js.map +0 -1
  207. package/lib/structures/ITtscPluginFactory.d.ts +0 -4
  208. package/lib/structures/ITtscPluginFactory.js +0 -3
  209. package/lib/structures/ITtscPluginFactory.js.map +0 -1
  210. package/lib/structures/ITtscPluginModule.d.ts +0 -3
  211. package/lib/structures/ITtscPluginModule.js +0 -3
  212. package/lib/structures/ITtscPluginModule.js.map +0 -1
  213. package/lib/structures/ITtscPreparedExecution.d.ts +0 -5
  214. package/lib/structures/ITtscPreparedExecution.js +0 -3
  215. package/lib/structures/ITtscPreparedExecution.js.map +0 -1
  216. package/lib/structures/ITtscProjectLocatorOptions.d.ts +0 -5
  217. package/lib/structures/ITtscProjectLocatorOptions.js.map +0 -1
  218. package/lib/structures/ITtscRegisterOptions.d.ts +0 -6
  219. package/lib/structures/ITtscRegisterOptions.js +0 -3
  220. package/lib/structures/ITtscRegisterOptions.js.map +0 -1
  221. package/lib/structures/ITtscResolveGoBinaryOptions.d.ts +0 -7
  222. package/lib/structures/ITtscResolveGoBinaryOptions.js +0 -3
  223. package/lib/structures/ITtscResolveGoBinaryOptions.js.map +0 -1
  224. package/lib/structures/ITtscResolveOptions.d.ts +0 -20
  225. package/lib/structures/ITtscResolveOptions.js +0 -3
  226. package/lib/structures/ITtscResolveOptions.js.map +0 -1
  227. package/lib/structures/ITtscResolvedTsgo.d.ts +0 -8
  228. package/lib/structures/ITtscResolvedTsgo.js +0 -3
  229. package/lib/structures/ITtscResolvedTsgo.js.map +0 -1
  230. package/lib/structures/ITtscTransformOptions.d.ts +0 -12
  231. package/lib/structures/ITtscTransformOptions.js +0 -3
  232. package/lib/structures/ITtscTransformOptions.js.map +0 -1
  233. package/lib/structures/ITtscTsgoResolveOptions.d.ts +0 -14
  234. package/lib/structures/ITtscTsgoResolveOptions.js +0 -3
  235. package/lib/structures/ITtscTsgoResolveOptions.js.map +0 -1
  236. package/lib/tsgo.d.ts +0 -6
  237. package/lib/tsgo.js.map +0 -1
  238. package/src/api.ts +0 -872
  239. package/src/native.ts +0 -64
  240. package/src/platform.ts +0 -151
  241. package/src/plugin.ts +0 -253
  242. package/src/runner/register.ts +0 -406
  243. package/src/structures/ITtscBuildOptions.ts +0 -19
  244. package/src/structures/ITtscBuildResult.ts +0 -6
  245. package/src/structures/ITtscBuildSourcePluginOptions.ts +0 -9
  246. package/src/structures/ITtscCacheKeyInput.ts +0 -7
  247. package/src/structures/ITtscCheckOptions.ts +0 -3
  248. package/src/structures/ITtscCommonOptions.ts +0 -23
  249. package/src/structures/ITtscLoadPluginsOptions.ts +0 -9
  250. package/src/structures/ITtscLoadedNativePlugin.ts +0 -8
  251. package/src/structures/ITtscLoadedPlugins.ts +0 -12
  252. package/src/structures/ITtscNativeBackend.ts +0 -11
  253. package/src/structures/ITtscNativePluginContractVersion.ts +0 -1
  254. package/src/structures/ITtscNativePluginShape.ts +0 -9
  255. package/src/structures/ITtscNativeRewriteMode.ts +0 -1
  256. package/src/structures/ITtscNativeSource.ts +0 -4
  257. package/src/structures/ITtscParsedProjectConfig.ts +0 -10
  258. package/src/structures/ITtscPluginFactory.ts +0 -7
  259. package/src/structures/ITtscPluginModule.ts +0 -4
  260. package/src/structures/ITtscPreparedExecution.ts +0 -5
  261. package/src/structures/ITtscProjectLocatorOptions.ts +0 -5
  262. package/src/structures/ITtscRegisterOptions.ts +0 -7
  263. package/src/structures/ITtscResolveGoBinaryOptions.ts +0 -7
  264. package/src/structures/ITtscResolveOptions.ts +0 -20
  265. package/src/structures/ITtscResolvedTsgo.ts +0 -8
  266. package/src/structures/ITtscTransformOptions.ts +0 -13
  267. package/src/structures/ITtscTsgoResolveOptions.ts +0 -14
  268. package/test/native.test.ts +0 -103
  269. /package/lib/structures/{ITtscLoadedNativePlugin.js → internal/ITtscLoadedNativePlugin.js} +0 -0
  270. /package/lib/structures/{ITtscParsedProjectConfig.js → internal/ITtscParsedProjectConfig.js} +0 -0
  271. /package/lib/structures/{ITtscProjectLocatorOptions.js → internal/ITtscProjectLocatorOptions.js} +0 -0
@@ -0,0 +1,368 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import * as fs from "node:fs";
3
+ import * as path from "node:path";
4
+
5
+ import { loadProjectPlugins } from "../../plugin/internal/loadProjectPlugins";
6
+ import type { ITtscCompilerContext } from "../../structures/ITtscCompilerContext";
7
+ import type { ITtscCompilerDiagnostic } from "../../structures/ITtscCompilerDiagnostic";
8
+ import type { ITtscProjectPluginConfig } from "../../structures/ITtscProjectPluginConfig";
9
+ import type { ITtscLoadedNativePlugin } from "../../structures/internal/ITtscLoadedNativePlugin";
10
+ import type { ITtscParsedProjectConfig } from "../../structures/internal/ITtscParsedProjectConfig";
11
+ import type { TtscBuildResult } from "../../structures/internal/TtscBuildResult";
12
+ import { buildNativeCompiler } from "./buildNativeCompiler";
13
+ import { readProjectConfig } from "./project/readProjectConfig";
14
+ import { resolveBinary } from "./resolveBinary";
15
+ import { resolveTsgo } from "./resolveTsgo";
16
+
17
+ /** Transform a project and capture TypeScript source output in memory. */
18
+ export function transformProjectInMemory(options: ITtscCompilerContext): {
19
+ result: TtscBuildResult;
20
+ typescript: Record<string, string>;
21
+ } {
22
+ const cwd = path.resolve(options.cwd ?? process.cwd());
23
+ const project = readProjectConfig({
24
+ cwd,
25
+ tsconfig: options.tsconfig,
26
+ });
27
+ if (configuredPlugins(options, project).length !== 0) {
28
+ return transformProjectWithPlugins(options, cwd, project);
29
+ }
30
+ return transformProjectWithNativeHost(options, project);
31
+ }
32
+
33
+ function configuredPlugins(
34
+ options: ITtscCompilerContext,
35
+ project: ITtscParsedProjectConfig,
36
+ ): ITtscProjectPluginConfig[] {
37
+ if (options.plugins === false) {
38
+ return [];
39
+ }
40
+ const entries = options.plugins ?? project.compilerOptions.plugins;
41
+ return entries.filter((entry) => entry.enabled !== false);
42
+ }
43
+
44
+ function transformProjectWithNativeHost(
45
+ options: ITtscCompilerContext,
46
+ project: ITtscParsedProjectConfig,
47
+ ): {
48
+ result: TtscBuildResult;
49
+ typescript: Record<string, string>;
50
+ } {
51
+ const binary = buildNativeCompiler({
52
+ cacheBaseDir: project.root,
53
+ cacheDir: options.cacheDir,
54
+ packageRoot: packageRootDir(),
55
+ });
56
+ const res = spawnNative(
57
+ binary,
58
+ ["api-transform", "--cwd", project.root, "--tsconfig", project.path],
59
+ {
60
+ cwd: project.root,
61
+ env: { ...process.env, ...options.env },
62
+ },
63
+ );
64
+ if (res.error) {
65
+ throw new Error(
66
+ `ttsc: failed to spawn native compiler host ${binary}: ${res.error.message}`,
67
+ );
68
+ }
69
+
70
+ const output = parseNativeTransformOutput(
71
+ outputText(res.stdout),
72
+ outputText(res.stderr),
73
+ );
74
+ return {
75
+ result: {
76
+ diagnostics: output.diagnostics,
77
+ status: res.status ?? 1,
78
+ stdout: "",
79
+ stderr: outputText(res.stderr),
80
+ },
81
+ typescript: output.typescript,
82
+ };
83
+ }
84
+
85
+ function transformProjectWithPlugins(
86
+ options: ITtscCompilerContext,
87
+ cwd: string,
88
+ project: ITtscParsedProjectConfig,
89
+ ): {
90
+ result: TtscBuildResult;
91
+ typescript: Record<string, string>;
92
+ } {
93
+ const loaded = loadProjectPlugins({
94
+ binary: resolveBinary(options) ?? "",
95
+ cacheDir: options.cacheDir ?? options.env?.TTSC_CACHE_DIR,
96
+ cwd,
97
+ entries: options.plugins,
98
+ tsconfig: project.path,
99
+ });
100
+ const checks = loaded.nativePlugins.filter(
101
+ (plugin) => plugin.stage === "check",
102
+ );
103
+ const transformers = loaded.nativePlugins.filter(
104
+ (plugin) => plugin.stage === "transform",
105
+ );
106
+ const checked = runNativeChecks(
107
+ options,
108
+ project,
109
+ loaded.nativePlugins,
110
+ checks,
111
+ );
112
+ if (checked.status !== 0) {
113
+ return {
114
+ result: checked,
115
+ typescript: {},
116
+ };
117
+ }
118
+ if (transformers.length === 0) {
119
+ return transformProjectWithNativeHost(options, project);
120
+ }
121
+ assertSingleTransformHost(transformers);
122
+
123
+ const plugin = transformers[0]!;
124
+ const res = spawnNative(
125
+ plugin.binary,
126
+ createNativeTransformArgs(project, transformers),
127
+ {
128
+ cwd: project.root,
129
+ env: nativePluginEnv(options, project.root),
130
+ },
131
+ );
132
+ if (res.error) {
133
+ throw new Error(
134
+ `ttsc.transform: failed to spawn ${plugin.binary}: ${res.error.message}`,
135
+ );
136
+ }
137
+ const output = parseNativeTransformOutput(
138
+ outputText(res.stdout),
139
+ outputText(res.stderr),
140
+ );
141
+ return {
142
+ result: {
143
+ diagnostics: output.diagnostics,
144
+ status: res.status ?? 1,
145
+ stdout: "",
146
+ stderr: outputText(res.stderr),
147
+ },
148
+ typescript: output.typescript,
149
+ };
150
+ }
151
+
152
+ function runNativeChecks(
153
+ options: ITtscCompilerContext,
154
+ project: ITtscParsedProjectConfig,
155
+ nativePlugins: readonly ITtscLoadedNativePlugin[],
156
+ checks: readonly ITtscLoadedNativePlugin[],
157
+ ): TtscBuildResult {
158
+ let result: TtscBuildResult = {
159
+ diagnostics: [],
160
+ status: 0,
161
+ stdout: "",
162
+ stderr: "",
163
+ };
164
+ for (const plugin of checks) {
165
+ const res = spawnNative(
166
+ plugin.binary,
167
+ createNativeCheckArgs(project, nativePlugins),
168
+ {
169
+ cwd: project.root,
170
+ env: nativePluginEnv(options, project.root),
171
+ },
172
+ );
173
+ if (res.error) {
174
+ throw new Error(
175
+ `ttsc.transform.check: failed to spawn ${plugin.binary}: ${res.error.message}`,
176
+ );
177
+ }
178
+ result = appendBuildResult(result, {
179
+ diagnostics: [],
180
+ status: res.status ?? 1,
181
+ stdout: outputText(res.stdout),
182
+ stderr: outputText(res.stderr),
183
+ });
184
+ if (result.status !== 0) {
185
+ return result;
186
+ }
187
+ }
188
+ return result;
189
+ }
190
+
191
+ function createNativeTransformArgs(
192
+ project: ITtscParsedProjectConfig,
193
+ plugins: readonly ITtscLoadedNativePlugin[],
194
+ ): string[] {
195
+ return [
196
+ "transform",
197
+ "--tsconfig=" + project.path,
198
+ "--plugins-json=" + serializeNativePlugins(plugins),
199
+ "--cwd=" + project.root,
200
+ ];
201
+ }
202
+
203
+ function createNativeCheckArgs(
204
+ project: ITtscParsedProjectConfig,
205
+ plugins: readonly ITtscLoadedNativePlugin[],
206
+ ): string[] {
207
+ return [
208
+ "check",
209
+ "--tsconfig=" + project.path,
210
+ "--plugins-json=" + serializeNativePlugins(plugins),
211
+ "--cwd=" + project.root,
212
+ ];
213
+ }
214
+
215
+ function serializeNativePlugins(
216
+ plugins: readonly ITtscLoadedNativePlugin[],
217
+ ): string {
218
+ return JSON.stringify(
219
+ plugins.map((plugin) => ({
220
+ config: plugin.config,
221
+ name: plugin.name,
222
+ stage: plugin.stage,
223
+ })),
224
+ );
225
+ }
226
+
227
+ function assertSingleTransformHost(
228
+ plugins: readonly ITtscLoadedNativePlugin[],
229
+ ): void {
230
+ const binaries = [...new Set(plugins.map((plugin) => plugin.binary))];
231
+ if (binaries.length > 1) {
232
+ throw new Error(
233
+ "ttsc: multiple transform native backends cannot share one source-to-source pass",
234
+ );
235
+ }
236
+ }
237
+
238
+ function appendBuildResult(
239
+ left: TtscBuildResult,
240
+ right: TtscBuildResult,
241
+ ): TtscBuildResult {
242
+ return {
243
+ diagnostics: [...left.diagnostics, ...right.diagnostics],
244
+ status: right.status !== 0 ? right.status : left.status,
245
+ stdout: left.stdout + right.stdout,
246
+ stderr: left.stderr + right.stderr,
247
+ };
248
+ }
249
+
250
+ function nativePluginEnv(
251
+ options: ITtscCompilerContext,
252
+ projectRoot: string,
253
+ ): NodeJS.ProcessEnv {
254
+ const tsgo = resolveTsgo({ ...options, cwd: projectRoot });
255
+ return {
256
+ ...process.env,
257
+ TTSC_NODE_BINARY: process.env.TTSC_NODE_BINARY ?? process.execPath,
258
+ TTSC_TSGO_BINARY: process.env.TTSC_TSGO_BINARY ?? tsgo.binary,
259
+ TTSC_TTSX_BINARY:
260
+ process.env.TTSC_TTSX_BINARY ??
261
+ path.join(__dirname, "..", "..", "launcher", "ttsx.js"),
262
+ ...options.env,
263
+ };
264
+ }
265
+
266
+ function spawnNative(
267
+ binary: string,
268
+ args: readonly string[],
269
+ options: {
270
+ cwd?: string;
271
+ env?: NodeJS.ProcessEnv;
272
+ },
273
+ ) {
274
+ const viaNode = /\.(?:[cm]?js|ts)$/i.test(binary);
275
+ if (!viaNode) {
276
+ ensureExecutable(binary);
277
+ }
278
+ return spawnSync(
279
+ viaNode ? process.execPath : binary,
280
+ viaNode ? [binary, ...args] : [...args],
281
+ {
282
+ cwd: options.cwd,
283
+ encoding: "utf8",
284
+ env: options.env,
285
+ maxBuffer: 1024 * 1024 * 256,
286
+ windowsHide: true,
287
+ },
288
+ );
289
+ }
290
+
291
+ function ensureExecutable(binary: string): void {
292
+ if (process.platform === "win32") {
293
+ return;
294
+ }
295
+ try {
296
+ const mode = fs.statSync(binary).mode & 0o777;
297
+ if ((mode & 0o111) !== 0) {
298
+ return;
299
+ }
300
+ fs.chmodSync(binary, mode | 0o755);
301
+ } catch {
302
+ /* keep the original spawn error path */
303
+ }
304
+ }
305
+
306
+ function outputText(value: string | Buffer | null | undefined): string {
307
+ if (value == null) {
308
+ return "";
309
+ }
310
+ return typeof value === "string" ? value : value.toString("utf8");
311
+ }
312
+
313
+ function parseNativeTransformOutput(
314
+ stdout: string,
315
+ stderr: string,
316
+ ): {
317
+ diagnostics: ITtscCompilerDiagnostic[];
318
+ typescript: Record<string, string>;
319
+ } {
320
+ try {
321
+ const parsed = JSON.parse(stdout) as {
322
+ diagnostics?: ITtscCompilerDiagnostic[];
323
+ typescript?: Record<string, string>;
324
+ };
325
+ if (!isTextRecord(parsed.typescript)) {
326
+ throw new Error(
327
+ "ttsc: native transform host did not return a TypeScript source map",
328
+ );
329
+ }
330
+ return {
331
+ diagnostics: Array.isArray(parsed.diagnostics) ? parsed.diagnostics : [],
332
+ typescript: parsed.typescript,
333
+ };
334
+ } catch (error) {
335
+ if (error instanceof Error && !(error instanceof SyntaxError)) {
336
+ throw error;
337
+ }
338
+ throw new Error(
339
+ (stderr || stdout).trim() ||
340
+ "ttsc: native transform host returned no output",
341
+ );
342
+ }
343
+ }
344
+
345
+ function isTextRecord(value: unknown): value is Record<string, string> {
346
+ return (
347
+ typeof value === "object" &&
348
+ value !== null &&
349
+ Object.values(value).every((entry) => typeof entry === "string")
350
+ );
351
+ }
352
+
353
+ function packageRootDir(): string {
354
+ let current = path.resolve(__dirname);
355
+ while (true) {
356
+ if (
357
+ fs.existsSync(path.join(current, "package.json")) &&
358
+ fs.existsSync(path.join(current, "go.mod"))
359
+ ) {
360
+ return fs.realpathSync.native?.(current) ?? fs.realpathSync(current);
361
+ }
362
+ const parent = path.dirname(current);
363
+ if (parent === current) {
364
+ throw new Error("ttsc: package root not found for native compiler build");
365
+ }
366
+ current = parent;
367
+ }
368
+ }
package/src/index.ts CHANGED
@@ -1,20 +1,11 @@
1
1
  /**
2
2
  * ttsc — public TypeScript entry.
3
3
  *
4
- * Exports:
5
- * - tsgo helpers (`resolveTsgo`, …) — resolve the consuming project's
6
- * `@typescript/native-preview` compiler binary.
7
- * - platform helpers (`resolveBinary`, `installHint`, …) — compatibility
8
- * helper resolution for commands such as `ttsc demo`.
9
- * - programmatic API (`transform`, `build`, `check`, `version`) — a thin
10
- * TS wrapper around the consumer `tsgo` binary and native plugin
11
- * descriptors. Adapters never have to shell out themselves; they call
12
- * these helpers and get back a string or a result record.
4
+ * The package root intentionally exposes only the programmatic compiler class
5
+ * and the plugin-author contracts. CLI launcher functions, binary resolution,
6
+ * project parsing helpers, and native build helpers stay internal so the
7
+ * public package surface remains small and stable.
13
8
  */
14
9
 
15
- export * from "./platform";
16
- export * from "./api";
17
- export * from "./project";
18
- export * from "./plugin";
19
- export * from "./tsgo";
20
- export * from "./runner/register";
10
+ export * from "./TtscCompiler";
11
+ export * from "./structures/index";
@@ -0,0 +1,44 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import * as fs from "node:fs";
3
+ import * as path from "node:path";
4
+
5
+ import { resolveTsgo } from "../../compiler/internal/resolveTsgo";
6
+ import type { TtscCommonOptions } from "../../structures/internal/TtscCommonOptions";
7
+
8
+ /** Format the CLI version banner from the wrapper package and resolved tsgo. */
9
+ export function getCompilerVersionText(
10
+ options: TtscCommonOptions = {},
11
+ ): string {
12
+ const tsgo = resolveTsgo(options);
13
+ const res = spawnSync(tsgo.binary, ["--version"], {
14
+ encoding: "utf8",
15
+ maxBuffer: 1024 * 1024 * 16,
16
+ windowsHide: true,
17
+ });
18
+ if (res.error || res.status !== 0) {
19
+ throw new Error(
20
+ "ttsc.version: failed: " +
21
+ (outputText(res.stderr) || res.error?.message),
22
+ );
23
+ }
24
+ return `ttsc ${readOwnPackageVersion()} (${outputText(res.stdout).trim()})`;
25
+ }
26
+
27
+ function outputText(value: string | Buffer | null | undefined): string {
28
+ if (value == null) {
29
+ return "";
30
+ }
31
+ return typeof value === "string" ? value : value.toString("utf8");
32
+ }
33
+
34
+ function readOwnPackageVersion(): string {
35
+ try {
36
+ const file = path.resolve(__dirname, "..", "..", "..", "package.json");
37
+ const pkg = JSON.parse(fs.readFileSync(file, "utf8")) as {
38
+ version?: string;
39
+ };
40
+ return pkg.version ?? "0.0.0";
41
+ } catch {
42
+ return "0.0.0";
43
+ }
44
+ }
@@ -0,0 +1,115 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+
4
+ import { resolveEmittedJavaScript } from "../../compiler/internal/resolveEmittedJavaScript";
5
+ import { runBuild } from "../../compiler/internal/runBuild";
6
+ import { resolveProjectConfig } from "../../compiler/internal/project/resolveProjectConfig";
7
+ import type { TtscCommonOptions } from "../../structures/internal/TtscCommonOptions";
8
+
9
+ const PROCESS_CACHE_KEY = String(process.pid);
10
+
11
+ /** Build the owning project and locate the emitted JavaScript entry for `ttsx`. */
12
+ export function prepareExecution(
13
+ entryFile: string,
14
+ options: TtscCommonOptions & {
15
+ cacheDir?: string;
16
+ project?: string;
17
+ } = {},
18
+ ): {
19
+ emitDir: string;
20
+ entryFile: string;
21
+ moduleKind: "cjs" | "esm";
22
+ } {
23
+ const context = createProjectContext(
24
+ path.resolve(options.cwd ?? process.cwd()),
25
+ entryFile,
26
+ options,
27
+ );
28
+ buildProject(context, options);
29
+ const emittedEntry = resolveEmittedJavaScript({
30
+ emittedFiles: context.emittedFiles ?? undefined,
31
+ outDir: context.emitDir,
32
+ projectRoot: context.root,
33
+ sourceFile: entryFile,
34
+ });
35
+ if (emittedEntry === null) {
36
+ throw new Error(`ttsx: emitted entry not found for ${entryFile}`);
37
+ }
38
+ const output = fs.readFileSync(emittedEntry, "utf8");
39
+ return {
40
+ emitDir: context.emitDir,
41
+ entryFile: emittedEntry,
42
+ moduleKind: looksLikeESM(output) ? "esm" : "cjs",
43
+ };
44
+ }
45
+
46
+ function createProjectContext(
47
+ cwd: string,
48
+ filename: string,
49
+ options: NonNullable<Parameters<typeof prepareExecution>[1]>,
50
+ ) {
51
+ const tsconfig = options.project
52
+ ? resolveProjectConfig({ cwd, tsconfig: path.resolve(cwd, options.project) })
53
+ : resolveProjectConfig({ cwd, file: filename });
54
+ const root = path.dirname(tsconfig);
55
+ const cacheDir =
56
+ options.cacheDir ?? path.join(root, "node_modules", ".cache", "ttsc", "ttsx");
57
+ return {
58
+ tsconfig,
59
+ root,
60
+ cacheDir,
61
+ emitDir: path.join(cacheDir, "project", PROCESS_CACHE_KEY),
62
+ built: false,
63
+ emittedFiles: undefined as string[] | undefined,
64
+ };
65
+ }
66
+
67
+ function buildProject(
68
+ context: ReturnType<typeof createProjectContext>,
69
+ options: NonNullable<Parameters<typeof prepareExecution>[1]>,
70
+ ): void {
71
+ if (context.built) return;
72
+
73
+ fs.mkdirSync(context.cacheDir, { recursive: true });
74
+ fs.rmSync(context.emitDir, { recursive: true, force: true });
75
+ const result = runBuild({
76
+ binary: options.binary,
77
+ cwd: context.root,
78
+ emit: true,
79
+ env: options.env,
80
+ forceListEmittedFiles: true,
81
+ outDir: context.emitDir,
82
+ plugins: options.plugins,
83
+ quiet: true,
84
+ tsconfig: context.tsconfig,
85
+ });
86
+ if (result.status === 0) {
87
+ context.built = true;
88
+ context.emittedFiles =
89
+ result.emittedFiles && result.emittedFiles.length !== 0
90
+ ? result.emittedFiles
91
+ : undefined;
92
+ return;
93
+ }
94
+
95
+ fs.rmSync(context.emitDir, { recursive: true, force: true });
96
+ const detail = [
97
+ `ttsx: project check failed for ${context.tsconfig}`,
98
+ result.stderr || result.stdout,
99
+ ]
100
+ .filter((line) => line.trim().length !== 0)
101
+ .join("\n");
102
+ throw new Error(detail);
103
+ }
104
+
105
+ function looksLikeESM(output: string): boolean {
106
+ if (
107
+ /\bObject\.defineProperty\(exports\b/.test(output) ||
108
+ /\bmodule\.exports\b/.test(output) ||
109
+ /\brequire\(/.test(output) ||
110
+ /\bexports\./.test(output)
111
+ ) {
112
+ return false;
113
+ }
114
+ return /^\s*(import|export)\s/m.test(output);
115
+ }