@smoothbricks/nx-plugin 0.1.0 → 0.2.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 (74) hide show
  1. package/README.md +35 -8
  2. package/dist/bounded-test-policy.d.ts +32 -0
  3. package/dist/bounded-test-policy.d.ts.map +1 -0
  4. package/dist/bounded-test-policy.js +184 -0
  5. package/dist/executors/bounded-exec/executor.d.ts +15 -0
  6. package/dist/executors/bounded-exec/executor.d.ts.map +1 -0
  7. package/dist/executors/bounded-exec/executor.js +179 -0
  8. package/dist/executors/bounded-exec/schema.d.ts +11 -0
  9. package/dist/executors/bounded-exec/schema.d.ts.map +1 -0
  10. package/dist/executors/bounded-exec/schema.js +1 -0
  11. package/dist/generators/bounded-test-targets/generator.d.ts +7 -0
  12. package/dist/generators/bounded-test-targets/generator.d.ts.map +1 -0
  13. package/dist/generators/bounded-test-targets/generator.js +40 -0
  14. package/dist/generators/bun-test-tracing/generator.d.ts.map +1 -1
  15. package/dist/generators/bun-test-tracing/generator.js +5 -4
  16. package/dist/generators/create-package/generator.d.ts +4 -0
  17. package/dist/generators/create-package/generator.d.ts.map +1 -0
  18. package/dist/generators/create-package/generator.js +204 -0
  19. package/dist/generators/make-public/generator.d.ts +4 -0
  20. package/dist/generators/make-public/generator.d.ts.map +1 -0
  21. package/dist/generators/make-public/generator.js +55 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +78 -7
  24. package/dist/package-target-policy.d.ts +31 -0
  25. package/dist/package-target-policy.d.ts.map +1 -0
  26. package/dist/package-target-policy.js +937 -0
  27. package/dist/release-config-policy.d.ts +32 -0
  28. package/dist/release-config-policy.d.ts.map +1 -0
  29. package/dist/release-config-policy.js +225 -0
  30. package/dist/typecheck-test-policy.d.ts +56 -0
  31. package/dist/typecheck-test-policy.d.ts.map +1 -0
  32. package/dist/typecheck-test-policy.js +640 -0
  33. package/dist/version-actions.cjs +78 -0
  34. package/dist/version-actions.d.cts +6 -0
  35. package/dist/version-actions.d.cts.map +1 -0
  36. package/dist/workspace-config-policy.d.ts +33 -0
  37. package/dist/workspace-config-policy.d.ts.map +1 -0
  38. package/dist/workspace-config-policy.js +334 -0
  39. package/executors.json +9 -0
  40. package/generators.json +15 -0
  41. package/package.json +45 -11
  42. package/src/bounded-test-policy.test.ts +257 -0
  43. package/src/bounded-test-policy.ts +257 -0
  44. package/src/bun-bootstrap.test.ts +52 -0
  45. package/src/executors/bounded-exec/executor.test.ts +258 -0
  46. package/src/executors/bounded-exec/executor.ts +228 -0
  47. package/src/executors/bounded-exec/schema.json +68 -0
  48. package/src/executors/bounded-exec/schema.ts +10 -0
  49. package/src/generators/bounded-test-targets/generator.test.ts +136 -0
  50. package/src/generators/bounded-test-targets/generator.ts +67 -0
  51. package/src/generators/bounded-test-targets/schema.d.ts +3 -0
  52. package/src/generators/bounded-test-targets/schema.json +14 -0
  53. package/src/generators/bun-test-tracing/generator.test.ts +25 -1
  54. package/src/generators/bun-test-tracing/generator.ts +6 -5
  55. package/src/generators/create-package/generator.test.ts +206 -0
  56. package/src/generators/create-package/generator.ts +251 -0
  57. package/src/generators/create-package/schema.d.ts +5 -0
  58. package/src/generators/create-package/schema.json +24 -0
  59. package/src/generators/make-public/generator.test.ts +137 -0
  60. package/src/generators/make-public/generator.ts +83 -0
  61. package/src/generators/make-public/schema.d.ts +3 -0
  62. package/src/generators/make-public/schema.json +13 -0
  63. package/src/index.test.ts +85 -18
  64. package/src/index.ts +99 -8
  65. package/src/package-target-policy.test.ts +785 -0
  66. package/src/package-target-policy.ts +1140 -0
  67. package/src/release-config-policy.test.ts +280 -0
  68. package/src/release-config-policy.ts +258 -0
  69. package/src/typecheck-test-policy.test.ts +797 -0
  70. package/src/typecheck-test-policy.ts +752 -0
  71. package/src/version-actions.cts +94 -0
  72. package/src/workspace-config-policy.test.ts +376 -0
  73. package/src/workspace-config-policy.ts +390 -0
  74. package/src/test-suite-tracer.ts +0 -0
@@ -0,0 +1,752 @@
1
+ import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
2
+ import { join, relative } from 'node:path';
3
+ import type { Tree } from 'nx/src/devkit-exports.js';
4
+ import { getProjects, readJson, readProjectConfiguration, updateJson, writeJson } from 'nx/src/devkit-exports.js';
5
+
6
+ import type { NxPolicyIssue } from './workspace-config-policy.js';
7
+
8
+ export type TestRunner = 'bun' | 'vitest';
9
+
10
+ const workspaceDependencyFields = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];
11
+
12
+ /**
13
+ * Detect test runners from a package.json object and optional project targets
14
+ * (from project.json or nx config).
15
+ */
16
+ export function detectPackageTestRunners(
17
+ pkg: Record<string, unknown>,
18
+ projectTargets?: Record<string, unknown>,
19
+ ): ReadonlySet<TestRunner> {
20
+ const runners = new Set<TestRunner>();
21
+
22
+ // Check package.json scripts
23
+ const scripts = recordProperty(pkg, 'scripts');
24
+ if (scripts) {
25
+ for (const command of Object.values(scripts)) {
26
+ if (typeof command !== 'string') {
27
+ continue;
28
+ }
29
+ const runner = detectTestRunnerFromCommand(command);
30
+ if (runner) {
31
+ runners.add(runner);
32
+ }
33
+ }
34
+ }
35
+
36
+ // Check package.json nx.targets
37
+ const nxTargets = recordProperty(recordProperty(pkg, 'nx'), 'targets');
38
+ if (nxTargets) {
39
+ for (const target of Object.values(nxTargets)) {
40
+ if (!isRecord(target)) {
41
+ continue;
42
+ }
43
+ const options = recordProperty(target, 'options');
44
+ const command = options ? stringProperty(options, 'command') : null;
45
+ if (command) {
46
+ const runner = detectTestRunnerFromCommand(command);
47
+ if (runner) {
48
+ runners.add(runner);
49
+ }
50
+ }
51
+ }
52
+ }
53
+
54
+ // Check project.json / project config targets
55
+ if (projectTargets) {
56
+ for (const target of Object.values(projectTargets)) {
57
+ if (!isRecord(target)) {
58
+ continue;
59
+ }
60
+ const options = recordProperty(target, 'options');
61
+ const command = options ? stringProperty(options, 'command') : null;
62
+ if (command) {
63
+ const runner = detectTestRunnerFromCommand(command);
64
+ if (runner) {
65
+ runners.add(runner);
66
+ }
67
+ }
68
+ }
69
+ }
70
+
71
+ return runners;
72
+ }
73
+
74
+ /**
75
+ * Validate tsconfig.test.json contents (in-memory object).
76
+ * Returns policy issues found.
77
+ */
78
+ export function checkTypecheckTestConfig(
79
+ tsconfigTest: Record<string, unknown> | null,
80
+ packagePath: string,
81
+ ): NxPolicyIssue[] {
82
+ if (!tsconfigTest) {
83
+ return [];
84
+ }
85
+ const issues: NxPolicyIssue[] = [];
86
+ const tsconfigTestPath = join(packagePath, 'tsconfig.test.json');
87
+ const compilerOptions = recordProperty(tsconfigTest, 'compilerOptions');
88
+
89
+ if (!compilerOptions || compilerOptions.noEmit !== true) {
90
+ issues.push({
91
+ path: tsconfigTestPath,
92
+ message: 'compilerOptions.noEmit must be true',
93
+ });
94
+ }
95
+ if (compilerOptions?.composite === true) {
96
+ issues.push({
97
+ path: tsconfigTestPath,
98
+ message:
99
+ 'must not set compilerOptions.composite = true. ' +
100
+ 'Bun test typechecking is a no-emit validation pass, not a TypeScript build-mode project.',
101
+ });
102
+ }
103
+ if (compilerOptions?.declaration === true) {
104
+ issues.push({
105
+ path: tsconfigTestPath,
106
+ message: 'must not set compilerOptions.declaration = true',
107
+ });
108
+ }
109
+ if (compilerOptions?.declarationMap === true) {
110
+ issues.push({
111
+ path: tsconfigTestPath,
112
+ message: 'must not set compilerOptions.declarationMap = true',
113
+ });
114
+ }
115
+ if (compilerOptions?.outDir === 'dist-test') {
116
+ issues.push({
117
+ path: tsconfigTestPath,
118
+ message: 'must not emit to dist-test',
119
+ });
120
+ }
121
+ if (typeof compilerOptions?.tsBuildInfoFile === 'string' && compilerOptions.tsBuildInfoFile.includes('dist-test')) {
122
+ issues.push({
123
+ path: tsconfigTestPath,
124
+ message: 'must not write tsbuildinfo under dist-test',
125
+ });
126
+ }
127
+
128
+ return issues;
129
+ }
130
+
131
+ /**
132
+ * Check if tsconfig.json has a bad reference to ./tsconfig.test.json.
133
+ */
134
+ export function checkTsconfigTestReference(
135
+ projectTsconfig: Record<string, unknown> | null,
136
+ packagePath: string,
137
+ ): NxPolicyIssue[] {
138
+ if (!projectTsconfigHasTestReference(projectTsconfig)) {
139
+ return [];
140
+ }
141
+ return [
142
+ {
143
+ path: join(packagePath, 'tsconfig.json'),
144
+ message:
145
+ 'must not reference ./tsconfig.test.json. ' +
146
+ 'Test typechecking is run by the inferred typecheck-tests target with tsc --noEmit, not TypeScript build mode.',
147
+ },
148
+ ];
149
+ }
150
+
151
+ /**
152
+ * Apply defaults to a tsconfig.test.json object (in-memory mutation).
153
+ * Returns whether anything changed.
154
+ */
155
+ export function applyTypecheckTestDefaults(
156
+ tsconfigTest: Record<string, unknown>,
157
+ options: {
158
+ testRunners: ReadonlySet<TestRunner>;
159
+ tsconfigLibExtends?: string;
160
+ libCompilerOptions?: Record<string, unknown>;
161
+ referencePaths: string[];
162
+ },
163
+ ): boolean {
164
+ const extendsValue = options.tsconfigLibExtends ?? '../../tsconfig.base.json';
165
+ let changed = setMissingStringProperty(tsconfigTest, 'extends', extendsValue);
166
+
167
+ const compilerOptions = getOrCreateRecord(tsconfigTest, 'compilerOptions');
168
+
169
+ // Copy relevant compiler options from lib tsconfig
170
+ if (options.libCompilerOptions) {
171
+ for (const key of ['baseUrl', 'module', 'moduleResolution', 'jsx', 'lib']) {
172
+ if (Object.hasOwn(options.libCompilerOptions, key) && compilerOptions[key] !== options.libCompilerOptions[key]) {
173
+ compilerOptions[key] = options.libCompilerOptions[key];
174
+ changed = true;
175
+ }
176
+ }
177
+ }
178
+
179
+ changed = setBooleanProperty(compilerOptions, 'composite', false) || changed;
180
+ changed = setBooleanProperty(compilerOptions, 'declaration', false) || changed;
181
+ changed = setBooleanProperty(compilerOptions, 'declarationMap', false) || changed;
182
+ changed = setBooleanProperty(compilerOptions, 'emitDeclarationOnly', false) || changed;
183
+ changed = setBooleanProperty(compilerOptions, 'noEmit', true) || changed;
184
+
185
+ if (options.testRunners.has('bun')) {
186
+ changed = mergeStringListProperty(compilerOptions, 'types', ['bun']) || changed;
187
+ }
188
+
189
+ if ('outDir' in compilerOptions) {
190
+ delete compilerOptions.outDir;
191
+ changed = true;
192
+ }
193
+ if ('tsBuildInfoFile' in compilerOptions) {
194
+ delete compilerOptions.tsBuildInfoFile;
195
+ changed = true;
196
+ }
197
+
198
+ changed =
199
+ mergeStringListProperty(tsconfigTest, 'include', [
200
+ 'src/**/*.test.ts',
201
+ 'src/**/*.spec.ts',
202
+ 'src/**/__tests__/**/*.ts',
203
+ 'src/**/__tests__/**/*.tsx',
204
+ 'src/test-suite-tracer.ts',
205
+ ]) || changed;
206
+
207
+ for (const referencePath of options.referencePaths) {
208
+ changed = addTsconfigReference(tsconfigTest, referencePath) || changed;
209
+ }
210
+
211
+ return changed;
212
+ }
213
+
214
+ /**
215
+ * Remove the ./tsconfig.test.json reference from a tsconfig.json object.
216
+ * Returns whether anything changed.
217
+ */
218
+ export function removeTsconfigTestReference(projectTsconfig: Record<string, unknown>): boolean {
219
+ if (!projectTsconfigHasTestReference(projectTsconfig)) {
220
+ return false;
221
+ }
222
+ const references = Array.isArray(projectTsconfig.references) ? projectTsconfig.references : [];
223
+ projectTsconfig.references = references.filter((entry) => !isRecord(entry) || entry.path !== './tsconfig.test.json');
224
+ return true;
225
+ }
226
+
227
+ /**
228
+ * Check all workspace packages for typecheck test policy compliance using an Nx Tree.
229
+ */
230
+ export function checkTypecheckTestPolicyTree(tree: Tree): NxPolicyIssue[] {
231
+ const issues: NxPolicyIssue[] = [];
232
+
233
+ for (const [projectName, config] of getProjects(tree)) {
234
+ const pkgPath = `${config.root}/package.json`;
235
+ if (!tree.exists(pkgPath)) continue;
236
+ if (config.root === '.' || config.root === '') continue;
237
+
238
+ const pkg = readJson<Record<string, unknown>>(tree, pkgPath);
239
+
240
+ // Get targets from project config (handles both package.json and project.json)
241
+ const projectConfig = readProjectConfiguration(tree, projectName);
242
+ const projectTargets = projectConfig.targets ?? {};
243
+
244
+ const testRunners = detectPackageTestRunners(pkg, projectTargets);
245
+ if (testRunners.size === 0) continue;
246
+
247
+ // Check tsconfig.test.json exists
248
+ const tsconfigTestPath = `${config.root}/tsconfig.test.json`;
249
+ if (!tree.exists(tsconfigTestPath)) {
250
+ issues.push({
251
+ path: tsconfigTestPath,
252
+ message: `${formatTestRunnerList(testRunners)} requires tsconfig.test.json because those runners do not typecheck test files by default`,
253
+ });
254
+ continue;
255
+ }
256
+
257
+ // Check tsconfig.test.json contents
258
+ const tsconfigTest = readJson<Record<string, unknown>>(tree, tsconfigTestPath);
259
+ issues.push(...checkTypecheckTestConfig(tsconfigTest, config.root));
260
+
261
+ // Check tsconfig.json reference
262
+ const tsconfigPath = `${config.root}/tsconfig.json`;
263
+ if (tree.exists(tsconfigPath)) {
264
+ const tsconfig = readJson<Record<string, unknown>>(tree, tsconfigPath);
265
+ issues.push(...checkTsconfigTestReference(tsconfig, config.root));
266
+ }
267
+ }
268
+
269
+ return issues;
270
+ }
271
+
272
+ /**
273
+ * Fix all workspace packages' tsconfig.test.json configuration using an Nx Tree.
274
+ * Returns whether any files changed.
275
+ */
276
+ export function applyTypecheckTestPolicyTree(tree: Tree): boolean {
277
+ let changed = false;
278
+ const workspacePackages = collectWorkspacePackagesTree(tree);
279
+
280
+ for (const [projectName, config] of getProjects(tree)) {
281
+ const pkgPath = `${config.root}/package.json`;
282
+ if (!tree.exists(pkgPath)) continue;
283
+ if (config.root === '.' || config.root === '') continue;
284
+
285
+ const pkg = readJson<Record<string, unknown>>(tree, pkgPath);
286
+ const projectConfig = readProjectConfiguration(tree, projectName);
287
+ const projectTargets = projectConfig.targets ?? {};
288
+
289
+ const testRunners = detectPackageTestRunners(pkg, projectTargets);
290
+ if (testRunners.size === 0) continue;
291
+
292
+ // Apply tsconfig.test.json defaults
293
+ const tsconfigTestPath = `${config.root}/tsconfig.test.json`;
294
+ const isNew = !tree.exists(tsconfigTestPath);
295
+ const tsconfigTest = isNew ? {} : readJson<Record<string, unknown>>(tree, tsconfigTestPath);
296
+
297
+ // Read lib tsconfig for extends and compiler options
298
+ const libTsconfigPath = `${config.root}/tsconfig.lib.json`;
299
+ const libTsconfig = tree.exists(libTsconfigPath) ? readJson<Record<string, unknown>>(tree, libTsconfigPath) : null;
300
+ const tsconfigLibExtends =
301
+ (libTsconfig ? stringProperty(libTsconfig, 'extends') : null) ?? '../../tsconfig.base.json';
302
+ const libCompilerOptions = libTsconfig ? recordProperty(libTsconfig, 'compilerOptions') : null;
303
+
304
+ // Collect reference paths
305
+ const referencePaths = collectReferencePathsTree(tree, config.root, pkg, workspacePackages);
306
+
307
+ if (
308
+ applyTypecheckTestDefaults(tsconfigTest, {
309
+ testRunners,
310
+ tsconfigLibExtends,
311
+ libCompilerOptions: libCompilerOptions ?? undefined,
312
+ referencePaths,
313
+ }) ||
314
+ isNew
315
+ ) {
316
+ writeJson(tree, tsconfigTestPath, tsconfigTest);
317
+ changed = true;
318
+ }
319
+
320
+ // Remove test reference from tsconfig.json
321
+ const tsconfigPath = `${config.root}/tsconfig.json`;
322
+ if (tree.exists(tsconfigPath)) {
323
+ updateJson(tree, tsconfigPath, (tsconfig: Record<string, unknown>) => {
324
+ if (removeTsconfigTestReference(tsconfig)) {
325
+ changed = true;
326
+ }
327
+ return tsconfig;
328
+ });
329
+ }
330
+ }
331
+
332
+ return changed;
333
+ }
334
+
335
+ /**
336
+ * Check all workspace packages for typecheck test policy compliance.
337
+ *
338
+ * For each package that uses bun test or vitest:
339
+ * - tsconfig.test.json must exist
340
+ * - tsconfig.test.json must be noEmit, not composite, no declaration, no dist-test output
341
+ * - tsconfig.json must NOT reference ./tsconfig.test.json
342
+ */
343
+ export function checkTypecheckTestPolicy(root: string): NxPolicyIssue[] {
344
+ const issues: NxPolicyIssue[] = [];
345
+ for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
346
+ const pkg = readJsonObject(packageJsonPath);
347
+ if (!pkg) {
348
+ continue;
349
+ }
350
+ const packageDir = packageJsonPath.slice(0, -'/package.json'.length);
351
+ const packagePath = relative(root, packageDir);
352
+ if (packagePath === '' || packagePath === '.') {
353
+ continue;
354
+ }
355
+
356
+ const testRunners = detectPackageTestRunners(pkg);
357
+ if (testRunners.size === 0) {
358
+ continue;
359
+ }
360
+
361
+ // tsconfig.test.json must exist
362
+ const tsconfigTestPath = join(root, packagePath, 'tsconfig.test.json');
363
+ if (!existsSync(tsconfigTestPath)) {
364
+ issues.push({
365
+ path: tsconfigTestPath,
366
+ message: `${formatTestRunnerList(testRunners)} requires tsconfig.test.json because those runners do not typecheck test files by default`,
367
+ });
368
+ continue;
369
+ }
370
+
371
+ // tsconfig.test.json must have correct settings
372
+ const tsconfig = readJsonObject(tsconfigTestPath);
373
+ if (tsconfig) {
374
+ // Use absolute path for filesystem layer
375
+ const absoluteIssues = checkTypecheckTestConfig(tsconfig, join(root, packagePath));
376
+ issues.push(...absoluteIssues);
377
+ }
378
+
379
+ // tsconfig.json must NOT reference ./tsconfig.test.json
380
+ const projectTsconfigPath = join(root, packagePath, 'tsconfig.json');
381
+ const projectTsconfig = readJsonObject(projectTsconfigPath);
382
+ const refIssues = checkTsconfigTestReference(projectTsconfig, join(root, packagePath));
383
+ issues.push(...refIssues);
384
+ }
385
+ return issues;
386
+ }
387
+
388
+ /**
389
+ * Fix all workspace packages' tsconfig.test.json configuration.
390
+ * Returns whether any files changed.
391
+ */
392
+ export function applyTypecheckTestPolicy(root: string): boolean {
393
+ const workspaceNames = getWorkspacePackageNames(root);
394
+ let changed = false;
395
+
396
+ for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
397
+ const pkg = readJsonObject(packageJsonPath);
398
+ if (!pkg) {
399
+ continue;
400
+ }
401
+ const packageDir = packageJsonPath.slice(0, -'/package.json'.length);
402
+ const packagePath = relative(root, packageDir);
403
+ if (packagePath === '' || packagePath === '.') {
404
+ continue;
405
+ }
406
+
407
+ const testRunners = detectPackageTestRunners(pkg);
408
+ if (testRunners.size === 0) {
409
+ continue;
410
+ }
411
+
412
+ // Create/update tsconfig.test.json
413
+ const tsconfigTestPath = join(root, packagePath, 'tsconfig.test.json');
414
+ const existing = readJsonObject(tsconfigTestPath);
415
+ const tsconfigTest = existing ?? {};
416
+ let fileChanged = existing === null;
417
+
418
+ // Read lib tsconfig for extends and compiler options
419
+ const libTsconfig = readJsonObject(join(root, packagePath, 'tsconfig.lib.json'));
420
+ const tsconfigLibExtends = stringProperty(libTsconfig ?? {}, 'extends') ?? '../../tsconfig.base.json';
421
+ const libCompilerOptions = libTsconfig ? recordProperty(libTsconfig, 'compilerOptions') : null;
422
+
423
+ // Collect reference paths
424
+ const referencePaths = collectTsconfigTestReferencePaths(root, packagePath, pkg, workspaceNames);
425
+
426
+ fileChanged =
427
+ applyTypecheckTestDefaults(tsconfigTest, {
428
+ testRunners,
429
+ tsconfigLibExtends,
430
+ libCompilerOptions: libCompilerOptions ?? undefined,
431
+ referencePaths,
432
+ }) || fileChanged;
433
+
434
+ if (fileChanged) {
435
+ writeJsonObjectFs(tsconfigTestPath, tsconfigTest);
436
+ changed = true;
437
+ }
438
+
439
+ // Remove ./tsconfig.test.json from tsconfig.json references if present
440
+ const projectTsconfigPath = join(root, packagePath, 'tsconfig.json');
441
+ const projectTsconfig = readJsonObject(projectTsconfigPath);
442
+ if (projectTsconfig && removeTsconfigTestReference(projectTsconfig)) {
443
+ writeJsonObjectFs(projectTsconfigPath, projectTsconfig);
444
+ changed = true;
445
+ }
446
+ }
447
+
448
+ return changed;
449
+ }
450
+
451
+ function detectTestRunnerFromCommand(command: string): TestRunner | null {
452
+ const trimmed = parseEnvPrefixedCommand(command).command.trim();
453
+ if (/^bun\s+test(?:\s|$)/.test(trimmed)) {
454
+ return 'bun';
455
+ }
456
+ if (/^vitest(?:\s|$)/.test(trimmed)) {
457
+ return 'vitest';
458
+ }
459
+ return null;
460
+ }
461
+
462
+ function formatTestRunnerList(testRunners: ReadonlySet<TestRunner>): string {
463
+ const labels: string[] = [];
464
+ if (testRunners.has('bun')) {
465
+ labels.push('bun test');
466
+ }
467
+ if (testRunners.has('vitest')) {
468
+ labels.push('vitest');
469
+ }
470
+ return labels.join(' or ');
471
+ }
472
+
473
+ function parseEnvPrefixedCommand(command: string): { command: string; env: Record<string, string> } {
474
+ const env: Record<string, string> = {};
475
+ let rest = command.trimStart();
476
+ while (true) {
477
+ const match = /^([A-Za-z_][A-Za-z0-9_]*)=/.exec(rest);
478
+ if (!match?.[1]) {
479
+ return { command: rest.trim(), env };
480
+ }
481
+ let index = match[0].length;
482
+ let value = '';
483
+ const quote = rest[index];
484
+ if (quote === '"' || quote === "'") {
485
+ index += 1;
486
+ const end = rest.indexOf(quote, index);
487
+ if (end === -1) {
488
+ return { command: command.trim(), env: {} };
489
+ }
490
+ value = rest.slice(index, end);
491
+ index = end + 1;
492
+ } else {
493
+ const end = rest.slice(index).search(/\s/);
494
+ const valueEnd = end === -1 ? rest.length : index + end;
495
+ value = rest.slice(index, valueEnd);
496
+ index = valueEnd;
497
+ }
498
+ if (index < rest.length && !/\s/.test(rest[index] ?? '')) {
499
+ return { command: command.trim(), env: {} };
500
+ }
501
+ env[match[1]] = value;
502
+ rest = rest.slice(index).trimStart();
503
+ }
504
+ }
505
+
506
+ function projectTsconfigHasTestReference(projectTsconfig: Record<string, unknown> | null): boolean {
507
+ return Boolean(
508
+ projectTsconfig &&
509
+ Array.isArray(projectTsconfig.references) &&
510
+ projectTsconfig.references.some((entry) => isRecord(entry) && entry.path === './tsconfig.test.json'),
511
+ );
512
+ }
513
+
514
+ interface TreeWorkspacePackage {
515
+ name: string;
516
+ path: string;
517
+ }
518
+
519
+ function collectWorkspacePackagesTree(tree: Tree): TreeWorkspacePackage[] {
520
+ const packages: TreeWorkspacePackage[] = [];
521
+ for (const [, config] of getProjects(tree)) {
522
+ const pkgPath = `${config.root}/package.json`;
523
+ if (!tree.exists(pkgPath)) continue;
524
+ const pkg = readJson<{ name?: string }>(tree, pkgPath);
525
+ if (pkg.name) {
526
+ packages.push({ name: pkg.name, path: config.root });
527
+ }
528
+ }
529
+ return packages;
530
+ }
531
+
532
+ function collectReferencePathsTree(
533
+ tree: Tree,
534
+ packageRoot: string,
535
+ pkg: Record<string, unknown>,
536
+ workspacePackages: TreeWorkspacePackage[],
537
+ ): string[] {
538
+ const paths: string[] = [];
539
+ const libTsconfigPath = `${packageRoot}/tsconfig.lib.json`;
540
+ if (tree.exists(libTsconfigPath)) {
541
+ paths.push('./tsconfig.lib.json');
542
+ }
543
+
544
+ const packagesByName = new Map(workspacePackages.map((p) => [p.name, p]));
545
+ for (const field of workspaceDependencyFields) {
546
+ const deps = recordProperty(pkg, field);
547
+ if (!deps) continue;
548
+ for (const depName of Object.keys(deps)) {
549
+ const depPkg = packagesByName.get(depName);
550
+ if (!depPkg) continue;
551
+ const depTsconfig = `${depPkg.path}/tsconfig.lib.json`;
552
+ if (!tree.exists(depTsconfig)) continue;
553
+ const refPath = relative(packageRoot, depTsconfig).replaceAll('\\', '/');
554
+ if (!paths.includes(refPath)) paths.push(refPath);
555
+ }
556
+ }
557
+ return paths;
558
+ }
559
+
560
+ function listWorkspacePackageJsonPaths(root: string): string[] {
561
+ const rootPackagePath = join(root, 'package.json');
562
+ if (!existsSync(rootPackagePath)) {
563
+ return [];
564
+ }
565
+ const rootPackage = readJsonObject(rootPackagePath);
566
+ if (!rootPackage) {
567
+ return [];
568
+ }
569
+ const workspacePatterns = Array.isArray(rootPackage.workspaces)
570
+ ? rootPackage.workspaces.filter((entry): entry is string => typeof entry === 'string')
571
+ : [];
572
+ const paths: string[] = [];
573
+ for (const pattern of workspacePatterns) {
574
+ if (!pattern.endsWith('/*')) {
575
+ continue;
576
+ }
577
+ const parent = join(root, pattern.slice(0, -2));
578
+ if (!existsSync(parent)) {
579
+ continue;
580
+ }
581
+ for (const entry of readdirSync(parent, { withFileTypes: true })) {
582
+ if (!entry.isDirectory()) {
583
+ continue;
584
+ }
585
+ const packageJsonPath = join(parent, entry.name, 'package.json');
586
+ if (existsSync(packageJsonPath)) {
587
+ paths.push(packageJsonPath);
588
+ }
589
+ }
590
+ }
591
+ return paths.sort((a, b) => a.localeCompare(b));
592
+ }
593
+
594
+ function getWorkspacePackageNames(root: string): Set<string> {
595
+ const names = new Set<string>();
596
+ for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
597
+ const pkg = readJsonObject(packageJsonPath);
598
+ if (pkg) {
599
+ const name = stringProperty(pkg, 'name');
600
+ if (name) {
601
+ names.add(name);
602
+ }
603
+ }
604
+ }
605
+ return names;
606
+ }
607
+
608
+ interface WorkspacePackageInfo {
609
+ name: string;
610
+ path: string;
611
+ }
612
+
613
+ function getWorkspacePackages(root: string): WorkspacePackageInfo[] {
614
+ const packages: WorkspacePackageInfo[] = [];
615
+ for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
616
+ const pkg = readJsonObject(packageJsonPath);
617
+ if (!pkg) {
618
+ continue;
619
+ }
620
+ const name = stringProperty(pkg, 'name');
621
+ if (!name) {
622
+ continue;
623
+ }
624
+ const packageDir = packageJsonPath.slice(0, -'/package.json'.length);
625
+ packages.push({ name, path: relative(root, packageDir) });
626
+ }
627
+ return packages;
628
+ }
629
+
630
+ function collectTsconfigTestReferencePaths(
631
+ root: string,
632
+ packagePath: string,
633
+ pkg: Record<string, unknown>,
634
+ workspaceNames: ReadonlySet<string>,
635
+ ): string[] {
636
+ const paths = existsSync(join(root, packagePath, 'tsconfig.lib.json')) ? ['./tsconfig.lib.json'] : [];
637
+ const packagesByName = new Map(getWorkspacePackages(root).map((workspacePkg) => [workspacePkg.name, workspacePkg]));
638
+ for (const field of workspaceDependencyFields) {
639
+ const dependencies = recordProperty(pkg, field);
640
+ if (!dependencies) {
641
+ continue;
642
+ }
643
+ for (const dependencyName of Object.keys(dependencies)) {
644
+ if (!workspaceNames.has(dependencyName)) {
645
+ continue;
646
+ }
647
+ const dependencyPackage = packagesByName.get(dependencyName);
648
+ if (!dependencyPackage) {
649
+ continue;
650
+ }
651
+ const dependencyTsconfig = join(root, dependencyPackage.path, 'tsconfig.lib.json');
652
+ if (!existsSync(dependencyTsconfig)) {
653
+ continue;
654
+ }
655
+ const refPath = relative(join(root, packagePath), dependencyTsconfig).replaceAll('\\', '/');
656
+ if (!paths.includes(refPath)) {
657
+ paths.push(refPath);
658
+ }
659
+ }
660
+ }
661
+ return paths;
662
+ }
663
+
664
+ function readJsonObject(path: string): Record<string, unknown> | null {
665
+ if (!existsSync(path)) {
666
+ return null;
667
+ }
668
+ try {
669
+ const parsed: unknown = JSON.parse(readFileSync(path, 'utf8'));
670
+ return isRecord(parsed) ? parsed : null;
671
+ } catch {
672
+ return null;
673
+ }
674
+ }
675
+
676
+ function writeJsonObjectFs(path: string, value: Record<string, unknown>): void {
677
+ writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`);
678
+ }
679
+
680
+ function isRecord(value: unknown): value is Record<string, unknown> {
681
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
682
+ }
683
+
684
+ function stringProperty(record: Record<string, unknown>, key: string): string | null {
685
+ const value = record[key];
686
+ return typeof value === 'string' && value.length > 0 ? value : null;
687
+ }
688
+
689
+ function recordProperty(record: Record<string, unknown> | null, key: string): Record<string, unknown> | null {
690
+ if (!record) return null;
691
+ const value = record[key];
692
+ return isRecord(value) ? value : null;
693
+ }
694
+
695
+ function getOrCreateRecord(record: Record<string, unknown>, key: string): Record<string, unknown> {
696
+ const value = record[key];
697
+ if (isRecord(value)) {
698
+ return value;
699
+ }
700
+ const next: Record<string, unknown> = {};
701
+ record[key] = next;
702
+ return next;
703
+ }
704
+
705
+ function setMissingStringProperty(record: Record<string, unknown>, key: string, value: string): boolean {
706
+ if (typeof record[key] === 'string') {
707
+ return false;
708
+ }
709
+ record[key] = value;
710
+ return true;
711
+ }
712
+
713
+ function setBooleanProperty(record: Record<string, unknown>, key: string, value: boolean): boolean {
714
+ if (record[key] === value) {
715
+ return false;
716
+ }
717
+ record[key] = value;
718
+ return true;
719
+ }
720
+
721
+ function mergeStringListProperty(record: Record<string, unknown>, key: string, values: string[]): boolean {
722
+ const rawCurrent = record[key];
723
+ const current = Array.isArray(rawCurrent)
724
+ ? rawCurrent.filter((entry): entry is string => typeof entry === 'string')
725
+ : [];
726
+ const next = [...current];
727
+ for (const value of values) {
728
+ if (!next.includes(value)) {
729
+ next.push(value);
730
+ }
731
+ }
732
+ if (
733
+ Array.isArray(rawCurrent) &&
734
+ next.length === rawCurrent.length &&
735
+ next.every((entry, index) => entry === rawCurrent[index])
736
+ ) {
737
+ return false;
738
+ }
739
+ record[key] = next;
740
+ return true;
741
+ }
742
+
743
+ function addTsconfigReference(tsconfig: Record<string, unknown>, path: string): boolean {
744
+ const current = Array.isArray(tsconfig.references)
745
+ ? tsconfig.references.filter((entry): entry is Record<string, unknown> => isRecord(entry))
746
+ : [];
747
+ if (current.some((entry) => entry.path === path)) {
748
+ return false;
749
+ }
750
+ tsconfig.references = [...current, { path }];
751
+ return true;
752
+ }