@smoothbricks/nx-plugin 0.0.0-bootstrap.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -2
- package/dist/bounded-test-policy.d.ts +32 -0
- package/dist/bounded-test-policy.d.ts.map +1 -0
- package/dist/bounded-test-policy.js +184 -0
- package/dist/executors/bounded-exec/executor.d.ts +15 -0
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -0
- package/dist/executors/bounded-exec/executor.js +179 -0
- package/dist/executors/bounded-exec/schema.d.ts +11 -0
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -0
- package/dist/executors/bounded-exec/schema.js +1 -0
- package/dist/generators/bounded-test-targets/generator.d.ts +7 -0
- package/dist/generators/bounded-test-targets/generator.d.ts.map +1 -0
- package/dist/generators/bounded-test-targets/generator.js +40 -0
- package/dist/generators/bun-test-tracing/generator.d.ts +10 -0
- package/dist/generators/bun-test-tracing/generator.d.ts.map +1 -0
- package/dist/generators/bun-test-tracing/generator.js +247 -0
- package/dist/generators/create-package/generator.d.ts +4 -0
- package/dist/generators/create-package/generator.d.ts.map +1 -0
- package/dist/generators/create-package/generator.js +204 -0
- package/dist/generators/make-public/generator.d.ts +4 -0
- package/dist/generators/make-public/generator.d.ts.map +1 -0
- package/dist/generators/make-public/generator.js +55 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +197 -0
- package/dist/package-target-policy.d.ts +31 -0
- package/dist/package-target-policy.d.ts.map +1 -0
- package/dist/package-target-policy.js +930 -0
- package/dist/release-config-policy.d.ts +32 -0
- package/dist/release-config-policy.d.ts.map +1 -0
- package/dist/release-config-policy.js +225 -0
- package/dist/typecheck-test-policy.d.ts +56 -0
- package/dist/typecheck-test-policy.d.ts.map +1 -0
- package/dist/typecheck-test-policy.js +640 -0
- package/dist/version-actions.cjs +78 -0
- package/dist/version-actions.d.cts +6 -0
- package/dist/version-actions.d.cts.map +1 -0
- package/dist/workspace-config-policy.d.ts +33 -0
- package/dist/workspace-config-policy.d.ts.map +1 -0
- package/dist/workspace-config-policy.js +334 -0
- package/executors.json +9 -0
- package/generators.json +24 -0
- package/loader.js +16 -0
- package/package.json +89 -4
- package/src/bounded-test-policy.test.ts +257 -0
- package/src/bounded-test-policy.ts +257 -0
- package/src/bun-bootstrap.test.ts +52 -0
- package/src/executors/bounded-exec/executor.test.ts +258 -0
- package/src/executors/bounded-exec/executor.ts +228 -0
- package/src/executors/bounded-exec/schema.json +68 -0
- package/src/executors/bounded-exec/schema.ts +10 -0
- package/src/generators/bounded-test-targets/generator.test.ts +136 -0
- package/src/generators/bounded-test-targets/generator.ts +67 -0
- package/src/generators/bounded-test-targets/schema.d.ts +3 -0
- package/src/generators/bounded-test-targets/schema.json +14 -0
- package/src/generators/bun-test-tracing/generator.test.ts +243 -0
- package/src/generators/bun-test-tracing/generator.ts +348 -0
- package/src/generators/bun-test-tracing/schema.d.ts +8 -0
- package/src/generators/bun-test-tracing/schema.json +28 -0
- package/src/generators/create-package/generator.test.ts +206 -0
- package/src/generators/create-package/generator.ts +251 -0
- package/src/generators/create-package/schema.d.ts +5 -0
- package/src/generators/create-package/schema.json +24 -0
- package/src/generators/make-public/generator.test.ts +137 -0
- package/src/generators/make-public/generator.ts +83 -0
- package/src/generators/make-public/schema.d.ts +3 -0
- package/src/generators/make-public/schema.json +13 -0
- package/src/index.test.ts +200 -0
- package/src/index.ts +242 -0
- package/src/package-target-policy.test.ts +778 -0
- package/src/package-target-policy.ts +1127 -0
- package/src/release-config-policy.test.ts +280 -0
- package/src/release-config-policy.ts +258 -0
- package/src/typecheck-test-policy.test.ts +797 -0
- package/src/typecheck-test-policy.ts +752 -0
- package/src/version-actions.cts +94 -0
- package/src/workspace-config-policy.test.ts +376 -0
- package/src/workspace-config-policy.ts +390 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
applyBoundedTestTargetPolicy,
|
|
8
|
+
applyWorkspaceBoundedTestTargetPolicy,
|
|
9
|
+
BOUNDED_TEST_EXECUTOR,
|
|
10
|
+
BOUNDED_TEST_KILL_AFTER_MS,
|
|
11
|
+
BOUNDED_TEST_TIMEOUT_MS,
|
|
12
|
+
type BoundedTestPolicyPackageJson,
|
|
13
|
+
type BoundedTestPolicyProjectJson,
|
|
14
|
+
boundedTestScriptAlias,
|
|
15
|
+
checkBoundedTestTargetPolicy,
|
|
16
|
+
checkWorkspaceBoundedTestTargetPolicy,
|
|
17
|
+
resolveTestCommand,
|
|
18
|
+
} from './bounded-test-policy.js';
|
|
19
|
+
|
|
20
|
+
describe('bounded test target policy', () => {
|
|
21
|
+
it('preserves an existing nx:run-commands test command', () => {
|
|
22
|
+
const packageJson: BoundedTestPolicyPackageJson = {
|
|
23
|
+
scripts: { test: 'bun test --old' },
|
|
24
|
+
nx: {
|
|
25
|
+
targets: {
|
|
26
|
+
test: {
|
|
27
|
+
executor: 'nx:run-commands',
|
|
28
|
+
dependsOn: ['typecheck-tests'],
|
|
29
|
+
options: { command: 'bun test --coverage', cwd: 'packages/example' },
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
applyBoundedTestTargetPolicy(packageJson, { projectName: 'example' });
|
|
36
|
+
|
|
37
|
+
expect(packageJson.nx?.targets?.test).toEqual({
|
|
38
|
+
executor: BOUNDED_TEST_EXECUTOR,
|
|
39
|
+
dependsOn: ['typecheck-tests'],
|
|
40
|
+
options: {
|
|
41
|
+
command: 'bun test --coverage',
|
|
42
|
+
cwd: '{projectRoot}',
|
|
43
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
44
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
expect(packageJson.scripts?.test).toBe('nx run example:test --tui=false --outputStyle=stream');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('uses a direct test script when no target command exists', () => {
|
|
51
|
+
const packageJson: BoundedTestPolicyPackageJson = {
|
|
52
|
+
scripts: { test: 'bun test --pass-with-no-tests' },
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
applyBoundedTestTargetPolicy(packageJson, { projectName: '@scope/example' });
|
|
56
|
+
|
|
57
|
+
expect(packageJson.nx?.targets?.test).toEqual({
|
|
58
|
+
executor: BOUNDED_TEST_EXECUTOR,
|
|
59
|
+
options: {
|
|
60
|
+
command: 'bun test --pass-with-no-tests',
|
|
61
|
+
cwd: '{projectRoot}',
|
|
62
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
63
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
expect(packageJson.scripts?.test).toBe('nx run @scope/example:test --tui=false --outputStyle=stream');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('normalizes project.json targets while reading package test scripts', () => {
|
|
70
|
+
const packageJson: BoundedTestPolicyPackageJson = {
|
|
71
|
+
scripts: { test: 'bun test --script' },
|
|
72
|
+
nx: {
|
|
73
|
+
targets: {
|
|
74
|
+
test: {
|
|
75
|
+
executor: 'nx:run-commands',
|
|
76
|
+
options: { command: 'bun test --package-target' },
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
const projectJson: BoundedTestPolicyProjectJson = {
|
|
82
|
+
targets: {
|
|
83
|
+
test: {
|
|
84
|
+
executor: 'nx:run-commands',
|
|
85
|
+
dependsOn: ['typecheck-tests'],
|
|
86
|
+
options: { command: 'bun test --project-target', cwd: 'packages/example' },
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
applyBoundedTestTargetPolicy(packageJson, { projectName: 'example', projectJson });
|
|
92
|
+
|
|
93
|
+
expect(projectJson.targets?.test).toEqual({
|
|
94
|
+
executor: BOUNDED_TEST_EXECUTOR,
|
|
95
|
+
dependsOn: ['typecheck-tests'],
|
|
96
|
+
options: {
|
|
97
|
+
command: 'bun test --project-target',
|
|
98
|
+
cwd: '{projectRoot}',
|
|
99
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
100
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
expect(packageJson.nx?.targets?.test).toEqual({
|
|
104
|
+
executor: 'nx:run-commands',
|
|
105
|
+
options: { command: 'bun test --package-target' },
|
|
106
|
+
});
|
|
107
|
+
expect(packageJson.scripts?.test).toBe('nx run example:test --tui=false --outputStyle=stream');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('creates project.json test targets from package test scripts', () => {
|
|
111
|
+
const packageJson: BoundedTestPolicyPackageJson = {
|
|
112
|
+
scripts: { test: 'bun test --script' },
|
|
113
|
+
};
|
|
114
|
+
const projectJson: BoundedTestPolicyProjectJson = {
|
|
115
|
+
targets: {},
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
applyBoundedTestTargetPolicy(packageJson, { projectName: 'example', projectJson });
|
|
119
|
+
|
|
120
|
+
expect(projectJson.targets?.test).toEqual({
|
|
121
|
+
executor: BOUNDED_TEST_EXECUTOR,
|
|
122
|
+
options: {
|
|
123
|
+
command: 'bun test --script',
|
|
124
|
+
cwd: '{projectRoot}',
|
|
125
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
126
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
expect(packageJson.nx).toBeUndefined();
|
|
130
|
+
expect(packageJson.scripts?.test).toBe('nx run example:test --tui=false --outputStyle=stream');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('requires bounded targets in project.json when project config exists', () => {
|
|
134
|
+
const packageJson: BoundedTestPolicyPackageJson = {
|
|
135
|
+
scripts: { test: boundedTestScriptAlias('example') },
|
|
136
|
+
nx: {
|
|
137
|
+
targets: {
|
|
138
|
+
test: {
|
|
139
|
+
executor: BOUNDED_TEST_EXECUTOR,
|
|
140
|
+
options: {
|
|
141
|
+
command: 'bun test --package-target',
|
|
142
|
+
cwd: '{projectRoot}',
|
|
143
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
144
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
const projectJson: BoundedTestPolicyProjectJson = { targets: {} };
|
|
151
|
+
|
|
152
|
+
expect(checkBoundedTestTargetPolicy(packageJson, { projectName: 'example', projectJson })).toBe(false);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('does not preserve recursive nx run aliases as commands', () => {
|
|
156
|
+
const packageJson: BoundedTestPolicyPackageJson = {
|
|
157
|
+
scripts: { test: boundedTestScriptAlias('example') },
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
expect(resolveTestCommand(packageJson)).toBe('bun test');
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('checks and fixes workspace package test targets', async () => {
|
|
164
|
+
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-bounded-policy-'));
|
|
165
|
+
try {
|
|
166
|
+
await writeJson(join(root, 'package.json'), {
|
|
167
|
+
name: '@scope/root',
|
|
168
|
+
private: true,
|
|
169
|
+
workspaces: ['packages/*'],
|
|
170
|
+
});
|
|
171
|
+
await writeJson(join(root, 'packages/app/package.json'), {
|
|
172
|
+
name: '@scope/app',
|
|
173
|
+
scripts: { test: 'bun test --pass-with-no-tests' },
|
|
174
|
+
nx: { name: 'app' },
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
expect(checkWorkspaceBoundedTestTargetPolicy(root)).toEqual([
|
|
178
|
+
{
|
|
179
|
+
path: join(root, 'packages/app/package.json'),
|
|
180
|
+
message: `nx.targets.test must use ${BOUNDED_TEST_EXECUTOR} with bounded test policy`,
|
|
181
|
+
},
|
|
182
|
+
]);
|
|
183
|
+
|
|
184
|
+
expect(applyWorkspaceBoundedTestTargetPolicy(root)).toBe(true);
|
|
185
|
+
|
|
186
|
+
const app = JSON.parse(await readFile(join(root, 'packages/app/package.json'), 'utf8'));
|
|
187
|
+
expect(app.scripts.test).toBe('nx run app:test --tui=false --outputStyle=stream');
|
|
188
|
+
expect(app.nx.targets.test).toEqual({
|
|
189
|
+
executor: BOUNDED_TEST_EXECUTOR,
|
|
190
|
+
options: {
|
|
191
|
+
command: 'bun test --pass-with-no-tests',
|
|
192
|
+
cwd: '{projectRoot}',
|
|
193
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
194
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
expect(checkWorkspaceBoundedTestTargetPolicy(root)).toEqual([]);
|
|
198
|
+
} finally {
|
|
199
|
+
await rm(root, { recursive: true, force: true });
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('checks and fixes workspace project.json test targets', async () => {
|
|
204
|
+
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-bounded-policy-'));
|
|
205
|
+
try {
|
|
206
|
+
await writeJson(join(root, 'package.json'), {
|
|
207
|
+
name: '@scope/root',
|
|
208
|
+
private: true,
|
|
209
|
+
workspaces: ['packages/*'],
|
|
210
|
+
});
|
|
211
|
+
await writeJson(join(root, 'packages/app/package.json'), {
|
|
212
|
+
name: '@scope/app',
|
|
213
|
+
scripts: { test: 'bun test --pass-with-no-tests' },
|
|
214
|
+
});
|
|
215
|
+
await writeJson(join(root, 'packages/app/project.json'), {
|
|
216
|
+
name: 'app',
|
|
217
|
+
targets: {
|
|
218
|
+
test: {
|
|
219
|
+
executor: 'nx:run-commands',
|
|
220
|
+
options: { command: 'bun test --project', cwd: 'packages/app' },
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
expect(checkWorkspaceBoundedTestTargetPolicy(root)).toEqual([
|
|
226
|
+
{
|
|
227
|
+
path: join(root, 'packages/app/project.json'),
|
|
228
|
+
message: `targets.test must use ${BOUNDED_TEST_EXECUTOR} with bounded test policy`,
|
|
229
|
+
},
|
|
230
|
+
]);
|
|
231
|
+
|
|
232
|
+
expect(applyWorkspaceBoundedTestTargetPolicy(root)).toBe(true);
|
|
233
|
+
|
|
234
|
+
const appPackage = JSON.parse(await readFile(join(root, 'packages/app/package.json'), 'utf8'));
|
|
235
|
+
const appProject = JSON.parse(await readFile(join(root, 'packages/app/project.json'), 'utf8'));
|
|
236
|
+
expect(appPackage.scripts.test).toBe('nx run app:test --tui=false --outputStyle=stream');
|
|
237
|
+
expect(appPackage.nx).toBeUndefined();
|
|
238
|
+
expect(appProject.targets.test).toEqual({
|
|
239
|
+
executor: BOUNDED_TEST_EXECUTOR,
|
|
240
|
+
options: {
|
|
241
|
+
command: 'bun test --project',
|
|
242
|
+
cwd: '{projectRoot}',
|
|
243
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
244
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
expect(checkWorkspaceBoundedTestTargetPolicy(root)).toEqual([]);
|
|
248
|
+
} finally {
|
|
249
|
+
await rm(root, { recursive: true, force: true });
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
async function writeJson(path: string, value: unknown): Promise<void> {
|
|
255
|
+
await mkdir(dirname(path), { recursive: true });
|
|
256
|
+
await writeFile(path, `${JSON.stringify(value, null, 2)}\n`);
|
|
257
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
export const BOUNDED_TEST_EXECUTOR = '@smoothbricks/nx-plugin:bounded-exec';
|
|
5
|
+
export const BOUNDED_TEST_TIMEOUT_MS = 120_000;
|
|
6
|
+
export const BOUNDED_TEST_KILL_AFTER_MS = 10_000;
|
|
7
|
+
|
|
8
|
+
export interface BoundedTestPolicyPackageJson {
|
|
9
|
+
name?: string;
|
|
10
|
+
workspaces?: unknown;
|
|
11
|
+
scripts?: Record<string, unknown>;
|
|
12
|
+
nx?: {
|
|
13
|
+
name?: string;
|
|
14
|
+
targets?: Record<string, Record<string, unknown>>;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface BoundedTestPolicyProjectJson {
|
|
19
|
+
name?: string;
|
|
20
|
+
targets?: Record<string, Record<string, unknown>>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface BoundedTestPolicyIssue {
|
|
24
|
+
path: string;
|
|
25
|
+
message: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ApplyBoundedTestTargetPolicyOptions {
|
|
29
|
+
projectName: string;
|
|
30
|
+
defaultCommand?: string;
|
|
31
|
+
projectJson?: BoundedTestPolicyProjectJson;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function applyBoundedTestTargetPolicy(
|
|
35
|
+
packageJson: BoundedTestPolicyPackageJson,
|
|
36
|
+
options: ApplyBoundedTestTargetPolicyOptions,
|
|
37
|
+
): void {
|
|
38
|
+
const command = resolveTestCommand(packageJson, options.defaultCommand ?? 'bun test', options.projectJson);
|
|
39
|
+
const targetOwner = options.projectJson ?? (packageJson.nx ??= {});
|
|
40
|
+
|
|
41
|
+
targetOwner.targets ??= {};
|
|
42
|
+
|
|
43
|
+
const existingTestTarget = targetOwner.targets.test;
|
|
44
|
+
const nextTestTarget: Record<string, unknown> = isRecord(existingTestTarget) ? { ...existingTestTarget } : {};
|
|
45
|
+
nextTestTarget.executor = BOUNDED_TEST_EXECUTOR;
|
|
46
|
+
nextTestTarget.options = {
|
|
47
|
+
command,
|
|
48
|
+
cwd: '{projectRoot}',
|
|
49
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
50
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
targetOwner.targets.test = nextTestTarget;
|
|
54
|
+
packageJson.scripts ??= {};
|
|
55
|
+
packageJson.scripts.test = boundedTestScriptAlias(options.projectName);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function applyWorkspaceBoundedTestTargetPolicy(root: string): boolean {
|
|
59
|
+
let changed = false;
|
|
60
|
+
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
61
|
+
const packageJson = readPackageJson(packageJsonPath);
|
|
62
|
+
const projectJsonPath = projectJsonPathForPackageJson(packageJsonPath);
|
|
63
|
+
const projectJson = existsSync(projectJsonPath) ? readProjectJson(projectJsonPath) : undefined;
|
|
64
|
+
if (!hasTestEntrypoint(packageJson, projectJson)) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const projectName = packageProjectName(packageJson, projectJson);
|
|
68
|
+
if (!projectName) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const beforePackageJson = JSON.stringify(packageJson);
|
|
72
|
+
const beforeProjectJson = JSON.stringify(projectJson);
|
|
73
|
+
applyBoundedTestTargetPolicy(packageJson, { projectName, projectJson });
|
|
74
|
+
if (JSON.stringify(packageJson) === beforePackageJson && JSON.stringify(projectJson) === beforeProjectJson) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
writePackageJson(packageJsonPath, packageJson);
|
|
78
|
+
if (projectJson) {
|
|
79
|
+
writeProjectJson(projectJsonPath, projectJson);
|
|
80
|
+
}
|
|
81
|
+
changed = true;
|
|
82
|
+
}
|
|
83
|
+
return changed;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function checkWorkspaceBoundedTestTargetPolicy(root: string): BoundedTestPolicyIssue[] {
|
|
87
|
+
const issues: BoundedTestPolicyIssue[] = [];
|
|
88
|
+
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
89
|
+
const packageJson = readPackageJson(packageJsonPath);
|
|
90
|
+
const projectJsonPath = projectJsonPathForPackageJson(packageJsonPath);
|
|
91
|
+
const projectJson = existsSync(projectJsonPath) ? readProjectJson(projectJsonPath) : undefined;
|
|
92
|
+
if (!hasTestEntrypoint(packageJson, projectJson)) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const projectName = packageProjectName(packageJson, projectJson);
|
|
96
|
+
if (!projectName) {
|
|
97
|
+
issues.push({
|
|
98
|
+
path: packageJsonPath,
|
|
99
|
+
message: 'test entrypoint requires package.json name, nx.name, or project.json name',
|
|
100
|
+
});
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (!checkBoundedTestTargetPolicy(packageJson, { projectName, projectJson })) {
|
|
104
|
+
issues.push({
|
|
105
|
+
path: projectJson ? projectJsonPath : packageJsonPath,
|
|
106
|
+
message: `${projectJson ? 'targets' : 'nx.targets'}.test must use ${BOUNDED_TEST_EXECUTOR} with bounded test policy`,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return issues;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function checkBoundedTestTargetPolicy(
|
|
114
|
+
packageJson: BoundedTestPolicyPackageJson,
|
|
115
|
+
options: ApplyBoundedTestTargetPolicyOptions,
|
|
116
|
+
): boolean {
|
|
117
|
+
const testTarget = options.projectJson ? options.projectJson.targets?.test : packageJson.nx?.targets?.test;
|
|
118
|
+
if (!isRecord(testTarget) || testTarget.executor !== BOUNDED_TEST_EXECUTOR) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
const targetOptions = testTarget.options;
|
|
122
|
+
if (
|
|
123
|
+
!isRecord(targetOptions) ||
|
|
124
|
+
typeof targetOptions.command !== 'string' ||
|
|
125
|
+
targetOptions.command.length === 0 ||
|
|
126
|
+
isPackageTestScriptRunnerCommand(targetOptions.command) ||
|
|
127
|
+
targetOptions.cwd !== '{projectRoot}' ||
|
|
128
|
+
targetOptions.timeoutMs !== BOUNDED_TEST_TIMEOUT_MS ||
|
|
129
|
+
targetOptions.killAfterMs !== BOUNDED_TEST_KILL_AFTER_MS
|
|
130
|
+
) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
return packageJson.scripts?.test === boundedTestScriptAlias(options.projectName);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function boundedTestScriptAlias(projectName: string): string {
|
|
137
|
+
return `nx run ${projectName}:test --tui=false --outputStyle=stream`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function resolveTestCommand(
|
|
141
|
+
packageJson: BoundedTestPolicyPackageJson,
|
|
142
|
+
defaultCommand = 'bun test',
|
|
143
|
+
projectJson?: BoundedTestPolicyProjectJson,
|
|
144
|
+
): string {
|
|
145
|
+
const commandFromProjectTarget = resolveTargetCommand(projectJson?.targets?.test);
|
|
146
|
+
if (commandFromProjectTarget) {
|
|
147
|
+
return commandFromProjectTarget;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const commandFromPackageTarget = resolveTargetCommand(packageJson.nx?.targets?.test);
|
|
151
|
+
if (commandFromPackageTarget) {
|
|
152
|
+
return commandFromPackageTarget;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const scriptCommand = packageJson.scripts?.test;
|
|
156
|
+
if (typeof scriptCommand === 'string' && !isNxRunTestAlias(scriptCommand)) {
|
|
157
|
+
return scriptCommand;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return defaultCommand;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function resolveTargetCommand(existingTarget: unknown): string | null {
|
|
164
|
+
if (isRecord(existingTarget)) {
|
|
165
|
+
const targetOptions = existingTarget.options;
|
|
166
|
+
if (
|
|
167
|
+
isRecord(targetOptions) &&
|
|
168
|
+
typeof targetOptions.command === 'string' &&
|
|
169
|
+
targetOptions.command.length > 0 &&
|
|
170
|
+
!isPackageTestScriptRunnerCommand(targetOptions.command)
|
|
171
|
+
) {
|
|
172
|
+
return targetOptions.command;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function isPackageTestScriptRunnerCommand(command: string): boolean {
|
|
179
|
+
return /^(?:bun\s+run|npm(?:\s+run)?|pnpm(?:\s+run)?|yarn(?:\s+run)?)\s+test(?:\s|$)/.test(command.trim());
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function isNxRunTestAlias(command: string): boolean {
|
|
183
|
+
return /^nx\s+run\s+[^\s:]+:test(?:\s|$)/.test(command.trim());
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function listWorkspacePackageJsonPaths(root: string): string[] {
|
|
187
|
+
const rootPackagePath = join(root, 'package.json');
|
|
188
|
+
if (!existsSync(rootPackagePath)) {
|
|
189
|
+
return [];
|
|
190
|
+
}
|
|
191
|
+
const rootPackage = readPackageJson(rootPackagePath);
|
|
192
|
+
const workspacePatterns = Array.isArray(rootPackage.workspaces)
|
|
193
|
+
? rootPackage.workspaces.filter((entry): entry is string => typeof entry === 'string')
|
|
194
|
+
: [];
|
|
195
|
+
const paths: string[] = [];
|
|
196
|
+
for (const pattern of workspacePatterns) {
|
|
197
|
+
if (!pattern.endsWith('/*')) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const parent = join(root, pattern.slice(0, -2));
|
|
201
|
+
if (!existsSync(parent)) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
for (const entry of readdirSync(parent, { withFileTypes: true })) {
|
|
205
|
+
if (!entry.isDirectory()) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const packageJsonPath = join(parent, entry.name, 'package.json');
|
|
209
|
+
if (existsSync(packageJsonPath)) {
|
|
210
|
+
paths.push(packageJsonPath);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return paths.sort((a, b) => a.localeCompare(b));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function readPackageJson(path: string): BoundedTestPolicyPackageJson {
|
|
218
|
+
return JSON.parse(readFileSync(path, 'utf8')) as BoundedTestPolicyPackageJson;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function writePackageJson(path: string, packageJson: BoundedTestPolicyPackageJson): void {
|
|
222
|
+
writeFileSync(path, `${JSON.stringify(packageJson, null, 2)}\n`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function readProjectJson(path: string): BoundedTestPolicyProjectJson {
|
|
226
|
+
return JSON.parse(readFileSync(path, 'utf8')) as BoundedTestPolicyProjectJson;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function writeProjectJson(path: string, projectJson: BoundedTestPolicyProjectJson): void {
|
|
230
|
+
writeFileSync(path, `${JSON.stringify(projectJson, null, 2)}\n`);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function hasTestEntrypoint(
|
|
234
|
+
packageJson: BoundedTestPolicyPackageJson,
|
|
235
|
+
projectJson: BoundedTestPolicyProjectJson | undefined,
|
|
236
|
+
): boolean {
|
|
237
|
+
return (
|
|
238
|
+
typeof packageJson.scripts?.test === 'string' ||
|
|
239
|
+
isRecord(projectJson?.targets?.test) ||
|
|
240
|
+
isRecord(packageJson.nx?.targets?.test)
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function packageProjectName(
|
|
245
|
+
packageJson: BoundedTestPolicyPackageJson,
|
|
246
|
+
projectJson: BoundedTestPolicyProjectJson | undefined,
|
|
247
|
+
): string | null {
|
|
248
|
+
return projectJson?.name ?? packageJson.nx?.name ?? packageJson.name ?? null;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function projectJsonPathForPackageJson(packageJsonPath: string): string {
|
|
252
|
+
return join(dirname(packageJsonPath), 'project.json');
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
256
|
+
return value !== null && typeof value === 'object';
|
|
257
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
describe('Bun bootstrap imports', () => {
|
|
4
|
+
it('imports the main plugin entry point', async () => {
|
|
5
|
+
const plugin = await import('./index.js');
|
|
6
|
+
expect(plugin.createNodesV2).toBeDefined();
|
|
7
|
+
expect(Array.isArray(plugin.createNodesV2)).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('imports bounded-test-policy', async () => {
|
|
11
|
+
const policy = await import('./bounded-test-policy.js');
|
|
12
|
+
expect(typeof policy.applyWorkspaceBoundedTestTargetPolicy).toBe('function');
|
|
13
|
+
expect(typeof policy.checkWorkspaceBoundedTestTargetPolicy).toBe('function');
|
|
14
|
+
expect(typeof policy.applyBoundedTestTargetPolicy).toBe('function');
|
|
15
|
+
expect(typeof policy.checkBoundedTestTargetPolicy).toBe('function');
|
|
16
|
+
expect(typeof policy.boundedTestScriptAlias).toBe('function');
|
|
17
|
+
expect(typeof policy.resolveTestCommand).toBe('function');
|
|
18
|
+
expect(typeof policy.BOUNDED_TEST_EXECUTOR).toBe('string');
|
|
19
|
+
expect(typeof policy.BOUNDED_TEST_TIMEOUT_MS).toBe('number');
|
|
20
|
+
expect(typeof policy.BOUNDED_TEST_KILL_AFTER_MS).toBe('number');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('imports workspace-config-policy', async () => {
|
|
24
|
+
const policy = await import('./workspace-config-policy.js');
|
|
25
|
+
expect(typeof policy.checkWorkspaceConfigPolicy).toBe('function');
|
|
26
|
+
expect(typeof policy.applyWorkspaceConfigPolicy).toBe('function');
|
|
27
|
+
expect(Array.isArray(policy.BUILD_OUTPUT_DEPENDENCIES)).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('imports release-config-policy', async () => {
|
|
31
|
+
const policy = await import('./release-config-policy.js');
|
|
32
|
+
expect(typeof policy.checkReleaseConfigPolicy).toBe('function');
|
|
33
|
+
expect(typeof policy.applyReleaseConfigPolicy).toBe('function');
|
|
34
|
+
expect(typeof policy.SMOO_NX_VERSION_ACTIONS).toBe('string');
|
|
35
|
+
expect(typeof policy.SMOO_NX_RELEASE_TAG_PATTERN).toBe('string');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('imports package-target-policy', async () => {
|
|
39
|
+
const policy = await import('./package-target-policy.js');
|
|
40
|
+
expect(typeof policy.checkPackageTargetPolicy).toBe('function');
|
|
41
|
+
expect(typeof policy.applyPackageTargetPolicy).toBe('function');
|
|
42
|
+
expect(typeof policy.nxRunAlias).toBe('function');
|
|
43
|
+
expect(typeof policy.packageNxProjectName).toBe('function');
|
|
44
|
+
expect(Array.isArray(policy.BUILD_OUTPUT_DEPENDENCIES)).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('imports typecheck-test-policy', async () => {
|
|
48
|
+
const policy = await import('./typecheck-test-policy.js');
|
|
49
|
+
expect(typeof policy.checkTypecheckTestPolicy).toBe('function');
|
|
50
|
+
expect(typeof policy.applyTypecheckTestPolicy).toBe('function');
|
|
51
|
+
});
|
|
52
|
+
});
|