@smoothbricks/nx-plugin 0.3.3 → 0.3.5
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 +40 -8
- package/dist/bounded-test-policy.d.ts +4 -2
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +93 -15
- package/dist/executors/typescript-emit/executor.d.ts +17 -0
- package/dist/executors/typescript-emit/executor.d.ts.map +1 -0
- package/dist/executors/typescript-emit/executor.js +61 -0
- package/dist/executors/typescript-emit/schema.d.ts +5 -0
- package/dist/executors/typescript-emit/schema.d.ts.map +1 -0
- package/dist/executors/typescript-emit/schema.js +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +278 -28
- package/dist/package-target-policy.d.ts +2 -0
- package/dist/package-target-policy.d.ts.map +1 -1
- package/dist/package-target-policy.js +5 -1
- package/dist/version-actions.cjs +13 -39
- package/dist/version-actions.d.cts.map +1 -1
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +11 -3
- package/executors.json +5 -0
- package/package.json +4 -3
- package/src/bounded-test-policy.test.ts +102 -2
- package/src/bounded-test-policy.ts +110 -17
- package/src/executors/typescript-emit/executor.test.ts +98 -0
- package/src/executors/typescript-emit/executor.ts +94 -0
- package/src/executors/typescript-emit/schema.json +19 -0
- package/src/executors/typescript-emit/schema.ts +4 -0
- package/src/index.test.ts +157 -17
- package/src/index.ts +356 -30
- package/src/package-target-policy.test.ts +17 -0
- package/src/package-target-policy.ts +5 -1
- package/src/version-actions.cts +13 -39
- package/src/workspace-config-policy.test.ts +41 -1
- package/src/workspace-config-policy.ts +12 -3
|
@@ -18,8 +18,10 @@ import {
|
|
|
18
18
|
ensureBunTestTimeoutFlag,
|
|
19
19
|
resolveTestCommand,
|
|
20
20
|
} from './bounded-test-policy.js';
|
|
21
|
+
import type { ResolvedProjectTargets } from './package-target-policy.js';
|
|
21
22
|
|
|
22
23
|
const TIMEOUT_FLAG = `--timeout=${BOUNDED_TEST_PER_TEST_TIMEOUT_MS}`;
|
|
24
|
+
const BOUNDED_POLICY_MESSAGE = `nx.targets.test must use ${BOUNDED_TEST_EXECUTOR} or delegate through no-op targets to bounded test execution`;
|
|
23
25
|
|
|
24
26
|
describe('bounded test target policy', () => {
|
|
25
27
|
it('preserves an existing nx:run-commands test command', () => {
|
|
@@ -156,6 +158,50 @@ describe('bounded test target policy', () => {
|
|
|
156
158
|
expect(checkBoundedTestTargetPolicy(packageJson, { projectName: 'example', projectJson })).toBe(false);
|
|
157
159
|
});
|
|
158
160
|
|
|
161
|
+
it('accepts an executorless aggregate whose resolved execution target is bounded', () => {
|
|
162
|
+
const packageJson: BoundedTestPolicyPackageJson = {
|
|
163
|
+
nx: { targets: { test: { dependsOn: ['napi-test'] } } },
|
|
164
|
+
};
|
|
165
|
+
const resolvedProject = resolvedAggregateProject();
|
|
166
|
+
|
|
167
|
+
expect(checkBoundedTestTargetPolicy(packageJson, { projectName: 'cowshed', resolvedProject })).toBe(true);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('rejects aggregate bypass scripts, unbounded leaves, and dependency cycles', () => {
|
|
171
|
+
const packageJson: BoundedTestPolicyPackageJson = {
|
|
172
|
+
nx: { targets: { test: { dependsOn: ['napi-test'] } } },
|
|
173
|
+
};
|
|
174
|
+
const unboundedLeaf = resolvedAggregateProject();
|
|
175
|
+
unboundedLeaf.targetExecutors = new Map([
|
|
176
|
+
['test', 'nx:noop'],
|
|
177
|
+
['napi-test', 'nx:run-commands'],
|
|
178
|
+
['build', 'nx:run-commands'],
|
|
179
|
+
]);
|
|
180
|
+
expect(checkBoundedTestTargetPolicy(packageJson, { projectName: 'cowshed', resolvedProject: unboundedLeaf })).toBe(
|
|
181
|
+
false,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
packageJson.scripts = { test: 'bun test' };
|
|
185
|
+
expect(
|
|
186
|
+
checkBoundedTestTargetPolicy(packageJson, {
|
|
187
|
+
projectName: 'cowshed',
|
|
188
|
+
resolvedProject: resolvedAggregateProject(),
|
|
189
|
+
}),
|
|
190
|
+
).toBe(false);
|
|
191
|
+
|
|
192
|
+
delete packageJson.scripts;
|
|
193
|
+
const cycle = resolvedAggregateProject();
|
|
194
|
+
cycle.targetExecutors = new Map([
|
|
195
|
+
['test', 'nx:noop'],
|
|
196
|
+
['napi-test', 'nx:noop'],
|
|
197
|
+
]);
|
|
198
|
+
cycle.targetDependencies = new Map([
|
|
199
|
+
['test', ['napi-test']],
|
|
200
|
+
['napi-test', ['test']],
|
|
201
|
+
]);
|
|
202
|
+
expect(checkBoundedTestTargetPolicy(packageJson, { projectName: 'cowshed', resolvedProject: cycle })).toBe(false);
|
|
203
|
+
});
|
|
204
|
+
|
|
159
205
|
it('does not preserve recursive nx run aliases as commands', () => {
|
|
160
206
|
const packageJson: BoundedTestPolicyPackageJson = {
|
|
161
207
|
scripts: { test: boundedTestScriptAlias('example') },
|
|
@@ -194,7 +240,7 @@ describe('bounded test target policy', () => {
|
|
|
194
240
|
expect(checkWorkspaceBoundedTestTargetPolicy(root)).toEqual([
|
|
195
241
|
{
|
|
196
242
|
path: join(root, 'packages/app/package.json'),
|
|
197
|
-
message:
|
|
243
|
+
message: BOUNDED_POLICY_MESSAGE,
|
|
198
244
|
},
|
|
199
245
|
]);
|
|
200
246
|
|
|
@@ -217,6 +263,32 @@ describe('bounded test target policy', () => {
|
|
|
217
263
|
}
|
|
218
264
|
});
|
|
219
265
|
|
|
266
|
+
it('preserves a resolved bounded aggregate during workspace updates', async () => {
|
|
267
|
+
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-bounded-policy-'));
|
|
268
|
+
try {
|
|
269
|
+
await writeJson(join(root, 'package.json'), {
|
|
270
|
+
name: '@scope/root',
|
|
271
|
+
private: true,
|
|
272
|
+
workspaces: ['packages/*'],
|
|
273
|
+
});
|
|
274
|
+
const packagePath = join(root, 'packages/cowshed/package.json');
|
|
275
|
+
await writeJson(packagePath, {
|
|
276
|
+
name: '@scope/cowshed',
|
|
277
|
+
nx: { name: 'cowshed', targets: { test: { dependsOn: ['napi-test'] } } },
|
|
278
|
+
});
|
|
279
|
+
const options = { resolvedTargetsByProject: new Map([['cowshed', resolvedAggregateProject()]]) };
|
|
280
|
+
|
|
281
|
+
expect(checkWorkspaceBoundedTestTargetPolicy(root, options)).toEqual([]);
|
|
282
|
+
expect(applyWorkspaceBoundedTestTargetPolicy(root, options)).toBe(false);
|
|
283
|
+
expect(JSON.parse(await readFile(packagePath, 'utf8'))).toEqual({
|
|
284
|
+
name: '@scope/cowshed',
|
|
285
|
+
nx: { name: 'cowshed', targets: { test: { dependsOn: ['napi-test'] } } },
|
|
286
|
+
});
|
|
287
|
+
} finally {
|
|
288
|
+
await rm(root, { recursive: true, force: true });
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
|
|
220
292
|
it('checks and fixes workspace project.json test targets', async () => {
|
|
221
293
|
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-bounded-policy-'));
|
|
222
294
|
try {
|
|
@@ -242,7 +314,7 @@ describe('bounded test target policy', () => {
|
|
|
242
314
|
expect(checkWorkspaceBoundedTestTargetPolicy(root)).toEqual([
|
|
243
315
|
{
|
|
244
316
|
path: join(root, 'packages/app/project.json'),
|
|
245
|
-
message:
|
|
317
|
+
message: BOUNDED_POLICY_MESSAGE.replace('nx.targets', 'targets'),
|
|
246
318
|
},
|
|
247
319
|
]);
|
|
248
320
|
|
|
@@ -268,6 +340,34 @@ describe('bounded test target policy', () => {
|
|
|
268
340
|
});
|
|
269
341
|
});
|
|
270
342
|
|
|
343
|
+
function resolvedAggregateProject(): ResolvedProjectTargets {
|
|
344
|
+
return {
|
|
345
|
+
root: 'packages/cowshed',
|
|
346
|
+
targets: new Set(['test', 'napi-test', 'build']),
|
|
347
|
+
targetDependencies: new Map([
|
|
348
|
+
['test', ['napi-test']],
|
|
349
|
+
['napi-test', ['build']],
|
|
350
|
+
]),
|
|
351
|
+
targetExecutors: new Map([
|
|
352
|
+
['test', 'nx:noop'],
|
|
353
|
+
['napi-test', BOUNDED_TEST_EXECUTOR],
|
|
354
|
+
['build', 'nx:run-commands'],
|
|
355
|
+
]),
|
|
356
|
+
targetOptions: new Map([
|
|
357
|
+
[
|
|
358
|
+
'napi-test',
|
|
359
|
+
{
|
|
360
|
+
command: `bun test ${TIMEOUT_FLAG} src/native.test.ts`,
|
|
361
|
+
cwd: 'packages/cowshed',
|
|
362
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
363
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
364
|
+
},
|
|
365
|
+
],
|
|
366
|
+
['build', { command: 'bun run build' }],
|
|
367
|
+
]),
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
|
|
271
371
|
async function writeJson(path: string, value: unknown): Promise<void> {
|
|
272
372
|
await mkdir(dirname(path), { recursive: true });
|
|
273
373
|
await writeFile(path, `${JSON.stringify(value, null, 2)}\n`);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { dirname, join } from 'node:path';
|
|
3
|
+
import type { PackageTargetPolicyOptions, ResolvedProjectTargets } from './package-target-policy.js';
|
|
3
4
|
|
|
4
5
|
export const BOUNDED_TEST_EXECUTOR = '@smoothbricks/nx-plugin:bounded-exec';
|
|
5
6
|
export const BOUNDED_TEST_TIMEOUT_MS = 120_000;
|
|
@@ -34,6 +35,7 @@ export interface ApplyBoundedTestTargetPolicyOptions {
|
|
|
34
35
|
projectName: string;
|
|
35
36
|
defaultCommand?: string;
|
|
36
37
|
projectJson?: BoundedTestPolicyProjectJson;
|
|
38
|
+
resolvedProject?: ResolvedProjectTargets;
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
export function applyBoundedTestTargetPolicy(
|
|
@@ -64,7 +66,7 @@ export function applyBoundedTestTargetPolicy(
|
|
|
64
66
|
packageJson.scripts.test = boundedTestScriptAlias(options.projectName);
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
export function applyWorkspaceBoundedTestTargetPolicy(root: string): boolean {
|
|
69
|
+
export function applyWorkspaceBoundedTestTargetPolicy(root: string, options: PackageTargetPolicyOptions = {}): boolean {
|
|
68
70
|
let changed = false;
|
|
69
71
|
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
70
72
|
const packageJson = readPackageJson(packageJsonPath);
|
|
@@ -77,6 +79,10 @@ export function applyWorkspaceBoundedTestTargetPolicy(root: string): boolean {
|
|
|
77
79
|
if (!projectName) {
|
|
78
80
|
continue;
|
|
79
81
|
}
|
|
82
|
+
const resolvedProject = resolvedProjectFor(options, projectName);
|
|
83
|
+
if (checkBoundedTestTargetPolicy(packageJson, { projectName, projectJson, resolvedProject })) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
80
86
|
const beforePackageJson = JSON.stringify(packageJson);
|
|
81
87
|
const beforeProjectJson = JSON.stringify(projectJson);
|
|
82
88
|
applyBoundedTestTargetPolicy(packageJson, { projectName, projectJson });
|
|
@@ -92,7 +98,10 @@ export function applyWorkspaceBoundedTestTargetPolicy(root: string): boolean {
|
|
|
92
98
|
return changed;
|
|
93
99
|
}
|
|
94
100
|
|
|
95
|
-
export function checkWorkspaceBoundedTestTargetPolicy(
|
|
101
|
+
export function checkWorkspaceBoundedTestTargetPolicy(
|
|
102
|
+
root: string,
|
|
103
|
+
options: PackageTargetPolicyOptions = {},
|
|
104
|
+
): BoundedTestPolicyIssue[] {
|
|
96
105
|
const issues: BoundedTestPolicyIssue[] = [];
|
|
97
106
|
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
98
107
|
const packageJson = readPackageJson(packageJsonPath);
|
|
@@ -109,10 +118,13 @@ export function checkWorkspaceBoundedTestTargetPolicy(root: string): BoundedTest
|
|
|
109
118
|
});
|
|
110
119
|
continue;
|
|
111
120
|
}
|
|
112
|
-
|
|
121
|
+
const resolvedProject = resolvedProjectFor(options, projectName);
|
|
122
|
+
if (!checkBoundedTestTargetPolicy(packageJson, { projectName, projectJson, resolvedProject })) {
|
|
113
123
|
issues.push({
|
|
114
124
|
path: projectJson ? projectJsonPath : packageJsonPath,
|
|
115
|
-
message:
|
|
125
|
+
message:
|
|
126
|
+
`${projectJson ? 'targets' : 'nx.targets'}.test must use ${BOUNDED_TEST_EXECUTOR} ` +
|
|
127
|
+
'or delegate through no-op targets to bounded test execution',
|
|
116
128
|
});
|
|
117
129
|
}
|
|
118
130
|
}
|
|
@@ -124,23 +136,104 @@ export function checkBoundedTestTargetPolicy(
|
|
|
124
136
|
options: ApplyBoundedTestTargetPolicyOptions,
|
|
125
137
|
): boolean {
|
|
126
138
|
const testTarget = options.projectJson ? options.projectJson.targets?.test : packageJson.nx?.targets?.test;
|
|
127
|
-
if (!isRecord(testTarget)
|
|
139
|
+
if (!isRecord(testTarget)) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
if (isBoundedExecutionTarget(testTarget.executor, testTarget.options, new Set(['{projectRoot}']))) {
|
|
143
|
+
return packageJson.scripts?.test === boundedTestScriptAlias(options.projectName);
|
|
144
|
+
}
|
|
145
|
+
if (!isNoopAggregateTarget(testTarget)) {
|
|
128
146
|
return false;
|
|
129
147
|
}
|
|
130
|
-
const
|
|
131
|
-
if (
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
targetOptions.killAfterMs !== BOUNDED_TEST_KILL_AFTER_MS
|
|
140
|
-
) {
|
|
148
|
+
const testScript = packageJson.scripts?.test;
|
|
149
|
+
if (testScript !== undefined && testScript !== boundedTestScriptAlias(options.projectName)) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
return resolvedAggregateTestIsBounded(options.resolvedProject);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function isNoopAggregateTarget(target: Record<string, unknown>): boolean {
|
|
156
|
+
if (target.executor !== undefined && target.executor !== 'nx:noop') {
|
|
141
157
|
return false;
|
|
142
158
|
}
|
|
143
|
-
|
|
159
|
+
if (typeof target.command === 'string' || (isRecord(target.options) && typeof target.options.command === 'string')) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
return Array.isArray(target.dependsOn) && target.dependsOn.length > 0;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function isBoundedExecutionTarget(executor: unknown, rawOptions: unknown, allowedCwds: ReadonlySet<string>): boolean {
|
|
166
|
+
if (executor !== BOUNDED_TEST_EXECUTOR || !isRecord(rawOptions)) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
const command = rawOptions.command;
|
|
170
|
+
return (
|
|
171
|
+
typeof command === 'string' &&
|
|
172
|
+
command.length > 0 &&
|
|
173
|
+
isPackageTestScriptRunnerCommand(command) === false &&
|
|
174
|
+
command === ensureBunTestTimeoutFlag(command) &&
|
|
175
|
+
typeof rawOptions.cwd === 'string' &&
|
|
176
|
+
allowedCwds.has(rawOptions.cwd) &&
|
|
177
|
+
rawOptions.timeoutMs === BOUNDED_TEST_TIMEOUT_MS &&
|
|
178
|
+
rawOptions.killAfterMs === BOUNDED_TEST_KILL_AFTER_MS
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function resolvedAggregateTestIsBounded(project: ResolvedProjectTargets | undefined): boolean {
|
|
183
|
+
if (!project?.targetDependencies || !project.targetExecutors || !project.targetOptions) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
const visiting = new Set<string>();
|
|
187
|
+
const verified = new Map<string, boolean>();
|
|
188
|
+
const allowedCwds = new Set(['{projectRoot}', ...(project.root ? [project.root] : [])]);
|
|
189
|
+
|
|
190
|
+
const visit = (targetName: string): boolean => {
|
|
191
|
+
const cached = verified.get(targetName);
|
|
192
|
+
if (cached !== undefined) {
|
|
193
|
+
return cached;
|
|
194
|
+
}
|
|
195
|
+
if (visiting.has(targetName) || !project.targets.has(targetName)) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
visiting.add(targetName);
|
|
199
|
+
const executor = project.targetExecutors?.get(targetName);
|
|
200
|
+
let valid: boolean;
|
|
201
|
+
if (executor !== undefined && executor !== 'nx:noop') {
|
|
202
|
+
valid = isBoundedExecutionTarget(executor, project.targetOptions?.get(targetName), allowedCwds);
|
|
203
|
+
} else {
|
|
204
|
+
const dependencies = project.targetDependencies?.get(targetName) ?? [];
|
|
205
|
+
valid =
|
|
206
|
+
dependencies.length > 0 &&
|
|
207
|
+
dependencies.every((dependency) => {
|
|
208
|
+
const matches = matchingResolvedTargets(dependency, project.targets);
|
|
209
|
+
return matches.length > 0 && matches.every(visit);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
visiting.delete(targetName);
|
|
213
|
+
verified.set(targetName, valid);
|
|
214
|
+
return valid;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
return visit('test');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function matchingResolvedTargets(dependency: string, targets: ReadonlySet<string>): string[] {
|
|
221
|
+
if (dependency.startsWith('^')) {
|
|
222
|
+
return [];
|
|
223
|
+
}
|
|
224
|
+
if (!dependency.startsWith('*')) {
|
|
225
|
+
return targets.has(dependency) ? [dependency] : [];
|
|
226
|
+
}
|
|
227
|
+
const suffix = dependency.slice(1);
|
|
228
|
+
return [...targets].filter((targetName) => targetName.endsWith(suffix));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function resolvedProjectFor(
|
|
232
|
+
options: PackageTargetPolicyOptions,
|
|
233
|
+
projectName: string,
|
|
234
|
+
): ResolvedProjectTargets | undefined {
|
|
235
|
+
const project = options.resolvedTargetsByProject?.get(projectName);
|
|
236
|
+
return project && 'targets' in project ? project : undefined;
|
|
144
237
|
}
|
|
145
238
|
|
|
146
239
|
export function boundedTestScriptAlias(projectName: string): string {
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
|
|
7
|
+
import { type CompilerInvocation, runTypeScriptEmit } from './executor.js';
|
|
8
|
+
|
|
9
|
+
describe('@smoothbricks/nx-plugin TypeScript emit executor', () => {
|
|
10
|
+
it('runs transformed JavaScript and native declaration lanes with project references preserved', async () => {
|
|
11
|
+
const root = await mkdtemp(join(tmpdir(), 'smoo-typescript-emit-'));
|
|
12
|
+
const projectRoot = join(root, 'packages/example');
|
|
13
|
+
await mkdir(projectRoot, { recursive: true });
|
|
14
|
+
await writeFile(
|
|
15
|
+
join(projectRoot, 'tsconfig.lib.json'),
|
|
16
|
+
'{\n // Project references are intentionally not inherited through extends.\n "references": [{ "path": "../dependency/tsconfig.lib.json" }]\n}\n',
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
const invocations: CompilerInvocation[] = [];
|
|
20
|
+
let overlayContents = '';
|
|
21
|
+
let overlayPath = '';
|
|
22
|
+
try {
|
|
23
|
+
const result = await runTypeScriptEmit(
|
|
24
|
+
{ cwd: 'packages/example', tsConfig: 'tsconfig.lib.json' },
|
|
25
|
+
{ root },
|
|
26
|
+
async (invocation) => {
|
|
27
|
+
invocations.push(invocation);
|
|
28
|
+
if (invocation.command === 'ttsc') {
|
|
29
|
+
overlayPath = invocation.args[1] ?? '';
|
|
30
|
+
overlayContents = await readFile(overlayPath, 'utf8');
|
|
31
|
+
}
|
|
32
|
+
return true;
|
|
33
|
+
},
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
expect(result).toEqual({ success: true });
|
|
37
|
+
expect(invocations).toHaveLength(2);
|
|
38
|
+
expect(invocations[0]).toEqual({
|
|
39
|
+
command: 'ttsc',
|
|
40
|
+
args: ['-p', overlayPath, '--emit'],
|
|
41
|
+
cwd: projectRoot,
|
|
42
|
+
});
|
|
43
|
+
expect(overlayPath).toMatch(/\/\.ttsc-js-\d+-[0-9a-f-]+\.json$/);
|
|
44
|
+
expect(overlayContents).toBe(
|
|
45
|
+
`${JSON.stringify(
|
|
46
|
+
{
|
|
47
|
+
extends: './tsconfig.lib.json',
|
|
48
|
+
compilerOptions: {
|
|
49
|
+
composite: false,
|
|
50
|
+
declaration: false,
|
|
51
|
+
declarationMap: false,
|
|
52
|
+
emitDeclarationOnly: false,
|
|
53
|
+
incremental: false,
|
|
54
|
+
},
|
|
55
|
+
references: [{ path: '../dependency/tsconfig.lib.json' }],
|
|
56
|
+
},
|
|
57
|
+
null,
|
|
58
|
+
2,
|
|
59
|
+
)}\n`,
|
|
60
|
+
);
|
|
61
|
+
expect(invocations[1]).toEqual({
|
|
62
|
+
command: 'tsc',
|
|
63
|
+
args: ['-p', overlayPath, '--emitDeclarationOnly', '--declaration', '--declarationMap'],
|
|
64
|
+
cwd: projectRoot,
|
|
65
|
+
});
|
|
66
|
+
expect(existsSync(overlayPath)).toBe(false);
|
|
67
|
+
} finally {
|
|
68
|
+
await rm(root, { recursive: true, force: true });
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('does not emit declarations when transformed JavaScript fails and still removes its overlay', async () => {
|
|
73
|
+
const root = await mkdtemp(join(tmpdir(), 'smoo-typescript-emit-'));
|
|
74
|
+
const projectRoot = join(root, 'packages/example');
|
|
75
|
+
await mkdir(projectRoot, { recursive: true });
|
|
76
|
+
await writeFile(join(projectRoot, 'tsconfig.lib.json'), '{}\n');
|
|
77
|
+
|
|
78
|
+
const invocations: CompilerInvocation[] = [];
|
|
79
|
+
let overlayPath = '';
|
|
80
|
+
try {
|
|
81
|
+
const result = await runTypeScriptEmit(
|
|
82
|
+
{ cwd: projectRoot, tsConfig: 'tsconfig.lib.json' },
|
|
83
|
+
{ root },
|
|
84
|
+
async (invocation) => {
|
|
85
|
+
invocations.push(invocation);
|
|
86
|
+
overlayPath = invocation.args[1] ?? '';
|
|
87
|
+
return false;
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
expect(result).toEqual({ success: false });
|
|
92
|
+
expect(invocations.map(({ command }) => command)).toEqual(['ttsc']);
|
|
93
|
+
expect(existsSync(overlayPath)).toBe(false);
|
|
94
|
+
} finally {
|
|
95
|
+
await rm(root, { recursive: true, force: true });
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { once } from 'node:events';
|
|
4
|
+
import { rm, writeFile } from 'node:fs/promises';
|
|
5
|
+
import { basename, dirname, isAbsolute, join } from 'node:path';
|
|
6
|
+
|
|
7
|
+
import { readJsonFile } from 'nx/src/devkit-exports.js';
|
|
8
|
+
|
|
9
|
+
import type { TypeScriptEmitOptions } from './schema.js';
|
|
10
|
+
|
|
11
|
+
interface TypeScriptEmitContext {
|
|
12
|
+
root: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface TypeScriptEmitResult {
|
|
16
|
+
success: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface TsConfigJson {
|
|
20
|
+
references?: unknown;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CompilerInvocation {
|
|
24
|
+
command: 'tsc' | 'ttsc';
|
|
25
|
+
args: string[];
|
|
26
|
+
cwd: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type CompilerRunner = (invocation: CompilerInvocation) => Promise<boolean>;
|
|
30
|
+
|
|
31
|
+
export default function typescriptEmitExecutor(
|
|
32
|
+
options: TypeScriptEmitOptions,
|
|
33
|
+
context: TypeScriptEmitContext,
|
|
34
|
+
): Promise<TypeScriptEmitResult> {
|
|
35
|
+
return runTypeScriptEmit(options, context, runCompiler);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function runTypeScriptEmit(
|
|
39
|
+
options: TypeScriptEmitOptions,
|
|
40
|
+
context: TypeScriptEmitContext,
|
|
41
|
+
runner: CompilerRunner,
|
|
42
|
+
): Promise<TypeScriptEmitResult> {
|
|
43
|
+
const cwd = isAbsolute(options.cwd) ? options.cwd : join(context.root, options.cwd);
|
|
44
|
+
const tsConfigPath = isAbsolute(options.tsConfig) ? options.tsConfig : join(cwd, options.tsConfig);
|
|
45
|
+
const tsConfig = readJsonFile<TsConfigJson>(tsConfigPath);
|
|
46
|
+
const overlayPath = join(dirname(tsConfigPath), `.ttsc-js-${process.pid}-${randomUUID()}.json`);
|
|
47
|
+
const overlay = {
|
|
48
|
+
extends: `./${basename(tsConfigPath)}`,
|
|
49
|
+
compilerOptions: {
|
|
50
|
+
composite: false,
|
|
51
|
+
declaration: false,
|
|
52
|
+
declarationMap: false,
|
|
53
|
+
emitDeclarationOnly: false,
|
|
54
|
+
incremental: false,
|
|
55
|
+
},
|
|
56
|
+
...(Array.isArray(tsConfig.references) ? { references: tsConfig.references } : {}),
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
await writeFile(overlayPath, `${JSON.stringify(overlay, null, 2)}\n`, { flag: 'wx' });
|
|
60
|
+
try {
|
|
61
|
+
const javascriptSucceeded = await runner({
|
|
62
|
+
command: 'ttsc',
|
|
63
|
+
args: ['-p', overlayPath, '--emit'],
|
|
64
|
+
cwd,
|
|
65
|
+
});
|
|
66
|
+
if (!javascriptSucceeded) {
|
|
67
|
+
return { success: false };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const declarationsSucceeded = await runner({
|
|
71
|
+
command: 'tsc',
|
|
72
|
+
args: ['-p', overlayPath, '--emitDeclarationOnly', '--declaration', '--declarationMap'],
|
|
73
|
+
cwd,
|
|
74
|
+
});
|
|
75
|
+
return { success: declarationsSucceeded };
|
|
76
|
+
} finally {
|
|
77
|
+
await rm(overlayPath, { force: true });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function runCompiler(invocation: CompilerInvocation): Promise<boolean> {
|
|
82
|
+
const child = spawn(invocation.command, invocation.args, {
|
|
83
|
+
cwd: invocation.cwd,
|
|
84
|
+
stdio: 'inherit',
|
|
85
|
+
windowsHide: true,
|
|
86
|
+
});
|
|
87
|
+
try {
|
|
88
|
+
await once(child, 'exit');
|
|
89
|
+
return child.exitCode === 0 && child.signalCode === null;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
console.error(`${invocation.command}: ${error instanceof Error ? error.message : String(error)}`);
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/schema",
|
|
3
|
+
"version": 2,
|
|
4
|
+
"title": "TypeScript emit executor",
|
|
5
|
+
"description": "Emit transformer-generated JavaScript with ttsc and declarations with native tsc",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"cwd": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "Working directory, relative to the workspace root"
|
|
11
|
+
},
|
|
12
|
+
"tsConfig": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "TypeScript project config, relative to cwd"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"required": ["cwd", "tsConfig"],
|
|
18
|
+
"additionalProperties": false
|
|
19
|
+
}
|