@smoothbricks/cli 0.1.1 → 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.
Files changed (117) hide show
  1. package/README.md +155 -30
  2. package/dist/cli.d.ts.map +1 -1
  3. package/dist/cli.js +91 -7
  4. package/dist/devenv/index.d.ts +10 -0
  5. package/dist/devenv/index.d.ts.map +1 -0
  6. package/dist/devenv/index.js +27 -0
  7. package/dist/lib/cli-package.d.ts +4 -0
  8. package/dist/lib/cli-package.d.ts.map +1 -0
  9. package/dist/lib/cli-package.js +15 -0
  10. package/dist/lib/run.d.ts +5 -0
  11. package/dist/lib/run.d.ts.map +1 -1
  12. package/dist/lib/run.js +13 -0
  13. package/dist/lib/workspace.d.ts +15 -1
  14. package/dist/lib/workspace.d.ts.map +1 -1
  15. package/dist/lib/workspace.js +25 -2
  16. package/dist/monorepo/commit-msg.d.ts +8 -1
  17. package/dist/monorepo/commit-msg.d.ts.map +1 -1
  18. package/dist/monorepo/commit-msg.js +85 -7
  19. package/dist/monorepo/index.d.ts +19 -2
  20. package/dist/monorepo/index.d.ts.map +1 -1
  21. package/dist/monorepo/index.js +104 -16
  22. package/dist/monorepo/managed-files.js +1 -1
  23. package/dist/monorepo/nx-sync.d.ts +2 -1
  24. package/dist/monorepo/nx-sync.d.ts.map +1 -1
  25. package/dist/monorepo/nx-sync.js +8 -2
  26. package/dist/monorepo/package-hygiene.d.ts +6 -2
  27. package/dist/monorepo/package-hygiene.d.ts.map +1 -1
  28. package/dist/monorepo/package-hygiene.js +12 -4
  29. package/dist/monorepo/package-policy.d.ts +22 -2
  30. package/dist/monorepo/package-policy.d.ts.map +1 -1
  31. package/dist/monorepo/package-policy.js +1167 -8
  32. package/dist/monorepo/packed-package.d.ts +3 -0
  33. package/dist/monorepo/packed-package.d.ts.map +1 -1
  34. package/dist/monorepo/packed-package.js +68 -36
  35. package/dist/monorepo/packs/index.d.ts +20 -1
  36. package/dist/monorepo/packs/index.d.ts.map +1 -1
  37. package/dist/monorepo/packs/index.js +238 -19
  38. package/dist/monorepo/publish-workflow.d.ts +0 -4
  39. package/dist/monorepo/publish-workflow.d.ts.map +1 -1
  40. package/dist/monorepo/publish-workflow.js +5 -12
  41. package/dist/monorepo/tool-validation.d.ts +22 -0
  42. package/dist/monorepo/tool-validation.d.ts.map +1 -0
  43. package/dist/monorepo/tool-validation.js +399 -0
  44. package/dist/nx/index.d.ts +24 -0
  45. package/dist/nx/index.d.ts.map +1 -0
  46. package/dist/nx/index.js +94 -0
  47. package/dist/release/bootstrap-npm-packages.d.ts +19 -0
  48. package/dist/release/bootstrap-npm-packages.d.ts.map +1 -0
  49. package/dist/release/bootstrap-npm-packages.js +58 -0
  50. package/dist/release/candidates.d.ts +11 -0
  51. package/dist/release/candidates.d.ts.map +1 -0
  52. package/dist/release/candidates.js +181 -0
  53. package/dist/release/core.d.ts +1 -0
  54. package/dist/release/core.d.ts.map +1 -1
  55. package/dist/release/github-release.js +2 -2
  56. package/dist/release/index.d.ts +25 -0
  57. package/dist/release/index.d.ts.map +1 -1
  58. package/dist/release/index.js +247 -49
  59. package/dist/release/npm-auth.d.ts +0 -1
  60. package/dist/release/npm-auth.d.ts.map +1 -1
  61. package/dist/release/npm-auth.js +4 -16
  62. package/dist/release/orchestration.d.ts +2 -1
  63. package/dist/release/orchestration.d.ts.map +1 -1
  64. package/dist/release/orchestration.js +9 -1
  65. package/dist/release/publish-plan.js +1 -1
  66. package/managed/raw/git-format-staged.yml +54 -0
  67. package/managed/raw/tooling/git-hooks/commit-msg.sh +1 -1
  68. package/managed/templates/github/actions/setup-devenv/action.yml +1 -7
  69. package/managed/templates/github/workflows/ci.yml +1 -0
  70. package/package.json +15 -3
  71. package/src/cli.ts +134 -20
  72. package/src/devenv/index.test.ts +49 -0
  73. package/src/devenv/index.ts +38 -0
  74. package/src/lib/cli-package.ts +18 -0
  75. package/src/lib/run.ts +19 -0
  76. package/src/lib/workspace.ts +41 -2
  77. package/src/monorepo/__tests__/publish-workflow.test.ts +16 -9
  78. package/src/monorepo/commit-msg.test.ts +45 -0
  79. package/src/monorepo/commit-msg.ts +107 -7
  80. package/src/monorepo/index.ts +142 -16
  81. package/src/monorepo/managed-files.ts +1 -1
  82. package/src/monorepo/nx-sync.ts +9 -2
  83. package/src/monorepo/package-hygiene.test.ts +34 -0
  84. package/src/monorepo/package-hygiene.ts +26 -4
  85. package/src/monorepo/package-policy.test.ts +902 -0
  86. package/src/monorepo/package-policy.ts +1393 -7
  87. package/src/monorepo/packed-package.ts +89 -37
  88. package/src/monorepo/packs/index.test.ts +189 -0
  89. package/src/monorepo/packs/index.ts +311 -20
  90. package/src/monorepo/publish-workflow.ts +8 -16
  91. package/src/monorepo/setup-test-tracing.test.ts +120 -0
  92. package/src/monorepo/tool-validation.test.ts +261 -0
  93. package/src/monorepo/tool-validation.ts +462 -0
  94. package/src/nx/index.test.ts +64 -0
  95. package/src/nx/index.ts +119 -0
  96. package/src/release/__tests__/bootstrap-npm-packages.test.ts +127 -0
  97. package/src/release/__tests__/candidates.test.ts +217 -0
  98. package/src/release/__tests__/core-properties.test.ts +4 -4
  99. package/src/release/__tests__/core-scenarios.test.ts +15 -5
  100. package/src/release/__tests__/core.test.ts +2 -2
  101. package/src/release/__tests__/fixture-repo.test.ts +22 -13
  102. package/src/release/__tests__/github-release.test.ts +13 -8
  103. package/src/release/__tests__/helpers/fixture-repo.ts +1 -0
  104. package/src/release/__tests__/npm-auth.test.ts +12 -21
  105. package/src/release/__tests__/orchestration.test.ts +51 -7
  106. package/src/release/__tests__/publish-plan.test.ts +9 -3
  107. package/src/release/__tests__/retag-unpublished.test.ts +1 -1
  108. package/src/release/__tests__/trust-publisher.test.ts +133 -0
  109. package/src/release/bootstrap-npm-packages.ts +88 -0
  110. package/src/release/candidates.ts +225 -0
  111. package/src/release/core.ts +1 -0
  112. package/src/release/github-release.ts +2 -2
  113. package/src/release/index.ts +346 -56
  114. package/src/release/npm-auth.ts +4 -33
  115. package/src/release/orchestration.ts +12 -2
  116. package/src/release/publish-plan.ts +1 -1
  117. package/managed/raw/.git-format-staged.yml +0 -47
@@ -1,6 +1,8 @@
1
- import { join } from 'node:path';
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { join, relative } from 'node:path';
2
3
  import {
3
4
  getOrCreateRecord,
5
+ hasOwn,
4
6
  hasOwnString,
5
7
  isRecord,
6
8
  readJsonObject,
@@ -21,8 +23,92 @@ import {
21
23
  workspaceDependencyFields,
22
24
  } from '../lib/workspace.js';
23
25
 
26
+ export interface WorkspaceDependencyDefaultOptions {
27
+ resolvedTargetsByProject?: ReadonlyMap<string, ReadonlySet<string> | ResolvedProjectTargets>;
28
+ }
29
+
30
+ export interface ResolvedProjectTargets {
31
+ targets: ReadonlySet<string>;
32
+ buildDependsOn?: readonly string[];
33
+ }
34
+
24
35
  export const SMOO_NX_VERSION_ACTIONS = '@smoothbricks/cli/nx-version-actions';
25
36
  export const SMOO_NX_RELEASE_TAG_PATTERN = '{projectName}@{version}';
37
+ const extraCommitScopes = ['release'];
38
+ const rootScriptPolicy: Record<string, string> = {
39
+ lint: 'nx run-many -t lint',
40
+ 'lint:fix': 'git-format-staged --config tooling/git-hooks/git-format-staged.yml --unstaged',
41
+ 'format:staged': 'git-format-staged --config tooling/git-hooks/git-format-staged.yml',
42
+ 'format:changed': 'git-format-staged --config tooling/git-hooks/git-format-staged.yml --also-unstaged',
43
+ };
44
+ const nxJsTypescriptPlugin = '@nx/js/typescript';
45
+ const smoothBricksNxPlugin = '@smoothbricks/nx-plugin';
46
+ const expectedSharedGlobalsNamedInput = ['{workspaceRoot}/.github/workflows/ci.yml'];
47
+ const defaultProductionNamedInput = [
48
+ '{projectRoot}/src/**/*',
49
+ '{projectRoot}/package.json',
50
+ '!{projectRoot}/**/__tests__/**',
51
+ '!{projectRoot}/**/*.test.*',
52
+ '!{projectRoot}/**/*.spec.*',
53
+ ];
54
+ const impreciseProductionInputs = new Set(['default', '{projectRoot}/**/*', '{projectRoot}/**']);
55
+
56
+ export function applyFixableMonorepoDefaults(root: string): void {
57
+ applyRootScriptDefaults(root);
58
+ applyNxPluginDefaults(root);
59
+ applyNxProjectNameDefaults(root);
60
+ }
61
+
62
+ export function applyRootScriptDefaults(root: string): void {
63
+ const rootPackagePath = join(root, 'package.json');
64
+ const rootPackage = readJsonObject(rootPackagePath);
65
+ if (!rootPackage) {
66
+ return;
67
+ }
68
+ const scripts = getOrCreateRecord(rootPackage, 'scripts');
69
+ let changed = false;
70
+ for (const [name, command] of Object.entries(rootScriptPolicy)) {
71
+ changed = setStringProperty(scripts, name, command) || changed;
72
+ }
73
+ const nx = getOrCreateRecord(rootPackage, 'nx');
74
+ if (!Array.isArray(nx.includedScripts) || nx.includedScripts.length !== 0) {
75
+ nx.includedScripts = [];
76
+ changed = true;
77
+ }
78
+ changed = sortRecordInPlace(scripts) || changed;
79
+ if (changed) {
80
+ writeJsonObject(rootPackagePath, rootPackage);
81
+ console.log('updated package.json root smoo scripts');
82
+ } else {
83
+ console.log('unchanged package.json root smoo scripts');
84
+ }
85
+ }
86
+
87
+ export function applyNxPluginDefaults(root: string): void {
88
+ const nxJsonPath = join(root, 'nx.json');
89
+ const nxJson = readJsonObject(nxJsonPath);
90
+ if (!nxJson) {
91
+ return;
92
+ }
93
+ let changed = removeColonTargetDefaults(nxJson);
94
+ changed = applyBuildTargetDefault(nxJson) || changed;
95
+ changed = applyNamedInputDefaults(nxJson) || changed;
96
+ const currentPlugins = Array.isArray(nxJson.plugins) ? nxJson.plugins : [];
97
+ const nextPlugins = upsertNxPlugin(
98
+ upsertNxPlugin(currentPlugins, expectedNxJsTypescriptPlugin()),
99
+ smoothBricksNxPlugin,
100
+ );
101
+ if (JSON.stringify(currentPlugins) !== JSON.stringify(nextPlugins)) {
102
+ nxJson.plugins = nextPlugins;
103
+ changed = true;
104
+ }
105
+ if (changed) {
106
+ writeJsonObject(nxJsonPath, nxJson);
107
+ console.log('updated nx.json smoo plugin config');
108
+ } else {
109
+ console.log('unchanged nx.json smoo plugin config');
110
+ }
111
+ }
26
112
 
27
113
  export function applyPublicPackageDefaults(root: string): void {
28
114
  const rootPackage = requiredJsonObject(join(root, 'package.json'));
@@ -65,16 +151,26 @@ export function applyPublicPackageDefaults(root: string): void {
65
151
  }
66
152
  }
67
153
 
68
- export function applyWorkspaceDependencyDefaults(root: string): void {
154
+ export function applyWorkspaceDependencyDefaults(root: string, options: WorkspaceDependencyDefaultOptions = {}): void {
69
155
  const workspaceNames = new Set(getWorkspacePackages(root).map((pkg) => pkg.name));
70
156
  for (const pkg of listPackageJsonRecords(root)) {
71
- const changed = fixWorkspaceDependencyRanges(pkg.json, workspaceNames);
157
+ let changed = fixWorkspaceDependencyRanges(pkg.json, workspaceNames);
158
+ const projectName = packageNxProjectName(pkg.json);
159
+ const resolvedProject = projectName ? options.resolvedTargetsByProject?.get(projectName) : undefined;
160
+ const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
161
+ changed = migratePackageColonTargets(pkg.json, resolvedTargets) || changed;
162
+ changed = rewriteColonTargetDependenciesInPackage(pkg.json, resolvedTargets) || changed;
163
+ changed = removePackageColonTargets(pkg.json) || changed;
164
+ changed = removeRedundantNoopBuildTarget(pkg.json, resolvedProject) || changed;
165
+ changed = applyPackageScriptPolicy(pkg.json, pkg.path, workspaceNames, { resolvedTargets }) || changed;
72
166
  if (changed) {
73
167
  writeJsonObject(pkg.packageJsonPath, pkg.json);
74
- console.log(`updated ${pkg.path}/package.json workspace dependency ranges`);
168
+ console.log(`updated ${pkg.path}/package.json workspace dependency policy`);
75
169
  } else {
76
- console.log(`unchanged ${pkg.path}/package.json workspace dependency ranges`);
170
+ console.log(`unchanged ${pkg.path}/package.json workspace dependency policy`);
77
171
  }
172
+ applyBunTestTsconfigDefaults(root, pkg.path, pkg.json, workspaceNames);
173
+ applyTsconfigTestReferenceDefaults(root, pkg.path);
78
174
  }
79
175
  }
80
176
 
@@ -117,6 +213,48 @@ export function applyNxReleaseDefaults(root: string): void {
117
213
  }
118
214
  }
119
215
 
216
+ export function applyNxProjectNameDefaults(root: string): void {
217
+ const rootPackage = requiredJsonObject(join(root, 'package.json'));
218
+ const rootName = stringProperty(rootPackage, 'name');
219
+ if (!rootName) {
220
+ return;
221
+ }
222
+ for (const pkg of getWorkspacePackages(root)) {
223
+ const suggestedName = suggestNxProjectName(rootName, pkg.name);
224
+ if (!suggestedName) {
225
+ continue;
226
+ }
227
+ const changed = applyPackageNxConfig(pkg.json, { projectName: suggestedName }).changed;
228
+ if (changed) {
229
+ writeJsonObject(pkg.packageJsonPath, pkg.json);
230
+ console.log(`updated ${pkg.path}/package.json nx.name`);
231
+ } else {
232
+ console.log(`unchanged ${pkg.path}/package.json nx.name`);
233
+ }
234
+ }
235
+ }
236
+
237
+ export function listValidCommitScopes(root: string): ReadonlySet<string> {
238
+ return new Set([...listNxProjectNames(root), ...extraCommitScopes]);
239
+ }
240
+
241
+ export function listNxProjectNames(root: string): string[] {
242
+ const rootPackage = readJsonObject(join(root, 'package.json'));
243
+ const rootName = rootPackage ? stringProperty(rootPackage, 'name') : null;
244
+ const names: string[] = [];
245
+ for (const pkg of getWorkspacePackages(root)) {
246
+ const nx = recordProperty(pkg.json, 'nx');
247
+ const configuredName = nx ? stringProperty(nx, 'name') : null;
248
+ const suggestedName = rootName ? suggestNxProjectName(rootName, pkg.name) : null;
249
+ if (configuredName) {
250
+ names.push(configuredName);
251
+ } else if (suggestedName) {
252
+ names.push(suggestedName);
253
+ }
254
+ }
255
+ return names;
256
+ }
257
+
120
258
  export function validateRootPackagePolicy(root: string): number {
121
259
  const rootPackage = readJsonObject(join(root, 'package.json'));
122
260
  if (!rootPackage) {
@@ -136,6 +274,8 @@ export function validateRootPackagePolicy(root: string): number {
136
274
  console.error('package.json must define repository.url');
137
275
  failures++;
138
276
  }
277
+ failures += validateRootScripts(rootPackage);
278
+ failures += validateRootNxScriptInference(rootPackage);
139
279
  const packageManager = stringProperty(rootPackage, 'packageManager');
140
280
  if (!packageManager?.startsWith('bun@')) {
141
281
  console.error('package.json packageManager must use bun@<version>');
@@ -172,6 +312,7 @@ export function validateNxReleaseConfig(root: string): number {
172
312
  console.error('nx.json release config is missing');
173
313
  failures++;
174
314
  }
315
+ failures += validateNxPluginConfig(nxJson);
175
316
  if (release && stringProperty(release, 'projectsRelationship') !== 'independent') {
176
317
  console.error('nx.json release.projectsRelationship must be independent');
177
318
  failures++;
@@ -239,6 +380,33 @@ export function validateNxReleaseConfig(root: string): number {
239
380
  return failures;
240
381
  }
241
382
 
383
+ export function validateNxProjectNames(root: string): number {
384
+ const rootPackage = readJsonObject(join(root, 'package.json'));
385
+ const rootName = rootPackage ? stringProperty(rootPackage, 'name') : null;
386
+ if (!rootName) {
387
+ return 0;
388
+ }
389
+ let failures = 0;
390
+ for (const pkg of getWorkspacePackages(root)) {
391
+ const suggestedName = suggestNxProjectName(rootName, pkg.name);
392
+ if (!suggestedName) {
393
+ continue;
394
+ }
395
+ const nx = recordProperty(pkg.json, 'nx');
396
+ const configuredName = nx ? stringProperty(nx, 'name') : null;
397
+ if (configuredName !== suggestedName) {
398
+ console.error(
399
+ `${pkg.path}: package.json nx.name must be "${suggestedName}" so fix(${suggestedName}): maps to this project`,
400
+ );
401
+ failures++;
402
+ }
403
+ }
404
+ if (failures === 0) {
405
+ console.log('Nx project names are valid.');
406
+ }
407
+ return failures;
408
+ }
409
+
242
410
  export function validatePublicTags(root: string): number {
243
411
  let failures = 0;
244
412
  for (const pkg of getWorkspacePackages(root)) {
@@ -315,10 +483,19 @@ export function validatePublicPackageMetadata(root: string): number {
315
483
  return failures;
316
484
  }
317
485
 
318
- export function validateWorkspaceDependencies(root: string): number {
486
+ export function validateWorkspaceDependencies(root: string, options: WorkspaceDependencyDefaultOptions = {}): number {
319
487
  const workspaceNames = new Set(getWorkspacePackages(root).map((pkg) => pkg.name));
320
488
  let failures = 0;
321
489
  for (const pkg of listPackageJsonRecords(root)) {
490
+ const projectName = packageNxProjectName(pkg.json);
491
+ const resolvedTargets = resolvedProjectTargetNames(
492
+ projectName ? options.resolvedTargetsByProject?.get(projectName) : undefined,
493
+ );
494
+ failures += validateExplicitNxTargets(pkg.json, pkg.path, resolvedTargets);
495
+ failures += validateBunTestTsconfigPresence(root, pkg.path, pkg.json);
496
+ failures += validateTsconfigTestPolicy(root, pkg.path);
497
+ failures += validateTsconfigTestReferencePolicy(root, pkg.path);
498
+ failures += validateBuildZigPolicy(root, pkg.path);
322
499
  for (const field of workspaceDependencyFields) {
323
500
  const dependencies = recordProperty(pkg.json, field);
324
501
  if (!dependencies) {
@@ -331,9 +508,170 @@ export function validateWorkspaceDependencies(root: string): number {
331
508
  }
332
509
  }
333
510
  }
511
+ failures += validatePackageScriptPolicy(pkg.json, pkg.path, workspaceNames, { resolvedTargets });
334
512
  }
335
513
  if (failures === 0) {
336
- console.log('Workspace dependency ranges are valid.');
514
+ console.log('Workspace dependency policy is valid.');
515
+ }
516
+ return failures;
517
+ }
518
+
519
+ export function applyPackageScriptPolicy(
520
+ pkg: Record<string, unknown>,
521
+ _packagePath: string,
522
+ workspaceNames: ReadonlySet<string>,
523
+ options: { resolvedTargets?: ReadonlySet<string> } = {},
524
+ ): boolean {
525
+ if (!hasWorkspaceDependency(pkg, workspaceNames)) {
526
+ return false;
527
+ }
528
+ const scripts = recordProperty(pkg, 'scripts');
529
+ if (!scripts) {
530
+ return false;
531
+ }
532
+ const projectName = packageNxProjectName(pkg);
533
+ if (!projectName) {
534
+ return false;
535
+ }
536
+ const nxConfig = applyPackageNxConfig(pkg, { projectName, targets: true });
537
+ const targets = nxConfig.targets;
538
+ let changed = nxConfig.changed;
539
+ for (const [scriptName, rawCommand] of Object.entries(scripts)) {
540
+ if (typeof rawCommand !== 'string') {
541
+ continue;
542
+ }
543
+ const rewrite = classifyScriptRewrite(scriptName, rawCommand);
544
+ if (!rewrite) {
545
+ continue;
546
+ }
547
+ const targetName = rewrite.targetName;
548
+ const alias = nxRunAlias(projectName, targetName, rewrite.continuous);
549
+ const existingTarget = recordProperty(targets, targetName);
550
+ if (
551
+ !existingTarget &&
552
+ targetName !== scriptName &&
553
+ targetExistsInResolvedProject(targetName, options.resolvedTargets)
554
+ ) {
555
+ if (scripts[scriptName] !== alias) {
556
+ scripts[scriptName] = alias;
557
+ changed = true;
558
+ }
559
+ continue;
560
+ }
561
+ const existingOptions = existingTarget ? recordProperty(existingTarget, 'options') : null;
562
+ const existingCommand = existingOptions ? stringProperty(existingOptions, 'command') : null;
563
+ const command = isScriptRunnerCommand(existingCommand, scriptName)
564
+ ? rewrite.command
565
+ : (existingCommand ?? rewrite.command);
566
+ const target = existingTarget ?? {};
567
+ changed = setStringProperty(target, 'executor', 'nx:run-commands') || changed;
568
+ changed = setStringArrayProperty(target, 'dependsOn', expectedTargetDependencies(targetName)) || changed;
569
+ if (rewrite.continuous && target.continuous !== true) {
570
+ target.continuous = true;
571
+ changed = true;
572
+ }
573
+ const targetOptions = getOrCreateRecord(target, 'options');
574
+ changed = setStringProperty(targetOptions, 'command', command) || changed;
575
+ changed = setStringProperty(targetOptions, 'cwd', '{projectRoot}') || changed;
576
+ for (const [name, value] of Object.entries(rewrite.env)) {
577
+ const env = getOrCreateRecord(targetOptions, 'env');
578
+ changed = setStringProperty(env, name, value) || changed;
579
+ }
580
+ if (targets[targetName] !== target) {
581
+ targets[targetName] = target;
582
+ changed = true;
583
+ }
584
+ if (scripts[scriptName] !== alias) {
585
+ scripts[scriptName] = alias;
586
+ changed = true;
587
+ }
588
+ }
589
+ return changed;
590
+ }
591
+
592
+ export function validatePackageScriptPolicy(
593
+ pkg: Record<string, unknown>,
594
+ packagePath: string,
595
+ workspaceNames: ReadonlySet<string>,
596
+ options: { resolvedTargets?: ReadonlySet<string> } = {},
597
+ ): number {
598
+ if (!hasWorkspaceDependency(pkg, workspaceNames)) {
599
+ return 0;
600
+ }
601
+ const scripts = recordProperty(pkg, 'scripts');
602
+ if (!scripts) {
603
+ return 0;
604
+ }
605
+ const nx = recordProperty(pkg, 'nx');
606
+ const projectName = nx ? stringProperty(nx, 'name') : stringProperty(pkg, 'name');
607
+ const targets = nx ? recordProperty(nx, 'targets') : null;
608
+ let failures = 0;
609
+ for (const [scriptName, rawCommand] of Object.entries(scripts)) {
610
+ if (typeof rawCommand !== 'string') {
611
+ continue;
612
+ }
613
+ const alias = parseNxRunAlias(rawCommand);
614
+ const rewrite = alias
615
+ ? { targetName: alias.targetName, continuous: isContinuousTarget(alias.targetName, '') }
616
+ : classifyScriptRewrite(scriptName, rawCommand);
617
+ if (!rewrite || (alias && projectName && alias.projectName !== projectName)) {
618
+ if (alias && projectName && alias.projectName !== projectName) {
619
+ console.error(`${packagePath}: scripts.${scriptName} must delegate to project ${projectName}`);
620
+ failures++;
621
+ }
622
+ continue;
623
+ }
624
+ if (!projectName) {
625
+ console.error(`${packagePath}: package scripts that use workspace dependencies require package.json nx.name`);
626
+ failures++;
627
+ continue;
628
+ }
629
+ const expectedAlias = nxRunAlias(projectName, rewrite.targetName, rewrite.continuous);
630
+ if (rawCommand !== expectedAlias) {
631
+ console.error(`${packagePath}: scripts.${scriptName} must delegate to ${expectedAlias}`);
632
+ failures++;
633
+ continue;
634
+ }
635
+ const target = targets ? recordProperty(targets, rewrite.targetName) : null;
636
+ if (
637
+ !target &&
638
+ rewrite.targetName !== scriptName &&
639
+ targetExistsInResolvedProject(rewrite.targetName, options.resolvedTargets)
640
+ ) {
641
+ continue;
642
+ }
643
+ if (rewrite.targetName.includes(':')) {
644
+ continue;
645
+ }
646
+ const targetOptions = target ? recordProperty(target, 'options') : null;
647
+ const command = targetOptions ? stringProperty(targetOptions, 'command') : null;
648
+ if (!target || stringProperty(target, 'executor') !== 'nx:run-commands' || !targetOptions || !command) {
649
+ console.error(`${packagePath}: nx.targets.${rewrite.targetName} must use nx:run-commands with options.command`);
650
+ failures++;
651
+ continue;
652
+ }
653
+ if (stringProperty(targetOptions, 'cwd') !== '{projectRoot}') {
654
+ console.error(`${packagePath}: nx.targets.${rewrite.targetName}.options.cwd must be {projectRoot}`);
655
+ failures++;
656
+ }
657
+ if (!targetDependsOn(target, expectedTargetDependencies(rewrite.targetName))) {
658
+ console.error(
659
+ `${packagePath}: nx.targets.${rewrite.targetName}.dependsOn must include ${expectedTargetDependencies(
660
+ rewrite.targetName,
661
+ ).join(', ')}`,
662
+ );
663
+ failures++;
664
+ }
665
+ if (rewrite.continuous && target.continuous !== true) {
666
+ console.error(`${packagePath}: nx.targets.${rewrite.targetName}.continuous must be true`);
667
+ failures++;
668
+ }
669
+ if (isScriptRunnerCommand(command, scriptName)) {
670
+ console.error(
671
+ `${packagePath}: nx.targets.${rewrite.targetName}.options.command must not call scripts.${scriptName}`,
672
+ );
673
+ failures++;
674
+ }
337
675
  }
338
676
  return failures;
339
677
  }
@@ -355,6 +693,1054 @@ function fixWorkspaceDependencyRanges(pkg: Record<string, unknown>, workspaceNam
355
693
  return changed;
356
694
  }
357
695
 
696
+ function validateRootScripts(rootPackage: Record<string, unknown>): number {
697
+ const scripts = recordProperty(rootPackage, 'scripts');
698
+ let failures = 0;
699
+ for (const [name, command] of Object.entries(rootScriptPolicy)) {
700
+ if (scripts?.[name] !== command) {
701
+ console.error(`package.json scripts.${name} must be ${command}`);
702
+ failures++;
703
+ }
704
+ }
705
+ if (scripts && !recordKeysAreSorted(scripts)) {
706
+ console.error(
707
+ 'package.json scripts must be sorted alphabetically so root command policy stays stable across fixes.',
708
+ );
709
+ failures++;
710
+ }
711
+ return failures;
712
+ }
713
+
714
+ function validateRootNxScriptInference(rootPackage: Record<string, unknown>): number {
715
+ const nx = recordProperty(rootPackage, 'nx');
716
+ if (nx && Array.isArray(nx.includedScripts) && nx.includedScripts.length === 0) {
717
+ return 0;
718
+ }
719
+ console.error('package.json nx.includedScripts must be [] so root scripts do not become recursive Nx targets.');
720
+ return 1;
721
+ }
722
+
723
+ function validateNxPluginConfig(nxJson: Record<string, unknown>): number {
724
+ let failures = 0;
725
+ const targetDefaults = recordProperty(nxJson, 'targetDefaults');
726
+ if (targetDefaults) {
727
+ for (const targetName of Object.keys(targetDefaults)) {
728
+ if (targetName.includes(':')) {
729
+ console.error(
730
+ `nx.json targetDefaults.${targetName} must not use colon target names. ` +
731
+ 'Nx CLI syntax already uses project:target:configuration, so smoo Nx target names must be unambiguous tool-output names.',
732
+ );
733
+ failures++;
734
+ }
735
+ }
736
+ }
737
+ failures += validateBuildTargetDefault(nxJson);
738
+ failures += validateNamedInputDefaults(nxJson);
739
+ const plugins = Array.isArray(nxJson.plugins) ? nxJson.plugins : [];
740
+ const nxJsPlugin = plugins.find(isNxJsTypescriptPlugin);
741
+ if (!nxJsPlugin) {
742
+ console.error(
743
+ `nx.json plugins must configure ${nxJsTypescriptPlugin}. ` +
744
+ 'Official Nx owns TypeScript library inference; smoo configures it so tsconfig.lib.json produces tsc-js and leaves build available as an aggregate target.',
745
+ );
746
+ failures++;
747
+ } else if (nxJsBuildTargetName(nxJsPlugin) !== 'tsc-js') {
748
+ console.error(
749
+ `nx.json ${nxJsTypescriptPlugin} build.targetName must be tsc-js. ` +
750
+ 'TypeScript library output is a concrete tool-output target; build is reserved for aggregate targets that depend on concrete build work.',
751
+ );
752
+ failures++;
753
+ }
754
+ if (!plugins.includes(smoothBricksNxPlugin) && !plugins.some(isSmoothBricksNxPluginRecord)) {
755
+ console.error(
756
+ `nx.json plugins must include ${smoothBricksNxPlugin}. ` +
757
+ 'Smoo relies on this plugin to infer convention targets that official Nx does not provide, including typecheck-tests, non-TypeScript build-tool targets, and aggregate build/lint targets.',
758
+ );
759
+ failures++;
760
+ }
761
+ return failures;
762
+ }
763
+
764
+ function validateExplicitNxTargets(
765
+ pkg: Record<string, unknown>,
766
+ packagePath: string,
767
+ resolvedTargets?: ReadonlySet<string>,
768
+ ): number {
769
+ const nx = recordProperty(pkg, 'nx');
770
+ const targets = nx ? recordProperty(nx, 'targets') : null;
771
+ if (!targets) {
772
+ return 0;
773
+ }
774
+ let failures = 0;
775
+ for (const [targetName, rawTarget] of Object.entries(targets)) {
776
+ if (targetName.includes(':')) {
777
+ console.error(
778
+ `${packagePath}: package.json nx.targets.${targetName} must not use colon target names. ` +
779
+ 'Nx CLI syntax already uses project:target:configuration; use a concrete tool-output target name and keep colon names only as package-script aliases.',
780
+ );
781
+ failures++;
782
+ }
783
+ if (!isRecord(rawTarget)) {
784
+ continue;
785
+ }
786
+ failures += validateTargetDependencies(rawTarget, `${packagePath}: nx.targets.${targetName}`, resolvedTargets);
787
+ }
788
+ return failures;
789
+ }
790
+
791
+ function validateTargetDependencies(
792
+ target: Record<string, unknown>,
793
+ label: string,
794
+ resolvedTargets?: ReadonlySet<string>,
795
+ ): number {
796
+ if (!Array.isArray(target.dependsOn)) {
797
+ return 0;
798
+ }
799
+ let failures = 0;
800
+ for (const dependency of target.dependsOn) {
801
+ if (typeof dependency !== 'string') {
802
+ continue;
803
+ }
804
+ if (dependency.includes(':')) {
805
+ console.error(`${label}.dependsOn must not include colon target dependency ${dependency}`);
806
+ failures++;
807
+ continue;
808
+ }
809
+ if (
810
+ label.endsWith('nx.targets.build') &&
811
+ !dependency.startsWith('^') &&
812
+ !targetExistsInResolvedProject(dependency, resolvedTargets)
813
+ ) {
814
+ console.error(`${label}.dependsOn references missing target ${dependency}`);
815
+ failures++;
816
+ }
817
+ }
818
+ return failures;
819
+ }
820
+
821
+ function validateTsconfigTestPolicy(root: string, packagePath: string): number {
822
+ const path = join(root, packagePath, 'tsconfig.test.json');
823
+ const tsconfig = readJsonObject(path);
824
+ if (!tsconfig) {
825
+ return 0;
826
+ }
827
+ const compilerOptions = recordProperty(tsconfig, 'compilerOptions');
828
+ let failures = 0;
829
+ if (!compilerOptions || compilerOptions.noEmit !== true) {
830
+ console.error(`${packagePath}/tsconfig.test.json compilerOptions.noEmit must be true`);
831
+ failures++;
832
+ }
833
+ if (compilerOptions?.composite === true) {
834
+ console.error(
835
+ `${packagePath}/tsconfig.test.json must not set compilerOptions.composite = true. ` +
836
+ 'Bun test typechecking is a no-emit validation pass, not a TypeScript build-mode project.',
837
+ );
838
+ failures++;
839
+ }
840
+ if (compilerOptions?.declaration === true) {
841
+ console.error(`${packagePath}/tsconfig.test.json must not set compilerOptions.declaration = true`);
842
+ failures++;
843
+ }
844
+ if (compilerOptions?.declarationMap === true) {
845
+ console.error(`${packagePath}/tsconfig.test.json must not set compilerOptions.declarationMap = true`);
846
+ failures++;
847
+ }
848
+ if (compilerOptions?.outDir === 'dist-test') {
849
+ console.error(`${packagePath}/tsconfig.test.json must not emit to dist-test`);
850
+ failures++;
851
+ }
852
+ if (typeof compilerOptions?.tsBuildInfoFile === 'string' && compilerOptions.tsBuildInfoFile.includes('dist-test')) {
853
+ console.error(`${packagePath}/tsconfig.test.json must not write tsbuildinfo under dist-test`);
854
+ failures++;
855
+ }
856
+ return failures;
857
+ }
858
+
859
+ function validateTsconfigTestReferencePolicy(root: string, packagePath: string): number {
860
+ const testTsconfigPath = join(root, packagePath, 'tsconfig.test.json');
861
+ if (!existsSync(testTsconfigPath)) {
862
+ return 0;
863
+ }
864
+ const projectTsconfig = readJsonObject(join(root, packagePath, 'tsconfig.json'));
865
+ if (!projectTsconfigHasTestReference(projectTsconfig)) {
866
+ return 0;
867
+ }
868
+ console.error(
869
+ `${packagePath}/tsconfig.json must not reference ./tsconfig.test.json. ` +
870
+ 'Test typechecking is run by the inferred typecheck-tests target with tsc --noEmit, not TypeScript build mode.',
871
+ );
872
+ return 1;
873
+ }
874
+
875
+ function validateBunTestTsconfigPresence(root: string, packagePath: string, pkg: Record<string, unknown>): number {
876
+ if (!usesBunTest(pkg)) {
877
+ return 0;
878
+ }
879
+ const path = join(root, packagePath, 'tsconfig.test.json');
880
+ if (existsSync(path)) {
881
+ return 0;
882
+ }
883
+ console.error(
884
+ `${packagePath}: bun test requires tsconfig.test.json because Bun executes tests without typechecking. ` +
885
+ 'Run smoo monorepo validate --fix to create the no-emit test typecheck config.',
886
+ );
887
+ return 1;
888
+ }
889
+
890
+ function applyBunTestTsconfigDefaults(
891
+ root: string,
892
+ packagePath: string,
893
+ pkg: Record<string, unknown>,
894
+ workspaceNames: ReadonlySet<string>,
895
+ ): void {
896
+ if (!usesBunTest(pkg)) {
897
+ return;
898
+ }
899
+ const tsconfigTestPath = join(root, packagePath, 'tsconfig.test.json');
900
+ const existing = readJsonObject(tsconfigTestPath);
901
+ const tsconfigTest = existing ?? {};
902
+ let changed = existing === null;
903
+
904
+ changed = applyTsconfigTestDefaults(root, packagePath, pkg, tsconfigTest, workspaceNames) || changed;
905
+ if (changed) {
906
+ writeJsonObject(tsconfigTestPath, tsconfigTest);
907
+ console.log(`updated ${packagePath}/tsconfig.test.json bun test typecheck config`);
908
+ } else {
909
+ console.log(`unchanged ${packagePath}/tsconfig.test.json bun test typecheck config`);
910
+ }
911
+ }
912
+
913
+ function applyTsconfigTestReferenceDefaults(root: string, packagePath: string): void {
914
+ const projectTsconfigPath = join(root, packagePath, 'tsconfig.json');
915
+ const projectTsconfig = readJsonObject(projectTsconfigPath);
916
+ if (!projectTsconfig || !projectTsconfigHasTestReference(projectTsconfig)) {
917
+ return;
918
+ }
919
+ const references = Array.isArray(projectTsconfig.references) ? projectTsconfig.references : [];
920
+ projectTsconfig.references = references.filter((entry) => !isRecord(entry) || entry.path !== './tsconfig.test.json');
921
+ writeJsonObject(projectTsconfigPath, projectTsconfig);
922
+ console.log(`updated ${packagePath}/tsconfig.json removed test project reference`);
923
+ }
924
+
925
+ function applyTsconfigTestDefaults(
926
+ root: string,
927
+ packagePath: string,
928
+ pkg: Record<string, unknown>,
929
+ tsconfigTest: Record<string, unknown>,
930
+ workspaceNames: ReadonlySet<string>,
931
+ ): boolean {
932
+ let changed = setMissingStringProperty(tsconfigTest, 'extends', defaultTsconfigTestExtends(root, packagePath));
933
+ const compilerOptions = getOrCreateRecord(tsconfigTest, 'compilerOptions');
934
+ changed = copyLibCompilerOptions(root, packagePath, compilerOptions) || changed;
935
+ changed = setBooleanProperty(compilerOptions, 'composite', false) || changed;
936
+ changed = setBooleanProperty(compilerOptions, 'declaration', false) || changed;
937
+ changed = setBooleanProperty(compilerOptions, 'declarationMap', false) || changed;
938
+ changed = setBooleanProperty(compilerOptions, 'emitDeclarationOnly', false) || changed;
939
+ changed = setBooleanProperty(compilerOptions, 'noEmit', true) || changed;
940
+ changed = mergeStringListProperty(compilerOptions, 'types', ['bun']) || changed;
941
+ if (delete compilerOptions.outDir) {
942
+ changed = true;
943
+ }
944
+ if (delete compilerOptions.tsBuildInfoFile) {
945
+ changed = true;
946
+ }
947
+ changed =
948
+ mergeStringListProperty(tsconfigTest, 'include', [
949
+ 'src/**/*.test.ts',
950
+ 'src/**/*.spec.ts',
951
+ 'src/**/__tests__/**/*.ts',
952
+ 'src/**/__tests__/**/*.tsx',
953
+ 'src/test-suite-tracer.ts',
954
+ ]) || changed;
955
+ for (const referencePath of collectTsconfigTestReferencePaths(root, packagePath, pkg, workspaceNames)) {
956
+ changed = addTsconfigReference(tsconfigTest, referencePath) || changed;
957
+ }
958
+ return changed;
959
+ }
960
+
961
+ function defaultTsconfigTestExtends(root: string, packagePath: string): string {
962
+ const libTsconfig = readJsonObject(join(root, packagePath, 'tsconfig.lib.json'));
963
+ return stringProperty(libTsconfig ?? {}, 'extends') ?? '../../tsconfig.base.json';
964
+ }
965
+
966
+ function copyLibCompilerOptions(root: string, packagePath: string, target: Record<string, unknown>): boolean {
967
+ const libTsconfig = readJsonObject(join(root, packagePath, 'tsconfig.lib.json'));
968
+ const libCompilerOptions = libTsconfig ? recordProperty(libTsconfig, 'compilerOptions') : null;
969
+ if (!libCompilerOptions) {
970
+ return false;
971
+ }
972
+ let changed = false;
973
+ for (const key of ['baseUrl', 'module', 'moduleResolution', 'jsx', 'lib']) {
974
+ if (hasOwn(libCompilerOptions, key) && target[key] !== libCompilerOptions[key]) {
975
+ target[key] = libCompilerOptions[key];
976
+ changed = true;
977
+ }
978
+ }
979
+ return changed;
980
+ }
981
+
982
+ function collectTsconfigTestReferencePaths(
983
+ root: string,
984
+ packagePath: string,
985
+ pkg: Record<string, unknown>,
986
+ workspaceNames: ReadonlySet<string>,
987
+ ): string[] {
988
+ const paths = existsSync(join(root, packagePath, 'tsconfig.lib.json')) ? ['./tsconfig.lib.json'] : [];
989
+ const packagesByName = new Map(getWorkspacePackages(root).map((workspacePkg) => [workspacePkg.name, workspacePkg]));
990
+ for (const field of workspaceDependencyFields) {
991
+ const dependencies = recordProperty(pkg, field);
992
+ if (!dependencies) {
993
+ continue;
994
+ }
995
+ for (const dependencyName of Object.keys(dependencies)) {
996
+ if (!workspaceNames.has(dependencyName)) {
997
+ continue;
998
+ }
999
+ const dependencyPackage = packagesByName.get(dependencyName);
1000
+ if (!dependencyPackage) {
1001
+ continue;
1002
+ }
1003
+ const dependencyTsconfig = join(root, dependencyPackage.path, 'tsconfig.lib.json');
1004
+ if (!existsSync(dependencyTsconfig)) {
1005
+ continue;
1006
+ }
1007
+ const refPath = relative(join(root, packagePath), dependencyTsconfig).replaceAll('\\', '/');
1008
+ if (!paths.includes(refPath)) {
1009
+ paths.push(refPath);
1010
+ }
1011
+ }
1012
+ }
1013
+ return paths;
1014
+ }
1015
+
1016
+ function usesBunTest(pkg: Record<string, unknown>): boolean {
1017
+ const scripts = recordProperty(pkg, 'scripts');
1018
+ if (scripts && Object.values(scripts).some((command) => typeof command === 'string' && isBunTestCommand(command))) {
1019
+ return true;
1020
+ }
1021
+ const nx = recordProperty(pkg, 'nx');
1022
+ const targets = nx ? recordProperty(nx, 'targets') : null;
1023
+ if (!targets) {
1024
+ return false;
1025
+ }
1026
+ for (const target of Object.values(targets)) {
1027
+ if (!isRecord(target)) {
1028
+ continue;
1029
+ }
1030
+ const options = recordProperty(target, 'options');
1031
+ const command = options ? stringProperty(options, 'command') : null;
1032
+ if (command && isBunTestCommand(command)) {
1033
+ return true;
1034
+ }
1035
+ }
1036
+ return false;
1037
+ }
1038
+
1039
+ function isBunTestCommand(command: string): boolean {
1040
+ return /^bun\s+test(?:\s|$)/.test(parseEnvPrefixedCommand(command).command.trim());
1041
+ }
1042
+
1043
+ function mergeStringListProperty(record: Record<string, unknown>, key: string, values: string[]): boolean {
1044
+ const rawCurrent = record[key];
1045
+ const current = Array.isArray(rawCurrent)
1046
+ ? rawCurrent.filter((entry): entry is string => typeof entry === 'string')
1047
+ : [];
1048
+ const next = [...current];
1049
+ for (const value of values) {
1050
+ if (!next.includes(value)) {
1051
+ next.push(value);
1052
+ }
1053
+ }
1054
+ if (
1055
+ Array.isArray(rawCurrent) &&
1056
+ next.length === rawCurrent.length &&
1057
+ next.every((entry, index) => entry === rawCurrent[index])
1058
+ ) {
1059
+ return false;
1060
+ }
1061
+ record[key] = next;
1062
+ return true;
1063
+ }
1064
+
1065
+ function addTsconfigReference(tsconfig: Record<string, unknown>, path: string): boolean {
1066
+ const current = Array.isArray(tsconfig.references)
1067
+ ? tsconfig.references.filter((entry): entry is Record<string, unknown> => isRecord(entry))
1068
+ : [];
1069
+ if (current.some((entry) => entry.path === path)) {
1070
+ return false;
1071
+ }
1072
+ tsconfig.references = [...current, { path }];
1073
+ return true;
1074
+ }
1075
+
1076
+ function projectTsconfigHasTestReference(projectTsconfig: Record<string, unknown> | null): boolean {
1077
+ return Boolean(
1078
+ projectTsconfig &&
1079
+ Array.isArray(projectTsconfig.references) &&
1080
+ projectTsconfig.references.some((entry) => isRecord(entry) && entry.path === './tsconfig.test.json'),
1081
+ );
1082
+ }
1083
+
1084
+ function validateBuildZigPolicy(root: string, packagePath: string): number {
1085
+ const path = join(root, packagePath, 'build.zig');
1086
+ if (!existsSync(path)) {
1087
+ return 0;
1088
+ }
1089
+ if (/\bb\.step\s*\(/.test(readFileSync(path, 'utf8'))) {
1090
+ return 0;
1091
+ }
1092
+ console.error(`${packagePath}/build.zig must define at least one b.step(...) target`);
1093
+ return 1;
1094
+ }
1095
+
1096
+ function applyBuildTargetDefault(nxJson: Record<string, unknown>): boolean {
1097
+ const targetDefaults = getOrCreateRecord(nxJson, 'targetDefaults');
1098
+ const build = getOrCreateRecord(targetDefaults, 'build');
1099
+ let changed = setBooleanProperty(build, 'cache', true);
1100
+ changed = setStringArrayProperty(build, 'outputs', ['{projectRoot}/dist']) || changed;
1101
+ return changed;
1102
+ }
1103
+
1104
+ function validateBuildTargetDefault(nxJson: Record<string, unknown>): number {
1105
+ const targetDefaults = recordProperty(nxJson, 'targetDefaults');
1106
+ const build = targetDefaults ? recordProperty(targetDefaults, 'build') : null;
1107
+ let failures = 0;
1108
+ if (!build || build.cache !== true) {
1109
+ console.error('nx.json targetDefaults.build.cache must be true');
1110
+ failures++;
1111
+ }
1112
+ const outputs = build?.outputs;
1113
+ if (!Array.isArray(outputs) || outputs.length !== 1 || outputs[0] !== '{projectRoot}/dist') {
1114
+ console.error('nx.json targetDefaults.build.outputs must be ["{projectRoot}/dist"]');
1115
+ failures++;
1116
+ }
1117
+ return failures;
1118
+ }
1119
+
1120
+ function applyNamedInputDefaults(nxJson: Record<string, unknown>): boolean {
1121
+ const namedInputs = getOrCreateRecord(nxJson, 'namedInputs');
1122
+ let changed = false;
1123
+ if (!Array.isArray(namedInputs.default)) {
1124
+ namedInputs.default = ['{projectRoot}/**/*', 'sharedGlobals'];
1125
+ changed = true;
1126
+ }
1127
+ changed = setStringArrayProperty(namedInputs, 'sharedGlobals', expectedSharedGlobalsNamedInput) || changed;
1128
+ const production = namedInputs.production;
1129
+ if (!Array.isArray(production) || !isPreciseProductionNamedInput(production)) {
1130
+ namedInputs.production = defaultProductionNamedInput;
1131
+ changed = true;
1132
+ }
1133
+ return changed;
1134
+ }
1135
+
1136
+ function validateNamedInputDefaults(nxJson: Record<string, unknown>): number {
1137
+ const namedInputs = recordProperty(nxJson, 'namedInputs');
1138
+ const production = namedInputs?.production;
1139
+ let failures = 0;
1140
+ if (!namedInputs) {
1141
+ console.error('nx.json namedInputs must be configured so production builds have precise cache inputs.');
1142
+ return 1;
1143
+ }
1144
+ if (!Array.isArray(namedInputs.default)) {
1145
+ console.error(
1146
+ 'nx.json namedInputs.default must be an array; smoo allows it to remain broad for non-production tasks.',
1147
+ );
1148
+ failures++;
1149
+ }
1150
+ if (!stringArrayEquals(namedInputs.sharedGlobals, expectedSharedGlobalsNamedInput)) {
1151
+ console.error('nx.json namedInputs.sharedGlobals must include only {workspaceRoot}/.github/workflows/ci.yml');
1152
+ failures++;
1153
+ }
1154
+ if (!Array.isArray(production)) {
1155
+ console.error('nx.json namedInputs.production must be an array of precise production inputs.');
1156
+ return failures + 1;
1157
+ }
1158
+ if (!isPreciseProductionNamedInput(production)) {
1159
+ console.error(
1160
+ 'nx.json namedInputs.production must enumerate precise production inputs. Do not include default or broad {projectRoot}/** globs; use language/tool-specific paths such as {projectRoot}/src/**/*, {projectRoot}/Cargo.toml, or {projectRoot}/pyproject.toml.',
1161
+ );
1162
+ failures++;
1163
+ }
1164
+ return failures;
1165
+ }
1166
+
1167
+ function isPreciseProductionNamedInput(production: unknown[]): boolean {
1168
+ let hasPositiveProjectInput = false;
1169
+ for (const input of production) {
1170
+ if (typeof input !== 'string') {
1171
+ return false;
1172
+ }
1173
+ const normalized = input.startsWith('!') ? input.slice(1) : input;
1174
+ if (impreciseProductionInputs.has(input) || impreciseProductionInputs.has(normalized)) {
1175
+ return false;
1176
+ }
1177
+ if (!input.startsWith('!') && normalized.startsWith('{projectRoot}/')) {
1178
+ hasPositiveProjectInput = true;
1179
+ }
1180
+ }
1181
+ return hasPositiveProjectInput;
1182
+ }
1183
+
1184
+ function removeColonTargetDefaults(nxJson: Record<string, unknown>): boolean {
1185
+ const targetDefaults = recordProperty(nxJson, 'targetDefaults');
1186
+ if (!targetDefaults) {
1187
+ return false;
1188
+ }
1189
+ let changed = false;
1190
+ for (const targetName of Object.keys(targetDefaults)) {
1191
+ if (targetName.includes(':')) {
1192
+ delete targetDefaults[targetName];
1193
+ changed = true;
1194
+ }
1195
+ }
1196
+ return changed;
1197
+ }
1198
+
1199
+ function rewriteColonTargetDependenciesInPackage(
1200
+ pkg: Record<string, unknown>,
1201
+ resolvedTargets?: ReadonlySet<string>,
1202
+ ): boolean {
1203
+ const nx = recordProperty(pkg, 'nx');
1204
+ const targets = nx ? recordProperty(nx, 'targets') : null;
1205
+ if (!targets) {
1206
+ return false;
1207
+ }
1208
+ const projectName = packageNxProjectName(pkg);
1209
+ const scriptTargetAliases = scriptTargetAliasesForProject(pkg, projectName);
1210
+ let changed = false;
1211
+ for (const target of Object.values(targets)) {
1212
+ if (!isRecord(target) || !Array.isArray(target.dependsOn)) {
1213
+ continue;
1214
+ }
1215
+ target.dependsOn = target.dependsOn.map((dependency) => {
1216
+ if (typeof dependency !== 'string' || !dependency.includes(':')) {
1217
+ return dependency;
1218
+ }
1219
+ const next = scriptTargetAliases.get(dependency) ?? replacementTargetName(dependency, null, resolvedTargets);
1220
+ if (!next) {
1221
+ return dependency;
1222
+ }
1223
+ changed = true;
1224
+ return next;
1225
+ });
1226
+ }
1227
+ return changed;
1228
+ }
1229
+
1230
+ function scriptTargetAliasesForProject(
1231
+ pkg: Record<string, unknown>,
1232
+ projectName: string | null,
1233
+ ): ReadonlyMap<string, string> {
1234
+ if (!projectName) {
1235
+ return new Map();
1236
+ }
1237
+ const scripts = recordProperty(pkg, 'scripts');
1238
+ if (!scripts) {
1239
+ return new Map();
1240
+ }
1241
+ const aliases = new Map<string, string>();
1242
+ for (const [scriptName, rawCommand] of Object.entries(scripts)) {
1243
+ if (typeof rawCommand !== 'string' || !scriptName.includes(':')) {
1244
+ continue;
1245
+ }
1246
+ const alias = parseNxRunAlias(rawCommand);
1247
+ if (alias?.projectName === projectName && !alias.targetName.includes(':')) {
1248
+ aliases.set(scriptName, alias.targetName);
1249
+ }
1250
+ }
1251
+ return aliases;
1252
+ }
1253
+
1254
+ function removePackageColonTargets(pkg: Record<string, unknown>): boolean {
1255
+ const nx = recordProperty(pkg, 'nx');
1256
+ const targets = nx ? recordProperty(nx, 'targets') : null;
1257
+ if (!targets) {
1258
+ return false;
1259
+ }
1260
+ let changed = false;
1261
+ for (const targetName of Object.keys(targets)) {
1262
+ if (targetName.includes(':')) {
1263
+ delete targets[targetName];
1264
+ changed = true;
1265
+ }
1266
+ }
1267
+ return changed;
1268
+ }
1269
+
1270
+ function resolvedProjectTargetNames(
1271
+ resolvedProject?: ReadonlySet<string> | ResolvedProjectTargets,
1272
+ ): ReadonlySet<string> | undefined {
1273
+ if (!resolvedProject) {
1274
+ return undefined;
1275
+ }
1276
+ return isResolvedProjectTargets(resolvedProject) ? resolvedProject.targets : resolvedProject;
1277
+ }
1278
+
1279
+ function resolvedProjectBuildDependsOn(
1280
+ resolvedProject?: ReadonlySet<string> | ResolvedProjectTargets,
1281
+ ): readonly string[] | undefined {
1282
+ if (!isResolvedProjectTargets(resolvedProject)) {
1283
+ return undefined;
1284
+ }
1285
+ return resolvedProject.buildDependsOn;
1286
+ }
1287
+
1288
+ function isResolvedProjectTargets(
1289
+ value: ReadonlySet<string> | ResolvedProjectTargets | undefined,
1290
+ ): value is ResolvedProjectTargets {
1291
+ return isRecord(value) && value.targets instanceof Set;
1292
+ }
1293
+
1294
+ function removeRedundantNoopBuildTarget(
1295
+ pkg: Record<string, unknown>,
1296
+ resolvedProject?: ReadonlySet<string> | ResolvedProjectTargets,
1297
+ ): boolean {
1298
+ const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
1299
+ const resolvedBuildDependsOn = resolvedProjectBuildDependsOn(resolvedProject);
1300
+ if (!resolvedTargets?.has('build') || !resolvedBuildDependsOn) {
1301
+ return false;
1302
+ }
1303
+ const nx = recordProperty(pkg, 'nx');
1304
+ const targets = nx ? recordProperty(nx, 'targets') : null;
1305
+ const build = targets ? recordProperty(targets, 'build') : null;
1306
+ if (
1307
+ !targets ||
1308
+ !build ||
1309
+ !isNoopTarget(build) ||
1310
+ !targetDependenciesMatchResolvedBuild(build, resolvedBuildDependsOn)
1311
+ ) {
1312
+ return false;
1313
+ }
1314
+ delete targets.build;
1315
+ return true;
1316
+ }
1317
+
1318
+ function isNoopTarget(target: Record<string, unknown>): boolean {
1319
+ const executor = stringProperty(target, 'executor');
1320
+ if (executor !== null && executor !== 'nx:noop') {
1321
+ return false;
1322
+ }
1323
+ const options = recordProperty(target, 'options');
1324
+ return !options || stringProperty(options, 'command') === null;
1325
+ }
1326
+
1327
+ function targetDependenciesMatchResolvedBuild(
1328
+ target: Record<string, unknown>,
1329
+ resolvedBuildDependsOn: readonly string[],
1330
+ ): boolean {
1331
+ if (!Array.isArray(target.dependsOn)) {
1332
+ return false;
1333
+ }
1334
+ const expected = new Set(resolvedBuildDependsOn);
1335
+ if (target.dependsOn.length !== expected.size) {
1336
+ return false;
1337
+ }
1338
+ return target.dependsOn.every((dependency) => {
1339
+ if (typeof dependency !== 'string') {
1340
+ return false;
1341
+ }
1342
+ return expected.has(dependency);
1343
+ });
1344
+ }
1345
+
1346
+ function migratePackageColonTargets(pkg: Record<string, unknown>, resolvedTargets?: ReadonlySet<string>): boolean {
1347
+ const nx = recordProperty(pkg, 'nx');
1348
+ const targets = nx ? recordProperty(nx, 'targets') : null;
1349
+ if (!targets) {
1350
+ return false;
1351
+ }
1352
+ const projectName = packageNxProjectName(pkg);
1353
+ const scripts = recordProperty(pkg, 'scripts');
1354
+ let changed = false;
1355
+ const renamedTargets = new Map<string, string>();
1356
+
1357
+ for (const [targetName, rawTarget] of Object.entries(targets)) {
1358
+ if (!targetName.includes(':') || !isRecord(rawTarget)) {
1359
+ continue;
1360
+ }
1361
+ const nextTargetName = replacementTargetName(targetName, targetCommand(rawTarget), resolvedTargets);
1362
+ if (!nextTargetName || nextTargetName.includes(':')) {
1363
+ continue;
1364
+ }
1365
+ if (!targetExistsInResolvedProject(nextTargetName, resolvedTargets)) {
1366
+ targets[nextTargetName] = rawTarget;
1367
+ }
1368
+ delete targets[targetName];
1369
+ renamedTargets.set(targetName, nextTargetName);
1370
+ changed = true;
1371
+ }
1372
+
1373
+ if (renamedTargets.size === 0) {
1374
+ return changed;
1375
+ }
1376
+
1377
+ for (const target of Object.values(targets)) {
1378
+ if (isRecord(target)) {
1379
+ changed = rewriteTargetDependencies(target, renamedTargets) || changed;
1380
+ }
1381
+ }
1382
+
1383
+ if (scripts && projectName) {
1384
+ for (const [scriptName, rawCommand] of Object.entries(scripts)) {
1385
+ if (typeof rawCommand !== 'string') {
1386
+ continue;
1387
+ }
1388
+ const alias = parseNxRunAlias(rawCommand);
1389
+ if (!alias || alias.projectName !== projectName) {
1390
+ continue;
1391
+ }
1392
+ const nextTargetName = renamedTargets.get(alias.targetName);
1393
+ if (!nextTargetName) {
1394
+ continue;
1395
+ }
1396
+ scripts[scriptName] = nxRunAlias(projectName, nextTargetName, isContinuousTarget(nextTargetName, ''));
1397
+ changed = true;
1398
+ }
1399
+ }
1400
+
1401
+ return changed;
1402
+ }
1403
+
1404
+ function rewriteTargetDependencies(
1405
+ target: Record<string, unknown>,
1406
+ renamedTargets: ReadonlyMap<string, string>,
1407
+ ): boolean {
1408
+ if (!Array.isArray(target.dependsOn)) {
1409
+ return false;
1410
+ }
1411
+ let changed = false;
1412
+ target.dependsOn = target.dependsOn.map((dependency) => {
1413
+ if (typeof dependency !== 'string') {
1414
+ return dependency;
1415
+ }
1416
+ const next = renamedTargets.get(dependency);
1417
+ if (!next) {
1418
+ return dependency;
1419
+ }
1420
+ changed = true;
1421
+ return next;
1422
+ });
1423
+ return changed;
1424
+ }
1425
+
1426
+ function targetCommand(target: Record<string, unknown>): string | null {
1427
+ const options = recordProperty(target, 'options');
1428
+ return options ? stringProperty(options, 'command') : null;
1429
+ }
1430
+
1431
+ function replacementTargetName(
1432
+ targetName: string,
1433
+ command: string | null,
1434
+ resolvedTargets?: ReadonlySet<string>,
1435
+ ): string | null {
1436
+ if (command) {
1437
+ const commandTargetName = targetNameForCommand(command);
1438
+ if (commandTargetName) {
1439
+ return commandTargetName;
1440
+ }
1441
+ }
1442
+ const suffix = targetName.slice(targetName.lastIndexOf(':') + 1);
1443
+ if (suffix && targetExistsInResolvedProject(suffix, resolvedTargets)) {
1444
+ return suffix;
1445
+ }
1446
+ const dashed = targetName.replaceAll(':', '-');
1447
+ if (targetExistsInResolvedProject(dashed, resolvedTargets)) {
1448
+ return dashed;
1449
+ }
1450
+ return null;
1451
+ }
1452
+
1453
+ function expectedNxJsTypescriptPlugin(): Record<string, unknown> {
1454
+ return {
1455
+ plugin: nxJsTypescriptPlugin,
1456
+ options: {
1457
+ typecheck: { targetName: 'typecheck' },
1458
+ build: {
1459
+ targetName: 'tsc-js',
1460
+ configName: 'tsconfig.lib.json',
1461
+ buildDepsName: 'build-deps',
1462
+ watchDepsName: 'watch-deps',
1463
+ },
1464
+ },
1465
+ };
1466
+ }
1467
+
1468
+ function upsertNxPlugin(plugins: readonly unknown[], plugin: string | Record<string, unknown>): unknown[] {
1469
+ const pluginName = typeof plugin === 'string' ? plugin : stringProperty(plugin, 'plugin');
1470
+ const next = plugins.filter((entry) => nxPluginName(entry) !== pluginName);
1471
+ next.push(plugin);
1472
+ return next;
1473
+ }
1474
+
1475
+ function nxPluginName(value: unknown): string | null {
1476
+ if (typeof value === 'string') {
1477
+ return value;
1478
+ }
1479
+ return isRecord(value) ? stringProperty(value, 'plugin') : null;
1480
+ }
1481
+
1482
+ function isNxJsTypescriptPlugin(value: unknown): value is Record<string, unknown> {
1483
+ return isRecord(value) && stringProperty(value, 'plugin') === nxJsTypescriptPlugin;
1484
+ }
1485
+
1486
+ function isSmoothBricksNxPluginRecord(value: unknown): boolean {
1487
+ return isRecord(value) && stringProperty(value, 'plugin') === smoothBricksNxPlugin;
1488
+ }
1489
+
1490
+ function nxJsBuildTargetName(plugin: Record<string, unknown>): string | null {
1491
+ const options = recordProperty(plugin, 'options');
1492
+ const build = options ? recordProperty(options, 'build') : null;
1493
+ return build ? stringProperty(build, 'targetName') : null;
1494
+ }
1495
+
1496
+ interface PackageNxConfig {
1497
+ nx: Record<string, unknown>;
1498
+ targets: Record<string, unknown> | null;
1499
+ changed: boolean;
1500
+ }
1501
+
1502
+ interface PackageNxTargetsConfig {
1503
+ nx: Record<string, unknown>;
1504
+ targets: Record<string, unknown>;
1505
+ changed: boolean;
1506
+ }
1507
+
1508
+ function applyPackageNxConfig(
1509
+ pkg: Record<string, unknown>,
1510
+ options: { projectName: string; targets: true },
1511
+ ): PackageNxTargetsConfig;
1512
+ function applyPackageNxConfig(pkg: Record<string, unknown>, options: { projectName: string }): PackageNxConfig;
1513
+ function applyPackageNxConfig(
1514
+ pkg: Record<string, unknown>,
1515
+ options: { projectName: string; targets?: boolean },
1516
+ ): PackageNxConfig {
1517
+ const nx = getOrCreateRecord(pkg, 'nx');
1518
+ const changed = setStringProperty(nx, 'name', options.projectName);
1519
+ const targets = options.targets === true ? getOrCreateRecord(nx, 'targets') : null;
1520
+ return { nx, targets, changed };
1521
+ }
1522
+
1523
+ function packageNxProjectName(pkg: Record<string, unknown>): string | null {
1524
+ const nx = recordProperty(pkg, 'nx');
1525
+ return (nx ? stringProperty(nx, 'name') : null) ?? stringProperty(pkg, 'name');
1526
+ }
1527
+
1528
+ interface ScriptRewrite {
1529
+ targetName: string;
1530
+ continuous: boolean;
1531
+ command: string;
1532
+ env: Record<string, string>;
1533
+ }
1534
+
1535
+ function classifyScriptRewrite(scriptName: string, command: string): ScriptRewrite | null {
1536
+ if (isNxRunAlias(command) || isBlockedScriptCommand(scriptName, command)) {
1537
+ return null;
1538
+ }
1539
+ const parsed = parseEnvPrefixedCommand(command);
1540
+ const targetName = rewriteTargetName(scriptName, parsed.command);
1541
+ if (!targetName || !isSafeNxScriptCommand(parsed.command)) {
1542
+ return null;
1543
+ }
1544
+ return {
1545
+ targetName,
1546
+ continuous: isContinuousTarget(targetName, parsed.command),
1547
+ command: parsed.command,
1548
+ env: parsed.env,
1549
+ };
1550
+ }
1551
+
1552
+ function rewriteTargetName(scriptName: string, command: string): string | null {
1553
+ return targetNameForCommand(command) ?? (scriptName.includes(':') ? null : scriptName);
1554
+ }
1555
+
1556
+ function targetNameForCommand(command: string): string | null {
1557
+ const trimmed = command.trim();
1558
+ if (/^tsc\s+--build\s+tsconfig\.lib\.json(?:\s|$)/.test(trimmed)) {
1559
+ return 'tsc-js';
1560
+ }
1561
+ const zigStep = /^zig\s+build\s+([A-Za-z0-9_-]+)(?:\s|$)/.exec(trimmed)?.[1];
1562
+ if (zigStep) {
1563
+ return `zig-${zigStep}`;
1564
+ }
1565
+ if (/^wrangler\s+build(?:\s|$)/.test(trimmed)) {
1566
+ return 'build';
1567
+ }
1568
+ return null;
1569
+ }
1570
+
1571
+ function nxRunAlias(projectName: string, targetName: string, continuous: boolean): string {
1572
+ const flags = continuous ? ' --tui=false --outputStyle=stream' : '';
1573
+ return `nx run ${projectName}:${targetName}${flags}`;
1574
+ }
1575
+
1576
+ function expectedTargetDependencies(targetName: string): string[] {
1577
+ return targetName === 'preview' ? ['build'] : ['^build'];
1578
+ }
1579
+
1580
+ function targetExistsInResolvedProject(targetName: string, resolvedTargets?: ReadonlySet<string>): boolean {
1581
+ return resolvedTargets?.has(targetName) === true;
1582
+ }
1583
+
1584
+ function setStringArrayProperty(record: Record<string, unknown>, key: string, value: string[]): boolean {
1585
+ const current = record[key];
1586
+ if (stringArrayEquals(current, value)) {
1587
+ return false;
1588
+ }
1589
+ record[key] = value;
1590
+ return true;
1591
+ }
1592
+
1593
+ function stringArrayEquals(value: unknown, expected: readonly string[]): boolean {
1594
+ return (
1595
+ Array.isArray(value) && value.length === expected.length && value.every((entry, index) => entry === expected[index])
1596
+ );
1597
+ }
1598
+
1599
+ function targetDependsOn(target: Record<string, unknown>, expected: string[]): boolean {
1600
+ const dependsOn = target.dependsOn;
1601
+ return Array.isArray(dependsOn) && expected.every((entry) => dependsOn.includes(entry));
1602
+ }
1603
+
1604
+ function hasWorkspaceDependency(pkg: Record<string, unknown>, workspaceNames: ReadonlySet<string>): boolean {
1605
+ for (const field of workspaceDependencyFields) {
1606
+ const dependencies = recordProperty(pkg, field);
1607
+ if (!dependencies) {
1608
+ continue;
1609
+ }
1610
+ for (const name of Object.keys(dependencies)) {
1611
+ if (workspaceNames.has(name)) {
1612
+ return true;
1613
+ }
1614
+ }
1615
+ }
1616
+ return false;
1617
+ }
1618
+
1619
+ function isSafeNxScriptCommand(command: string): boolean {
1620
+ const trimmed = command.trim();
1621
+ return (
1622
+ /^bun\s+test(?:\s|$)/.test(trimmed) ||
1623
+ /^tsc-bun-test(?:\s|$)/.test(trimmed) ||
1624
+ /^vitest(?:\s|$)/.test(trimmed) ||
1625
+ /^tsc\s+(?:--build|--noEmit)(?:\s|$)/.test(trimmed) ||
1626
+ /^tsdown(?:\s|$)/.test(trimmed) ||
1627
+ /^vite\s+(?:build|dev|preview)(?:\s|$)/.test(trimmed) ||
1628
+ /^astro\s+(?:build|dev|preview|check)(?:\s|$)/.test(trimmed) ||
1629
+ /^zig\s+build(?:\s|$)/.test(trimmed) ||
1630
+ /^bun\s+[./\w-]*build[\w.-]*\.ts(?:\s|$)/.test(trimmed) ||
1631
+ /(?:^|\s)(?:bench|benchmark)(?:\s|$)/.test(trimmed) ||
1632
+ /^wrangler\s+build(?:\s|$)/.test(trimmed)
1633
+ );
1634
+ }
1635
+
1636
+ function isBlockedScriptCommand(scriptName: string, command: string): boolean {
1637
+ if (/^(?:deploy|db|release|sync|subtree|publish|pack)(?::|$)/.test(scriptName)) {
1638
+ return true;
1639
+ }
1640
+ const trimmed = parseEnvPrefixedCommand(command).command;
1641
+ return /^(?:deploy|db|release|sync|subtree|publish|pack)(?:\s|$)/.test(trimmed) || trimmed === 'astro';
1642
+ }
1643
+
1644
+ function parseEnvPrefixedCommand(command: string): { command: string; env: Record<string, string> } {
1645
+ const env: Record<string, string> = {};
1646
+ let rest = command.trimStart();
1647
+ while (true) {
1648
+ const match = /^([A-Za-z_][A-Za-z0-9_]*)=/.exec(rest);
1649
+ if (!match?.[1]) {
1650
+ return { command: rest.trim(), env };
1651
+ }
1652
+ let index = match[0].length;
1653
+ let value = '';
1654
+ const quote = rest[index];
1655
+ if (quote === '"' || quote === "'") {
1656
+ index += 1;
1657
+ const end = rest.indexOf(quote, index);
1658
+ if (end === -1) {
1659
+ return { command: command.trim(), env: {} };
1660
+ }
1661
+ value = rest.slice(index, end);
1662
+ index = end + 1;
1663
+ } else {
1664
+ const end = rest.slice(index).search(/\s/);
1665
+ const valueEnd = end === -1 ? rest.length : index + end;
1666
+ value = rest.slice(index, valueEnd);
1667
+ index = valueEnd;
1668
+ }
1669
+ if (index < rest.length && !/\s/.test(rest[index] ?? '')) {
1670
+ return { command: command.trim(), env: {} };
1671
+ }
1672
+ env[match[1]] = value;
1673
+ rest = rest.slice(index).trimStart();
1674
+ }
1675
+ }
1676
+
1677
+ function isContinuousTarget(targetName: string, command: string): boolean {
1678
+ return (
1679
+ /(?:^|:|-)(?:dev|serve|preview|watch)(?:$|:|-)/.test(targetName) ||
1680
+ /(?:^|\s)(?:dev|serve|preview|--watch|-w)(?:\s|$)/.test(command)
1681
+ );
1682
+ }
1683
+
1684
+ function isNxRunAlias(command: string): boolean {
1685
+ return /^nx\s+run\s+\S+:\S+/.test(command.trim());
1686
+ }
1687
+
1688
+ function parseNxRunAlias(command: string): { projectName: string; targetName: string } | null {
1689
+ const match = /^nx\s+run\s+([^:\s]+):([^\s]+)(?:\s|$)/.exec(command.trim());
1690
+ if (!match?.[1] || !match[2]) {
1691
+ return null;
1692
+ }
1693
+ return { projectName: match[1], targetName: match[2] };
1694
+ }
1695
+
1696
+ function recordKeysAreSorted(record: Record<string, unknown>): boolean {
1697
+ const keys = Object.keys(record);
1698
+ return keys.every((key, index) => index === 0 || keys[index - 1] <= key);
1699
+ }
1700
+
1701
+ function sortRecordInPlace(record: Record<string, unknown>): boolean {
1702
+ if (recordKeysAreSorted(record)) {
1703
+ return false;
1704
+ }
1705
+ const entries = Object.entries(record).sort(([a], [b]) => a.localeCompare(b));
1706
+ for (const key of Object.keys(record)) {
1707
+ delete record[key];
1708
+ }
1709
+ for (const [key, value] of entries) {
1710
+ record[key] = value;
1711
+ }
1712
+ return true;
1713
+ }
1714
+
1715
+ function isScriptRunnerCommand(command: string | null, scriptName: string): boolean {
1716
+ if (!command) {
1717
+ return false;
1718
+ }
1719
+ const escaped = escapeRegex(scriptName);
1720
+ return new RegExp(`^(?:bun|npm)\\s+run\\s+${escaped}(?:\\s|$)`).test(command.trim());
1721
+ }
1722
+
1723
+ function escapeRegex(value: string): string {
1724
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1725
+ }
1726
+
1727
+ function suggestNxProjectName(rootPackageName: string, packageName: string): string | null {
1728
+ const rootScope = npmScope(rootPackageName);
1729
+ if (!rootScope || npmScope(packageName) !== rootScope) {
1730
+ return null;
1731
+ }
1732
+ return unscopedPackageName(packageName);
1733
+ }
1734
+
1735
+ function npmScope(packageName: string): string | null {
1736
+ const match = /^(@[^/]+)\//.exec(packageName);
1737
+ return match?.[1] ?? null;
1738
+ }
1739
+
1740
+ function unscopedPackageName(packageName: string): string {
1741
+ return packageName.startsWith('@') ? packageName.slice(packageName.indexOf('/') + 1) : packageName;
1742
+ }
1743
+
358
1744
  function setBooleanProperty(record: Record<string, unknown>, key: string, value: boolean): boolean {
359
1745
  if (record[key] === value) {
360
1746
  return false;