ttsc 0.5.0 → 0.6.0-dev.20250430
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/README.md +1 -1
- package/go.mod +1 -1
- package/go.sum +2 -2
- package/lib/api.d.ts +15 -67
- package/lib/api.js +14 -3
- package/lib/api.js.map +1 -1
- package/lib/cli.js.map +1 -1
- package/lib/native.d.ts +8 -20
- package/lib/native.js.map +1 -1
- package/lib/platform.d.ts +7 -25
- package/lib/platform.js.map +1 -1
- package/lib/plugin.d.ts +13 -41
- package/lib/plugin.js +2 -2
- package/lib/plugin.js.map +1 -1
- package/lib/project.d.ts +11 -29
- package/lib/project.js.map +1 -1
- package/lib/runner/cli.js.map +1 -1
- package/lib/runner/register.d.ts +6 -13
- package/lib/runner/register.js.map +1 -1
- package/lib/source-build.d.ts +11 -22
- package/lib/source-build.js +25 -1
- package/lib/source-build.js.map +1 -1
- package/lib/structures/ITtscBuildOptions.d.ts +14 -0
- package/lib/structures/ITtscBuildOptions.js +3 -0
- package/lib/structures/ITtscBuildOptions.js.map +1 -0
- package/lib/structures/ITtscBuildResult.d.ts +6 -0
- package/lib/structures/ITtscBuildResult.js +3 -0
- package/lib/structures/ITtscBuildResult.js.map +1 -0
- package/lib/structures/ITtscBuildSourcePluginOptions.d.ts +8 -0
- package/lib/structures/ITtscBuildSourcePluginOptions.js +3 -0
- package/lib/structures/ITtscBuildSourcePluginOptions.js.map +1 -0
- package/lib/structures/ITtscCacheKeyInput.d.ts +7 -0
- package/lib/structures/ITtscCacheKeyInput.js +3 -0
- package/lib/structures/ITtscCacheKeyInput.js.map +1 -0
- package/lib/structures/ITtscCheckOptions.d.ts +2 -0
- package/lib/structures/ITtscCheckOptions.js +3 -0
- package/lib/structures/ITtscCheckOptions.js.map +1 -0
- package/lib/structures/ITtscCommonOptions.d.ts +22 -0
- package/lib/structures/ITtscCommonOptions.js +3 -0
- package/lib/structures/ITtscCommonOptions.js.map +1 -0
- package/lib/structures/ITtscLoadPluginsOptions.d.ts +8 -0
- package/lib/structures/ITtscLoadPluginsOptions.js +3 -0
- package/lib/structures/ITtscLoadPluginsOptions.js.map +1 -0
- package/lib/structures/ITtscLoadedNativePlugin.d.ts +7 -0
- package/lib/structures/ITtscLoadedNativePlugin.js +3 -0
- package/lib/structures/ITtscLoadedNativePlugin.js.map +1 -0
- package/lib/structures/ITtscLoadedPlugins.d.ts +11 -0
- package/lib/structures/ITtscLoadedPlugins.js +3 -0
- package/lib/structures/ITtscLoadedPlugins.js.map +1 -0
- package/lib/structures/ITtscNativeBackend.d.ts +10 -0
- package/lib/structures/ITtscNativeBackend.js +3 -0
- package/lib/structures/ITtscNativeBackend.js.map +1 -0
- package/lib/structures/ITtscNativePluginContractVersion.d.ts +1 -0
- package/lib/structures/ITtscNativePluginContractVersion.js +3 -0
- package/lib/structures/ITtscNativePluginContractVersion.js.map +1 -0
- package/lib/structures/ITtscNativePluginShape.d.ts +8 -0
- package/lib/structures/ITtscNativePluginShape.js +3 -0
- package/lib/structures/ITtscNativePluginShape.js.map +1 -0
- package/lib/structures/ITtscNativeRewriteMode.d.ts +1 -0
- package/lib/structures/ITtscNativeRewriteMode.js +3 -0
- package/lib/structures/ITtscNativeRewriteMode.js.map +1 -0
- package/lib/structures/ITtscNativeSource.d.ts +4 -0
- package/lib/structures/ITtscNativeSource.js +3 -0
- package/lib/structures/ITtscNativeSource.js.map +1 -0
- package/lib/structures/ITtscParsedProjectConfig.d.ts +9 -0
- package/lib/structures/ITtscParsedProjectConfig.js +3 -0
- package/lib/structures/ITtscParsedProjectConfig.js.map +1 -0
- package/lib/structures/ITtscPlugin.d.ts +10 -0
- package/lib/structures/ITtscPlugin.js +3 -0
- package/lib/structures/ITtscPlugin.js.map +1 -0
- package/lib/structures/ITtscPluginFactory.d.ts +4 -0
- package/lib/structures/ITtscPluginFactory.js +3 -0
- package/lib/structures/ITtscPluginFactory.js.map +1 -0
- package/lib/structures/ITtscPluginFactoryContext.d.ts +8 -0
- package/lib/structures/ITtscPluginFactoryContext.js +3 -0
- package/lib/structures/ITtscPluginFactoryContext.js.map +1 -0
- package/lib/structures/ITtscPluginModule.d.ts +3 -0
- package/lib/structures/ITtscPluginModule.js +3 -0
- package/lib/structures/ITtscPluginModule.js.map +1 -0
- package/lib/structures/ITtscPreparedExecution.d.ts +5 -0
- package/lib/structures/ITtscPreparedExecution.js +3 -0
- package/lib/structures/ITtscPreparedExecution.js.map +1 -0
- package/lib/structures/ITtscProjectLocatorOptions.d.ts +5 -0
- package/lib/structures/ITtscProjectLocatorOptions.js +3 -0
- package/lib/structures/ITtscProjectLocatorOptions.js.map +1 -0
- package/lib/structures/ITtscProjectPluginConfig.d.ts +5 -0
- package/lib/structures/ITtscProjectPluginConfig.js +3 -0
- package/lib/structures/ITtscProjectPluginConfig.js.map +1 -0
- package/lib/structures/ITtscRegisterOptions.d.ts +6 -0
- package/lib/structures/ITtscRegisterOptions.js +3 -0
- package/lib/structures/ITtscRegisterOptions.js.map +1 -0
- package/lib/structures/ITtscResolveGoBinaryOptions.d.ts +7 -0
- package/lib/structures/ITtscResolveGoBinaryOptions.js +3 -0
- package/lib/structures/ITtscResolveGoBinaryOptions.js.map +1 -0
- package/lib/structures/ITtscResolveOptions.d.ts +20 -0
- package/lib/structures/ITtscResolveOptions.js +3 -0
- package/lib/structures/ITtscResolveOptions.js.map +1 -0
- package/lib/structures/ITtscResolvedTsgo.d.ts +8 -0
- package/lib/structures/ITtscResolvedTsgo.js +3 -0
- package/lib/structures/ITtscResolvedTsgo.js.map +1 -0
- package/lib/structures/ITtscTransformOptions.d.ts +12 -0
- package/lib/structures/ITtscTransformOptions.js +3 -0
- package/lib/structures/ITtscTransformOptions.js.map +1 -0
- package/lib/structures/ITtscTsgoResolveOptions.d.ts +14 -0
- package/lib/structures/ITtscTsgoResolveOptions.js +3 -0
- package/lib/structures/ITtscTsgoResolveOptions.js.map +1 -0
- package/lib/tsgo.d.ts +6 -24
- package/lib/tsgo.js.map +1 -1
- package/package.json +10 -10
- package/shim/ast/go.mod +1 -1
- package/shim/ast/lint.go +2 -0
- package/shim/bundled/go.mod +1 -1
- package/shim/checker/go.mod +1 -1
- package/shim/compiler/go.mod +1 -1
- package/shim/core/go.mod +1 -1
- package/shim/diagnosticwriter/go.mod +1 -1
- package/shim/parser/go.mod +1 -1
- package/shim/scanner/go.mod +1 -1
- package/shim/tsoptions/go.mod +1 -1
- package/shim/tspath/go.mod +1 -1
- package/shim/vfs/cachedvfs/go.mod +1 -1
- package/shim/vfs/go.mod +1 -1
- package/shim/vfs/osvfs/go.mod +1 -1
- package/src/api.ts +108 -155
- package/src/cli.ts +10 -10
- package/src/native.ts +9 -23
- package/src/platform.ts +10 -27
- package/src/plugin.ts +37 -74
- package/src/project.ts +18 -37
- package/src/runner/cli.ts +5 -5
- package/src/runner/register.ts +32 -43
- package/src/source-build.ts +45 -25
- package/src/structures/ITtscBuildOptions.ts +19 -0
- package/src/structures/ITtscBuildResult.ts +6 -0
- package/src/structures/ITtscBuildSourcePluginOptions.ts +9 -0
- package/src/structures/ITtscCacheKeyInput.ts +7 -0
- package/src/structures/ITtscCheckOptions.ts +3 -0
- package/src/structures/ITtscCommonOptions.ts +23 -0
- package/src/structures/ITtscLoadPluginsOptions.ts +9 -0
- package/src/structures/ITtscLoadedNativePlugin.ts +8 -0
- package/src/structures/ITtscLoadedPlugins.ts +12 -0
- package/src/structures/ITtscNativeBackend.ts +11 -0
- package/src/structures/ITtscNativePluginContractVersion.ts +1 -0
- package/src/structures/ITtscNativePluginShape.ts +9 -0
- package/src/structures/ITtscNativeRewriteMode.ts +1 -0
- package/src/structures/ITtscNativeSource.ts +4 -0
- package/src/structures/ITtscParsedProjectConfig.ts +10 -0
- package/src/structures/ITtscPlugin.ts +11 -0
- package/src/structures/ITtscPluginFactory.ts +7 -0
- package/src/structures/ITtscPluginFactoryContext.ts +9 -0
- package/src/structures/ITtscPluginModule.ts +4 -0
- package/src/structures/ITtscPreparedExecution.ts +5 -0
- package/src/structures/ITtscProjectLocatorOptions.ts +5 -0
- package/src/structures/ITtscProjectPluginConfig.ts +5 -0
- package/src/structures/ITtscRegisterOptions.ts +7 -0
- package/src/structures/ITtscResolveGoBinaryOptions.ts +7 -0
- package/src/structures/ITtscResolveOptions.ts +20 -0
- package/src/structures/ITtscResolvedTsgo.ts +8 -0
- package/src/structures/ITtscTransformOptions.ts +13 -0
- package/src/structures/ITtscTsgoResolveOptions.ts +14 -0
- package/src/tsgo.ts +6 -24
- package/test/source-build.test.ts +13 -0
package/src/platform.ts
CHANGED
|
@@ -27,36 +27,19 @@
|
|
|
27
27
|
import * as fs from "node:fs";
|
|
28
28
|
import * as path from "node:path";
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* returns an absolute path (or throws).
|
|
34
|
-
*/
|
|
35
|
-
resolver?: (request: string) => string;
|
|
36
|
-
/** Override `process.platform` for testing. */
|
|
37
|
-
platform?: NodeJS.Platform;
|
|
38
|
-
/** Override `process.arch` for testing. */
|
|
39
|
-
arch?: string;
|
|
40
|
-
/** Override `process.env` for testing. */
|
|
41
|
-
env?: NodeJS.ProcessEnv;
|
|
42
|
-
/**
|
|
43
|
-
* Override the package-local scan used for the local `native/ttsc-native`
|
|
44
|
-
* fallback. Returns an absolute path if present, else null.
|
|
45
|
-
*/
|
|
46
|
-
localBinaryLookup?: () => string | null;
|
|
47
|
-
/** Override the module directory used to resolve the package-local binary. */
|
|
48
|
-
moduleDir?: string;
|
|
49
|
-
}
|
|
30
|
+
import type { ITtscResolveOptions } from "./structures/ITtscResolveOptions";
|
|
31
|
+
|
|
32
|
+
export type { ITtscResolveOptions } from "./structures/ITtscResolveOptions";
|
|
50
33
|
|
|
51
34
|
/** Canonical key `{platform}-{arch}` used in the binary package name. */
|
|
52
|
-
export function platformKey(opts:
|
|
35
|
+
export function platformKey(opts: ITtscResolveOptions = {}): string {
|
|
53
36
|
const platform = opts.platform ?? process.platform;
|
|
54
37
|
const arch = opts.arch ?? process.arch;
|
|
55
38
|
return `${platform}-${arch}`;
|
|
56
39
|
}
|
|
57
40
|
|
|
58
41
|
/** Helper binary filename for the current platform (`.exe` on Windows, else none). */
|
|
59
|
-
export function binaryName(opts:
|
|
42
|
+
export function binaryName(opts: ITtscResolveOptions = {}): string {
|
|
60
43
|
const platform = opts.platform ?? process.platform;
|
|
61
44
|
return platform === "win32" ? "ttsc.exe" : "ttsc";
|
|
62
45
|
}
|
|
@@ -66,7 +49,7 @@ export function binaryName(opts: ResolveOptions = {}): string {
|
|
|
66
49
|
* platform. Separated from actual resolution so we can log the request when
|
|
67
50
|
* the dependency is missing.
|
|
68
51
|
*/
|
|
69
|
-
export function platformPackageRequest(opts:
|
|
52
|
+
export function platformPackageRequest(opts: ITtscResolveOptions = {}): string {
|
|
70
53
|
const key = platformKey(opts);
|
|
71
54
|
const bin = binaryName(opts);
|
|
72
55
|
return `@ttsc/${key}/bin/${bin}`;
|
|
@@ -93,7 +76,7 @@ export function isSupported(key: string): boolean {
|
|
|
93
76
|
* order. Returns `null` when every strategy fails — the launcher then prints
|
|
94
77
|
* the install hint and exits non-zero.
|
|
95
78
|
*/
|
|
96
|
-
export function resolveBinary(opts:
|
|
79
|
+
export function resolveBinary(opts: ITtscResolveOptions = {}): string | null {
|
|
97
80
|
const env = opts.env ?? process.env;
|
|
98
81
|
|
|
99
82
|
// 1. Env override. Absolute path is required; a relative value is ignored so
|
|
@@ -126,7 +109,7 @@ export function resolveBinary(opts: ResolveOptions = {}): string | null {
|
|
|
126
109
|
* Human-readable message shown when no binary can be located. Pure string —
|
|
127
110
|
* the launcher writes it to stderr.
|
|
128
111
|
*/
|
|
129
|
-
export function installHint(opts:
|
|
112
|
+
export function installHint(opts: ITtscResolveOptions = {}): string {
|
|
130
113
|
const key = platformKey(opts);
|
|
131
114
|
const pkg = `@ttsc/${key}`;
|
|
132
115
|
const supported = SUPPORTED_PLATFORMS.join(", ");
|
|
@@ -149,7 +132,7 @@ export function installHint(opts: ResolveOptions = {}): string {
|
|
|
149
132
|
].join("\n");
|
|
150
133
|
}
|
|
151
134
|
|
|
152
|
-
function defaultLocalBinaryPath(opts:
|
|
135
|
+
function defaultLocalBinaryPath(opts: ITtscResolveOptions): string | null {
|
|
153
136
|
const root = packageRootDir(opts);
|
|
154
137
|
const candidate = path.resolve(
|
|
155
138
|
root,
|
|
@@ -160,7 +143,7 @@ function defaultLocalBinaryPath(opts: ResolveOptions): string | null {
|
|
|
160
143
|
return fs.existsSync(candidate) ? candidate : null;
|
|
161
144
|
}
|
|
162
145
|
|
|
163
|
-
function packageRootDir(opts:
|
|
146
|
+
function packageRootDir(opts: ITtscResolveOptions = {}): string {
|
|
164
147
|
if (opts.moduleDir) {
|
|
165
148
|
return fs.realpathSync.native?.(opts.moduleDir) ?? fs.realpathSync(opts.moduleDir);
|
|
166
149
|
}
|
package/src/plugin.ts
CHANGED
|
@@ -2,76 +2,37 @@ import { createRequire } from "node:module";
|
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from "./
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
} from "./project";
|
|
5
|
+
import type { ITtscLoadPluginsOptions } from "./structures/ITtscLoadPluginsOptions";
|
|
6
|
+
import type { ITtscLoadedNativePlugin } from "./structures/ITtscLoadedNativePlugin";
|
|
7
|
+
import type { ITtscLoadedPlugins } from "./structures/ITtscLoadedPlugins";
|
|
8
|
+
import type { ITtscPlugin } from "./structures/ITtscPlugin";
|
|
9
|
+
import type { ITtscPluginFactory } from "./structures/ITtscPluginFactory";
|
|
10
|
+
import type { ITtscPluginFactoryContext } from "./structures/ITtscPluginFactoryContext";
|
|
11
|
+
import type { ITtscPluginModule } from "./structures/ITtscPluginModule";
|
|
12
|
+
import type { ITtscProjectPluginConfig } from "./structures/ITtscProjectPluginConfig";
|
|
13
|
+
import { resolveNativeBackend } from "./native";
|
|
14
|
+
import { readProjectConfig } from "./project";
|
|
15
15
|
import { buildSourcePlugin } from "./source-build";
|
|
16
16
|
|
|
17
|
+
export type { ITtscLoadPluginsOptions } from "./structures/ITtscLoadPluginsOptions";
|
|
18
|
+
export type { ITtscLoadedNativePlugin } from "./structures/ITtscLoadedNativePlugin";
|
|
19
|
+
export type { ITtscLoadedPlugins } from "./structures/ITtscLoadedPlugins";
|
|
17
20
|
export type {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
ITtscNativePluginContractVersion,
|
|
22
|
+
ITtscNativeRewriteMode,
|
|
23
|
+
ITtscNativeBackend,
|
|
24
|
+
ITtscNativeSource,
|
|
22
25
|
} from "./native";
|
|
26
|
+
export type { ITtscPlugin } from "./structures/ITtscPlugin";
|
|
27
|
+
export type { ITtscPluginFactory } from "./structures/ITtscPluginFactory";
|
|
28
|
+
export type { ITtscPluginFactoryContext } from "./structures/ITtscPluginFactoryContext";
|
|
29
|
+
export type { ITtscPluginModule } from "./structures/ITtscPluginModule";
|
|
23
30
|
|
|
24
|
-
export
|
|
25
|
-
binary: string;
|
|
26
|
-
cwd: string;
|
|
27
|
-
projectRoot: string;
|
|
28
|
-
tsconfig: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface TtscPlugin {
|
|
32
|
-
name: string;
|
|
33
|
-
native?: TtscNativeBackend;
|
|
34
|
-
/** @deprecated Use `native.mode` instead. */
|
|
35
|
-
nativeMode?: NativeRewriteMode;
|
|
36
|
-
/** @deprecated Use `native.binary` instead. */
|
|
37
|
-
nativeBinary?: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export type TtscPluginFactory = (
|
|
41
|
-
config: ProjectPluginConfig,
|
|
42
|
-
context: TtscPluginFactoryContext,
|
|
43
|
-
) => TtscPlugin;
|
|
44
|
-
|
|
45
|
-
export type TtscPluginModule = TtscPlugin | TtscPluginFactory;
|
|
46
|
-
|
|
47
|
-
export interface LoadedNativePlugin {
|
|
48
|
-
backend: TtscNativeBackend;
|
|
49
|
-
config: ProjectPluginConfig;
|
|
50
|
-
name: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface LoadedPlugins {
|
|
54
|
-
compatibilityFallback: boolean;
|
|
55
|
-
nativeBinary: string | null;
|
|
56
|
-
nativeBinaries: string[];
|
|
57
|
-
nativePlugins: LoadedNativePlugin[];
|
|
58
|
-
plugins: TtscPlugin[];
|
|
59
|
-
project: ParsedProjectConfig;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export interface LoadPluginsOptions {
|
|
63
|
-
binary: string;
|
|
64
|
-
cwd?: string;
|
|
65
|
-
entries?: readonly ProjectPluginConfig[] | false;
|
|
66
|
-
file?: string;
|
|
67
|
-
tsconfig?: string;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function definePlugin<T extends TtscPluginModule>(plugin: T): T {
|
|
31
|
+
export function definePlugin<T extends ITtscPluginModule>(plugin: T): T {
|
|
71
32
|
return plugin;
|
|
72
33
|
}
|
|
73
34
|
|
|
74
|
-
export function loadProjectPlugins(options:
|
|
35
|
+
export function loadProjectPlugins(options: ITtscLoadPluginsOptions): ITtscLoadedPlugins {
|
|
75
36
|
const project = readProjectConfig({
|
|
76
37
|
cwd: options.cwd,
|
|
77
38
|
file: options.file,
|
|
@@ -94,15 +55,17 @@ export function loadProjectPlugins(options: LoadPluginsOptions): LoadedPlugins {
|
|
|
94
55
|
};
|
|
95
56
|
}
|
|
96
57
|
|
|
97
|
-
const context
|
|
58
|
+
const context = {
|
|
98
59
|
binary: options.binary,
|
|
99
60
|
cwd: path.resolve(options.cwd ?? process.cwd()),
|
|
100
61
|
projectRoot: project.root,
|
|
101
62
|
tsconfig: project.path,
|
|
102
63
|
};
|
|
103
|
-
const plugins = entries.map((entry) =>
|
|
64
|
+
const plugins = entries.map((entry) =>
|
|
65
|
+
loadPluginEntry(entry, { ...context, plugin: entry }),
|
|
66
|
+
);
|
|
104
67
|
|
|
105
|
-
const nativePlugins:
|
|
68
|
+
const nativePlugins: ITtscLoadedNativePlugin[] = [];
|
|
106
69
|
const ttscVersion = readTtscVersion();
|
|
107
70
|
const tsgoVersion = readTsgoVersion(context.projectRoot);
|
|
108
71
|
plugins.forEach((plugin, index) => {
|
|
@@ -148,9 +111,9 @@ export function loadProjectPlugins(options: LoadPluginsOptions): LoadedPlugins {
|
|
|
148
111
|
}
|
|
149
112
|
|
|
150
113
|
function loadPluginEntry(
|
|
151
|
-
entry:
|
|
152
|
-
context:
|
|
153
|
-
):
|
|
114
|
+
entry: ITtscProjectPluginConfig,
|
|
115
|
+
context: ITtscPluginFactoryContext,
|
|
116
|
+
): ITtscPlugin {
|
|
154
117
|
const specifier = entry.transform;
|
|
155
118
|
if (typeof specifier !== "string" || specifier.length === 0) {
|
|
156
119
|
throw new Error(`ttsc: plugin entry is missing a string "transform" field`);
|
|
@@ -158,16 +121,16 @@ function loadPluginEntry(
|
|
|
158
121
|
|
|
159
122
|
const request = resolvePluginRequest(specifier, context.projectRoot);
|
|
160
123
|
const mod = require(request) as {
|
|
161
|
-
createTtscPlugin?:
|
|
162
|
-
default?:
|
|
163
|
-
} & Partial<Record<"plugin",
|
|
124
|
+
createTtscPlugin?: ITtscPluginFactory;
|
|
125
|
+
default?: ITtscPluginModule;
|
|
126
|
+
} & Partial<Record<"plugin", ITtscPluginModule>>;
|
|
164
127
|
const candidate =
|
|
165
128
|
mod.createTtscPlugin ??
|
|
166
129
|
mod.default ??
|
|
167
130
|
mod.plugin ??
|
|
168
|
-
(mod as unknown as
|
|
131
|
+
(mod as unknown as ITtscPluginModule);
|
|
169
132
|
if (typeof candidate === "function") {
|
|
170
|
-
const plugin = candidate(
|
|
133
|
+
const plugin = candidate(context);
|
|
171
134
|
if (!isTtscPlugin(plugin)) {
|
|
172
135
|
throw new Error(
|
|
173
136
|
`ttsc: plugin "${specifier}" does not export a valid ttsc plugin`,
|
|
@@ -185,7 +148,7 @@ function loadPluginEntry(
|
|
|
185
148
|
);
|
|
186
149
|
}
|
|
187
150
|
|
|
188
|
-
function isTtscPlugin(value: unknown): value is
|
|
151
|
+
function isTtscPlugin(value: unknown): value is ITtscPlugin {
|
|
189
152
|
return (
|
|
190
153
|
typeof value === "object" &&
|
|
191
154
|
value !== null &&
|
package/src/project.ts
CHANGED
|
@@ -3,26 +3,15 @@ import * as fs from "node:fs";
|
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import * as path from "node:path";
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface ProjectPluginConfig {
|
|
13
|
-
enabled?: boolean;
|
|
14
|
-
transform?: string;
|
|
15
|
-
[key: string]: unknown;
|
|
16
|
-
}
|
|
6
|
+
import type { ITtscCacheKeyInput } from "./structures/ITtscCacheKeyInput";
|
|
7
|
+
import type { ITtscParsedProjectConfig } from "./structures/ITtscParsedProjectConfig";
|
|
8
|
+
import type { ITtscProjectLocatorOptions } from "./structures/ITtscProjectLocatorOptions";
|
|
9
|
+
import type { ITtscProjectPluginConfig } from "./structures/ITtscProjectPluginConfig";
|
|
17
10
|
|
|
18
|
-
export
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
} & Record<string, unknown>;
|
|
23
|
-
path: string;
|
|
24
|
-
root: string;
|
|
25
|
-
}
|
|
11
|
+
export type { ITtscCacheKeyInput } from "./structures/ITtscCacheKeyInput";
|
|
12
|
+
export type { ITtscParsedProjectConfig } from "./structures/ITtscParsedProjectConfig";
|
|
13
|
+
export type { ITtscProjectLocatorOptions } from "./structures/ITtscProjectLocatorOptions";
|
|
14
|
+
export type { ITtscProjectPluginConfig } from "./structures/ITtscProjectPluginConfig";
|
|
26
15
|
|
|
27
16
|
export function resolveWorkingDirectory(cwd?: string): string {
|
|
28
17
|
return path.resolve(cwd ?? process.cwd());
|
|
@@ -32,7 +21,7 @@ export function resolveAbsolutePath(cwd: string, target: string): string {
|
|
|
32
21
|
return path.isAbsolute(target) ? target : path.resolve(cwd, target);
|
|
33
22
|
}
|
|
34
23
|
|
|
35
|
-
export function resolveProjectConfig(opts:
|
|
24
|
+
export function resolveProjectConfig(opts: ITtscProjectLocatorOptions = {}): string {
|
|
36
25
|
const cwd = resolveWorkingDirectory(opts.cwd);
|
|
37
26
|
if (opts.tsconfig) {
|
|
38
27
|
const resolved = resolveAbsolutePath(cwd, opts.tsconfig);
|
|
@@ -55,7 +44,7 @@ export function resolveProjectConfig(opts: ProjectLocatorOptions = {}): string {
|
|
|
55
44
|
return resolveRealPath(found);
|
|
56
45
|
}
|
|
57
46
|
|
|
58
|
-
export function resolveProjectRoot(opts:
|
|
47
|
+
export function resolveProjectRoot(opts: ITtscProjectLocatorOptions = {}): string {
|
|
59
48
|
return path.dirname(resolveProjectConfig(opts));
|
|
60
49
|
}
|
|
61
50
|
|
|
@@ -63,7 +52,7 @@ export function defaultCacheDirectory(projectRoot: string, tool: string): string
|
|
|
63
52
|
return path.join(projectRoot, "node_modules", ".cache", "ttsc", tool);
|
|
64
53
|
}
|
|
65
54
|
|
|
66
|
-
export function readProjectConfig(opts:
|
|
55
|
+
export function readProjectConfig(opts: ITtscProjectLocatorOptions = {}): ITtscParsedProjectConfig {
|
|
67
56
|
const tsconfig = resolveProjectConfig(opts);
|
|
68
57
|
const root = path.dirname(tsconfig);
|
|
69
58
|
const compilerOptions = readResolvedCompilerOptions(tsconfig);
|
|
@@ -78,15 +67,7 @@ export function readProjectConfig(opts: ProjectLocatorOptions = {}): ParsedProje
|
|
|
78
67
|
};
|
|
79
68
|
}
|
|
80
69
|
|
|
81
|
-
export
|
|
82
|
-
file: string;
|
|
83
|
-
tsconfig: string;
|
|
84
|
-
version: string;
|
|
85
|
-
mode?: string;
|
|
86
|
-
extra?: readonly string[];
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function createTransformCacheKey(input: CacheKeyInput): string {
|
|
70
|
+
export function createTransformCacheKey(input: ITtscCacheKeyInput): string {
|
|
90
71
|
const hash = crypto.createHash("sha256");
|
|
91
72
|
hash.update("file\0");
|
|
92
73
|
hash.update(fs.readFileSync(input.file));
|
|
@@ -125,11 +106,11 @@ function isDirectory(location: string): boolean {
|
|
|
125
106
|
}
|
|
126
107
|
}
|
|
127
108
|
|
|
128
|
-
function isProjectPluginConfig(value: unknown): value is
|
|
109
|
+
function isProjectPluginConfig(value: unknown): value is ITtscProjectPluginConfig {
|
|
129
110
|
return typeof value === "object" && value !== null;
|
|
130
111
|
}
|
|
131
112
|
|
|
132
|
-
interface
|
|
113
|
+
interface ITtscRawTsconfig {
|
|
133
114
|
extends?: unknown;
|
|
134
115
|
compilerOptions?: Record<string, unknown> & {
|
|
135
116
|
outDir?: unknown;
|
|
@@ -137,10 +118,10 @@ interface RawTsconfig {
|
|
|
137
118
|
};
|
|
138
119
|
}
|
|
139
120
|
|
|
140
|
-
interface
|
|
121
|
+
interface ITtscResolvedCompilerOptions {
|
|
141
122
|
options: Record<string, unknown>;
|
|
142
123
|
outDir?: string;
|
|
143
|
-
plugins:
|
|
124
|
+
plugins: ITtscProjectPluginConfig[];
|
|
144
125
|
}
|
|
145
126
|
|
|
146
127
|
function resolveRealPath(location: string): string {
|
|
@@ -154,14 +135,14 @@ function resolveRealPath(location: string): string {
|
|
|
154
135
|
function readResolvedCompilerOptions(
|
|
155
136
|
tsconfig: string,
|
|
156
137
|
seen: Set<string> = new Set(),
|
|
157
|
-
):
|
|
138
|
+
): ITtscResolvedCompilerOptions {
|
|
158
139
|
const canonical = resolveRealPath(tsconfig);
|
|
159
140
|
if (seen.has(canonical)) {
|
|
160
141
|
throw new Error(`ttsc: circular tsconfig extends detected: ${canonical}`);
|
|
161
142
|
}
|
|
162
143
|
seen.add(canonical);
|
|
163
144
|
|
|
164
|
-
const parsed = parseJsonc(fs.readFileSync(canonical, "utf8")) as
|
|
145
|
+
const parsed = parseJsonc(fs.readFileSync(canonical, "utf8")) as ITtscRawTsconfig;
|
|
165
146
|
const own = parsed.compilerOptions;
|
|
166
147
|
const base =
|
|
167
148
|
typeof parsed.extends === "string"
|
package/src/runner/cli.ts
CHANGED
|
@@ -7,11 +7,11 @@ import { version } from "../api";
|
|
|
7
7
|
import {
|
|
8
8
|
prepareExecution,
|
|
9
9
|
register,
|
|
10
|
-
type
|
|
11
|
-
type
|
|
10
|
+
type ITtscPreparedExecution,
|
|
11
|
+
type ITtscRegisterOptions,
|
|
12
12
|
} from "./register";
|
|
13
13
|
|
|
14
|
-
interface
|
|
14
|
+
interface ITtscParsedCLI extends ITtscRegisterOptions {
|
|
15
15
|
entry: string;
|
|
16
16
|
passthrough: string[];
|
|
17
17
|
preload: string[];
|
|
@@ -80,7 +80,7 @@ function formatError(error: unknown): string {
|
|
|
80
80
|
return String(error);
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
function parseCLI(argv: readonly string[]):
|
|
83
|
+
function parseCLI(argv: readonly string[]): ITtscParsedCLI | "help" | "version" {
|
|
84
84
|
const preload: string[] = [];
|
|
85
85
|
const passthroughIndex = argv.indexOf("--");
|
|
86
86
|
const head = passthroughIndex === -1 ? [...argv] : [...argv.slice(0, passthroughIndex)];
|
|
@@ -199,7 +199,7 @@ function takeValue(flag: string, rest: string[]): string {
|
|
|
199
199
|
return value;
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
function runEsmEntry(parsed:
|
|
202
|
+
function runEsmEntry(parsed: ITtscParsedCLI, execution: ITtscPreparedExecution, cwd: string): number {
|
|
203
203
|
fs.mkdirSync(execution.emitDir, { recursive: true });
|
|
204
204
|
rewriteEsmSpecifiers(execution.emitDir);
|
|
205
205
|
fs.writeFileSync(
|
package/src/runner/register.ts
CHANGED
|
@@ -1,35 +1,24 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
2
|
import * as path from "node:path";
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type CommonOptions,
|
|
8
|
-
} from "../api";
|
|
4
|
+
import type { ITtscPreparedExecution } from "../structures/ITtscPreparedExecution";
|
|
5
|
+
import type { ITtscRegisterOptions } from "../structures/ITtscRegisterOptions";
|
|
6
|
+
import { build, check } from "../api";
|
|
9
7
|
import {
|
|
10
8
|
defaultCacheDirectory,
|
|
11
9
|
resolveProjectConfig,
|
|
12
10
|
resolveProjectRoot,
|
|
13
11
|
} from "../project";
|
|
14
12
|
|
|
15
|
-
export
|
|
16
|
-
|
|
17
|
-
project?: string;
|
|
18
|
-
extensions?: readonly string[];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface PreparedExecution {
|
|
22
|
-
emitDir: string;
|
|
23
|
-
entryFile: string;
|
|
24
|
-
moduleKind: "cjs" | "esm";
|
|
25
|
-
}
|
|
13
|
+
export type { ITtscPreparedExecution } from "../structures/ITtscPreparedExecution";
|
|
14
|
+
export type { ITtscRegisterOptions } from "../structures/ITtscRegisterOptions";
|
|
26
15
|
|
|
27
|
-
type
|
|
28
|
-
type
|
|
16
|
+
type ITtscRequireExtension = (module: NodeJS.Module, filename: string) => void;
|
|
17
|
+
type ITtscCompilableModule = NodeJS.Module & {
|
|
29
18
|
_compile(code: string, filename: string): void;
|
|
30
19
|
};
|
|
31
20
|
|
|
32
|
-
interface
|
|
21
|
+
interface ITtscRunnerProjectContext {
|
|
33
22
|
cacheDir: string;
|
|
34
23
|
emitDir: string;
|
|
35
24
|
emittedFiles: string[] | null;
|
|
@@ -47,15 +36,15 @@ const DEFAULT_EXTENSIONS: readonly string[] = Object.freeze([
|
|
|
47
36
|
]);
|
|
48
37
|
|
|
49
38
|
const PROCESS_CACHE_KEY = String(process.pid);
|
|
50
|
-
const preparedContexts = new Map<string,
|
|
39
|
+
const preparedContexts = new Map<string, ITtscRunnerProjectContext>();
|
|
51
40
|
|
|
52
|
-
export function register(options:
|
|
41
|
+
export function register(options: ITtscRegisterOptions = {}): () => void {
|
|
53
42
|
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
54
43
|
const extensions = [...(options.extensions ?? DEFAULT_EXTENSIONS)];
|
|
55
|
-
const contextCache = new Map<string,
|
|
56
|
-
const previous = new Map<string,
|
|
44
|
+
const contextCache = new Map<string, ITtscRunnerProjectContext>();
|
|
45
|
+
const previous = new Map<string, ITtscRequireExtension | undefined>();
|
|
57
46
|
|
|
58
|
-
const getContext = (filename: string):
|
|
47
|
+
const getContext = (filename: string): ITtscRunnerProjectContext => {
|
|
59
48
|
if (options.project) {
|
|
60
49
|
const key = path.resolve(cwd, options.project);
|
|
61
50
|
const cached = contextCache.get(key);
|
|
@@ -120,7 +109,7 @@ export function register(options: RegisterOptions = {}): () => void {
|
|
|
120
109
|
previous.set(extension, require.extensions[extension]);
|
|
121
110
|
require.extensions[extension] = (module: NodeJS.Module, filename: string) => {
|
|
122
111
|
const compiled = compile(filename);
|
|
123
|
-
(module as
|
|
112
|
+
(module as ITtscCompilableModule)._compile(compiled, filename);
|
|
124
113
|
};
|
|
125
114
|
}
|
|
126
115
|
|
|
@@ -138,8 +127,8 @@ export function register(options: RegisterOptions = {}): () => void {
|
|
|
138
127
|
|
|
139
128
|
export function prepareExecution(
|
|
140
129
|
entryFile: string,
|
|
141
|
-
options:
|
|
142
|
-
):
|
|
130
|
+
options: ITtscRegisterOptions = {},
|
|
131
|
+
): ITtscPreparedExecution {
|
|
143
132
|
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
144
133
|
const context = resolveProjectContext(cwd, entryFile, options);
|
|
145
134
|
ensureProjectDiagnostics(context, options);
|
|
@@ -166,8 +155,8 @@ export function prepareExecution(
|
|
|
166
155
|
}
|
|
167
156
|
|
|
168
157
|
function storePreparedContext(
|
|
169
|
-
context:
|
|
170
|
-
options:
|
|
158
|
+
context: ITtscRunnerProjectContext,
|
|
159
|
+
options: ITtscRegisterOptions,
|
|
171
160
|
): void {
|
|
172
161
|
preparedContexts.set(preparedContextKey(context.tsconfig, context.cacheDir, options), context);
|
|
173
162
|
}
|
|
@@ -175,8 +164,8 @@ function storePreparedContext(
|
|
|
175
164
|
function takePreparedContext(
|
|
176
165
|
tsconfig: string,
|
|
177
166
|
cacheDir: string,
|
|
178
|
-
options:
|
|
179
|
-
):
|
|
167
|
+
options: ITtscRegisterOptions,
|
|
168
|
+
): ITtscRunnerProjectContext | null {
|
|
180
169
|
const key = preparedContextKey(tsconfig, cacheDir, options);
|
|
181
170
|
const context = preparedContexts.get(key);
|
|
182
171
|
if (!context) {
|
|
@@ -189,7 +178,7 @@ function takePreparedContext(
|
|
|
189
178
|
function preparedContextKey(
|
|
190
179
|
tsconfig: string,
|
|
191
180
|
cacheDir: string,
|
|
192
|
-
options:
|
|
181
|
+
options: ITtscRegisterOptions,
|
|
193
182
|
): string {
|
|
194
183
|
return JSON.stringify([
|
|
195
184
|
PROCESS_CACHE_KEY,
|
|
@@ -203,7 +192,7 @@ function preparedContextKey(
|
|
|
203
192
|
]);
|
|
204
193
|
}
|
|
205
194
|
|
|
206
|
-
function ensureProjectBuild(context:
|
|
195
|
+
function ensureProjectBuild(context: ITtscRunnerProjectContext, options: ITtscRegisterOptions): void {
|
|
207
196
|
if (context.emittedFiles !== null) return;
|
|
208
197
|
|
|
209
198
|
fs.mkdirSync(context.cacheDir, { recursive: true });
|
|
@@ -236,9 +225,9 @@ function ensureProjectBuild(context: ProjectContext, options: RegisterOptions):
|
|
|
236
225
|
}
|
|
237
226
|
|
|
238
227
|
function readCompiledOutput(
|
|
239
|
-
context:
|
|
228
|
+
context: ITtscRunnerProjectContext,
|
|
240
229
|
filename: string,
|
|
241
|
-
options:
|
|
230
|
+
options: ITtscRegisterOptions,
|
|
242
231
|
): string {
|
|
243
232
|
ensureProjectBuild(context, options);
|
|
244
233
|
const emitted = resolveEmittedFile(context, filename);
|
|
@@ -251,8 +240,8 @@ function readCompiledOutput(
|
|
|
251
240
|
function resolveProjectContext(
|
|
252
241
|
cwd: string,
|
|
253
242
|
filename: string,
|
|
254
|
-
options:
|
|
255
|
-
):
|
|
243
|
+
options: ITtscRegisterOptions,
|
|
244
|
+
): ITtscRunnerProjectContext {
|
|
256
245
|
if (options.project) {
|
|
257
246
|
const tsconfig = resolveProjectConfig({
|
|
258
247
|
cwd,
|
|
@@ -267,8 +256,8 @@ function resolveProjectContext(
|
|
|
267
256
|
function createProjectContext(
|
|
268
257
|
cwd: string,
|
|
269
258
|
tsconfig: string,
|
|
270
|
-
options:
|
|
271
|
-
):
|
|
259
|
+
options: ITtscRegisterOptions,
|
|
260
|
+
): ITtscRunnerProjectContext {
|
|
272
261
|
const root = resolveProjectRoot({ cwd, tsconfig });
|
|
273
262
|
const cacheDir = options.cacheDir ?? defaultCacheDirectory(root, "ttsx");
|
|
274
263
|
return {
|
|
@@ -283,8 +272,8 @@ function createProjectContext(
|
|
|
283
272
|
}
|
|
284
273
|
|
|
285
274
|
function ensureProjectDiagnostics(
|
|
286
|
-
context:
|
|
287
|
-
options:
|
|
275
|
+
context: ITtscRunnerProjectContext,
|
|
276
|
+
options: ITtscRegisterOptions,
|
|
288
277
|
): void {
|
|
289
278
|
if (context.diagnosticsChecked) {
|
|
290
279
|
return;
|
|
@@ -310,7 +299,7 @@ function ensureProjectDiagnostics(
|
|
|
310
299
|
context.diagnosticsChecked = true;
|
|
311
300
|
}
|
|
312
301
|
|
|
313
|
-
function resolveEmittedFile(context:
|
|
302
|
+
function resolveEmittedFile(context: ITtscRunnerProjectContext, filename: string): string | null {
|
|
314
303
|
const normalized = path.resolve(filename);
|
|
315
304
|
const cached = context.entryMap.get(normalized);
|
|
316
305
|
if (cached && fs.existsSync(cached)) return cached;
|
|
@@ -339,7 +328,7 @@ function resolveEmittedFile(context: ProjectContext, filename: string): string |
|
|
|
339
328
|
}
|
|
340
329
|
|
|
341
330
|
function resolveExactEmittedFile(
|
|
342
|
-
context:
|
|
331
|
+
context: ITtscRunnerProjectContext,
|
|
343
332
|
filename: string,
|
|
344
333
|
): string | null {
|
|
345
334
|
const relative = path.relative(context.root, filename);
|