@smoothbricks/cli 0.2.0 → 0.3.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 (68) hide show
  1. package/README.md +29 -24
  2. package/dist/cli.d.ts.map +1 -1
  3. package/dist/cli.js +19 -3
  4. package/dist/generate/index.d.ts +20 -0
  5. package/dist/generate/index.d.ts.map +1 -0
  6. package/dist/generate/index.js +47 -0
  7. package/dist/lib/run.d.ts +1 -0
  8. package/dist/lib/run.d.ts.map +1 -1
  9. package/dist/lib/run.js +19 -0
  10. package/dist/lib/workspace.d.ts +10 -0
  11. package/dist/lib/workspace.d.ts.map +1 -1
  12. package/dist/lib/workspace.js +55 -16
  13. package/dist/monorepo/lockfile.d.ts.map +1 -1
  14. package/dist/monorepo/lockfile.js +9 -0
  15. package/dist/monorepo/package-policy.d.ts +6 -17
  16. package/dist/monorepo/package-policy.d.ts.map +1 -1
  17. package/dist/monorepo/package-policy.js +63 -1127
  18. package/dist/monorepo/publish-workflow.js +2 -3
  19. package/dist/nx/index.d.ts +1 -0
  20. package/dist/nx/index.d.ts.map +1 -1
  21. package/dist/nx/index.js +23 -5
  22. package/dist/release/candidates.d.ts +1 -0
  23. package/dist/release/candidates.d.ts.map +1 -1
  24. package/dist/release/candidates.js +12 -0
  25. package/dist/release/core.d.ts +3 -1
  26. package/dist/release/core.d.ts.map +1 -1
  27. package/dist/release/core.js +76 -9
  28. package/dist/release/github-release.d.ts +1 -0
  29. package/dist/release/github-release.d.ts.map +1 -1
  30. package/dist/release/github-release.js +1 -1
  31. package/dist/release/index.d.ts +9 -3
  32. package/dist/release/index.d.ts.map +1 -1
  33. package/dist/release/index.js +287 -39
  34. package/dist/release/orchestration.d.ts +10 -2
  35. package/dist/release/orchestration.d.ts.map +1 -1
  36. package/dist/release/orchestration.js +33 -3
  37. package/managed/raw/tooling/git-hooks/commit-msg.sh +7 -1
  38. package/managed/raw/tooling/git-hooks/pre-commit.sh +7 -1
  39. package/managed/templates/github/workflows/ci.yml +3 -2
  40. package/package.json +7 -12
  41. package/src/cli.ts +21 -4
  42. package/src/generate/index.ts +92 -0
  43. package/src/lib/run.ts +25 -0
  44. package/src/lib/workspace.ts +72 -16
  45. package/src/monorepo/lockfile.ts +9 -0
  46. package/src/monorepo/package-policy.test.ts +254 -26
  47. package/src/monorepo/package-policy.ts +88 -1351
  48. package/src/monorepo/publish-workflow.ts +2 -3
  49. package/src/nx/index.test.ts +9 -0
  50. package/src/nx/index.ts +25 -5
  51. package/src/release/__tests__/candidates.test.ts +109 -8
  52. package/src/release/__tests__/core-properties.test.ts +2 -2
  53. package/src/release/__tests__/core-scenarios.test.ts +12 -15
  54. package/src/release/__tests__/core.test.ts +50 -3
  55. package/src/release/__tests__/fixture-repo.test.ts +68 -18
  56. package/src/release/__tests__/github-release.test.ts +9 -16
  57. package/src/release/__tests__/orchestration.test.ts +91 -5
  58. package/src/release/__tests__/retag-unpublished.test.ts +9 -9
  59. package/src/release/__tests__/trust-publisher.test.ts +65 -33
  60. package/src/release/candidates.ts +13 -0
  61. package/src/release/core.ts +95 -10
  62. package/src/release/github-release.ts +1 -1
  63. package/src/release/index.ts +366 -49
  64. package/src/release/orchestration.ts +50 -5
  65. package/dist/nx-version-actions.cjs +0 -25
  66. package/dist/nx-version-actions.d.cts +0 -6
  67. package/dist/nx-version-actions.d.cts.map +0 -1
  68. package/src/nx-version-actions.cts +0 -36
@@ -1,8 +1,27 @@
1
- import { existsSync, readFileSync } from 'node:fs';
2
- import { join, relative } from 'node:path';
1
+ import { join } from 'node:path';
2
+ import {
3
+ applyWorkspaceBoundedTestTargetPolicy,
4
+ checkWorkspaceBoundedTestTargetPolicy,
5
+ } from '@smoothbricks/nx-plugin/bounded-test-policy';
6
+ import {
7
+ applyPackageTargetPolicy,
8
+ checkPackageTargetPolicy,
9
+ type PackageTargetPolicyOptions,
10
+ type ResolvedProjectTargets,
11
+ } from '@smoothbricks/nx-plugin/package-target-policy';
12
+ import {
13
+ applyReleaseConfigPolicy,
14
+ checkReleaseConfigPolicy,
15
+ SMOO_NX_RELEASE_TAG_PATTERN,
16
+ SMOO_NX_VERSION_ACTIONS,
17
+ } from '@smoothbricks/nx-plugin/release-config-policy';
18
+ import { applyTypecheckTestPolicy, checkTypecheckTestPolicy } from '@smoothbricks/nx-plugin/typecheck-test-policy';
19
+ import {
20
+ applyWorkspaceConfigPolicy,
21
+ checkWorkspaceConfigPolicy,
22
+ } from '@smoothbricks/nx-plugin/workspace-config-policy';
3
23
  import {
4
24
  getOrCreateRecord,
5
- hasOwn,
6
25
  hasOwnString,
7
26
  isRecord,
8
27
  readJsonObject,
@@ -14,6 +33,7 @@ import {
14
33
  writeJsonObject,
15
34
  } from '../lib/json.js';
16
35
  import {
36
+ getWorkspacePackageManifests,
17
37
  getWorkspacePackages,
18
38
  listPackageJsonRecords,
19
39
  listPublicPackages,
@@ -23,17 +43,9 @@ import {
23
43
  workspaceDependencyFields,
24
44
  } from '../lib/workspace.js';
25
45
 
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
- }
46
+ export type { PackageTargetPolicyOptions as WorkspaceDependencyDefaultOptions, ResolvedProjectTargets };
47
+ export { SMOO_NX_RELEASE_TAG_PATTERN, SMOO_NX_VERSION_ACTIONS };
34
48
 
35
- export const SMOO_NX_VERSION_ACTIONS = '@smoothbricks/cli/nx-version-actions';
36
- export const SMOO_NX_RELEASE_TAG_PATTERN = '{projectName}@{version}';
37
49
  const extraCommitScopes = ['release'];
38
50
  const rootScriptPolicy: Record<string, string> = {
39
51
  lint: 'nx run-many -t lint',
@@ -41,17 +53,6 @@ const rootScriptPolicy: Record<string, string> = {
41
53
  'format:staged': 'git-format-staged --config tooling/git-hooks/git-format-staged.yml',
42
54
  'format:changed': 'git-format-staged --config tooling/git-hooks/git-format-staged.yml --also-unstaged',
43
55
  };
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
 
56
57
  export function applyFixableMonorepoDefaults(root: string): void {
57
58
  applyRootScriptDefaults(root);
@@ -85,25 +86,7 @@ export function applyRootScriptDefaults(root: string): void {
85
86
  }
86
87
 
87
88
  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);
89
+ if (applyWorkspaceConfigPolicy(root)) {
107
90
  console.log('updated nx.json smoo plugin config');
108
91
  } else {
109
92
  console.log('unchanged nx.json smoo plugin config');
@@ -151,62 +134,34 @@ export function applyPublicPackageDefaults(root: string): void {
151
134
  }
152
135
  }
153
136
 
154
- export function applyWorkspaceDependencyDefaults(root: string, options: WorkspaceDependencyDefaultOptions = {}): void {
137
+ export function applyWorkspaceDependencyDefaults(root: string, options: PackageTargetPolicyOptions = {}): void {
155
138
  const workspaceNames = new Set(getWorkspacePackages(root).map((pkg) => pkg.name));
156
139
  for (const pkg of listPackageJsonRecords(root)) {
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;
140
+ const changed = fixWorkspaceDependencyRanges(pkg.json, workspaceNames);
166
141
  if (changed) {
167
142
  writeJsonObject(pkg.packageJsonPath, pkg.json);
168
- console.log(`updated ${pkg.path}/package.json workspace dependency policy`);
169
- } else {
170
- console.log(`unchanged ${pkg.path}/package.json workspace dependency policy`);
143
+ console.log(`updated ${pkg.path}/package.json workspace dependency ranges`);
171
144
  }
172
- applyBunTestTsconfigDefaults(root, pkg.path, pkg.json, workspaceNames);
173
- applyTsconfigTestReferenceDefaults(root, pkg.path);
174
145
  }
175
- }
176
-
177
- export function applyNxReleaseDefaults(root: string): void {
178
- const nxJsonPath = join(root, 'nx.json');
179
- const nxJson = requiredJsonObject(nxJsonPath);
180
- let changed = false;
181
- const release = getOrCreateRecord(nxJson, 'release');
182
- changed = setStringProperty(release, 'projectsRelationship', 'independent') || changed;
183
- const version = getOrCreateRecord(release, 'version');
184
- changed = setStringProperty(version, 'specifierSource', 'conventional-commits') || changed;
185
- changed = setStringProperty(version, 'currentVersionResolver', 'git-tag') || changed;
186
- changed = setStringProperty(version, 'fallbackCurrentVersionResolver', 'disk') || changed;
187
- changed = setStringProperty(version, 'versionActions', SMOO_NX_VERSION_ACTIONS) || changed;
188
- if (delete version.preVersionCommand) {
189
- changed = true;
146
+ if (applyPackageTargetPolicy(root, options)) {
147
+ console.log('updated package Nx target policy');
148
+ } else {
149
+ console.log('unchanged package Nx target policy');
190
150
  }
191
- const releaseTag = getOrCreateRecord(release, 'releaseTag');
192
- changed = setStringProperty(releaseTag, 'pattern', SMOO_NX_RELEASE_TAG_PATTERN) || changed;
193
- const changelog = getOrCreateRecord(release, 'changelog');
194
- changed = setBooleanProperty(changelog, 'workspaceChangelog', false) || changed;
195
- const projectChangelogs = getOrCreateRecord(changelog, 'projectChangelogs');
196
- changed = setBooleanProperty(projectChangelogs, 'createRelease', false) || changed;
197
- changed = setBooleanProperty(projectChangelogs, 'file', false) || changed;
198
- const renderOptions = getOrCreateRecord(projectChangelogs, 'renderOptions');
199
- if (typeof renderOptions.authors !== 'boolean') {
200
- renderOptions.authors = true;
201
- changed = true;
151
+ if (applyTypecheckTestPolicy(root)) {
152
+ console.log('updated tsconfig.test.json policy');
153
+ } else {
154
+ console.log('unchanged tsconfig.test.json policy');
202
155
  }
203
- if (typeof renderOptions.applyUsernameToAuthors !== 'boolean') {
204
- renderOptions.applyUsernameToAuthors = true;
205
- changed = true;
156
+ if (applyWorkspaceBoundedTestTargetPolicy(root)) {
157
+ console.log('updated package test targets bounded execution policy');
158
+ } else {
159
+ console.log('unchanged package test targets bounded execution policy');
206
160
  }
161
+ }
207
162
 
208
- if (changed) {
209
- writeJsonObject(nxJsonPath, nxJson);
163
+ export function applyNxReleaseDefaults(root: string): void {
164
+ if (applyReleaseConfigPolicy(root)) {
210
165
  console.log('updated nx.json release config');
211
166
  } else {
212
167
  console.log('unchanged nx.json release config');
@@ -219,12 +174,13 @@ export function applyNxProjectNameDefaults(root: string): void {
219
174
  if (!rootName) {
220
175
  return;
221
176
  }
222
- for (const pkg of getWorkspacePackages(root)) {
177
+ for (const pkg of getWorkspacePackageManifests(root)) {
223
178
  const suggestedName = suggestNxProjectName(rootName, pkg.name);
224
179
  if (!suggestedName) {
225
180
  continue;
226
181
  }
227
- const changed = applyPackageNxConfig(pkg.json, { projectName: suggestedName }).changed;
182
+ const nx = getOrCreateRecord(pkg.json, 'nx');
183
+ const changed = setStringProperty(nx, 'name', suggestedName);
228
184
  if (changed) {
229
185
  writeJsonObject(pkg.packageJsonPath, pkg.json);
230
186
  console.log(`updated ${pkg.path}/package.json nx.name`);
@@ -242,7 +198,7 @@ export function listNxProjectNames(root: string): string[] {
242
198
  const rootPackage = readJsonObject(join(root, 'package.json'));
243
199
  const rootName = rootPackage ? stringProperty(rootPackage, 'name') : null;
244
200
  const names: string[] = [];
245
- for (const pkg of getWorkspacePackages(root)) {
201
+ for (const pkg of getWorkspacePackageManifests(root)) {
246
202
  const nx = recordProperty(pkg.json, 'nx');
247
203
  const configuredName = nx ? stringProperty(nx, 'name') : null;
248
204
  const suggestedName = rootName ? suggestNxProjectName(rootName, pkg.name) : null;
@@ -296,85 +252,13 @@ export function validateRootPackagePolicy(root: string): number {
296
252
  }
297
253
 
298
254
  export function validateNxReleaseConfig(root: string): number {
299
- const nxJson = readJsonObject(join(root, 'nx.json'));
300
- if (!nxJson) {
301
- console.error('nx.json not found or invalid');
302
- return 1;
303
- }
304
- const release = recordProperty(nxJson, 'release');
305
- const version = release ? recordProperty(release, 'version') : null;
306
- const releaseTag = release ? recordProperty(release, 'releaseTag') : null;
307
- const changelog = release ? recordProperty(release, 'changelog') : null;
308
- const projectChangelogs = changelog ? recordProperty(changelog, 'projectChangelogs') : null;
309
- const renderOptions = projectChangelogs ? recordProperty(projectChangelogs, 'renderOptions') : null;
310
255
  let failures = 0;
311
- if (!release) {
312
- console.error('nx.json release config is missing');
313
- failures++;
314
- }
315
- failures += validateNxPluginConfig(nxJson);
316
- if (release && stringProperty(release, 'projectsRelationship') !== 'independent') {
317
- console.error('nx.json release.projectsRelationship must be independent');
256
+ for (const issue of checkWorkspaceConfigPolicy(root)) {
257
+ console.error(issue.message);
318
258
  failures++;
319
259
  }
320
- if (!version) {
321
- console.error('nx.json release.version config is missing');
322
- failures++;
323
- }
324
- if (version && stringProperty(version, 'specifierSource') !== 'conventional-commits') {
325
- console.error('nx.json release.version.specifierSource must be conventional-commits');
326
- failures++;
327
- }
328
- // Nx requires git-tag as the primary resolver when deriving versions from
329
- // conventional commits. Disk is only a fallback for initial untagged packages.
330
- if (version && stringProperty(version, 'currentVersionResolver') !== 'git-tag') {
331
- console.error('nx.json release.version.currentVersionResolver must be git-tag');
332
- failures++;
333
- }
334
- if (version && stringProperty(version, 'fallbackCurrentVersionResolver') !== 'disk') {
335
- console.error('nx.json release.version.fallbackCurrentVersionResolver must be disk');
336
- failures++;
337
- }
338
- if (version && stringProperty(version, 'versionActions') !== SMOO_NX_VERSION_ACTIONS) {
339
- console.error(`nx.json release.version.versionActions must be ${SMOO_NX_VERSION_ACTIONS}`);
340
- failures++;
341
- }
342
- if (version && stringProperty(version, 'preVersionCommand')) {
343
- console.error(
344
- 'nx.json release.version.preVersionCommand must not be defined; smoo builds npm-missing packages before publish',
345
- );
346
- failures++;
347
- }
348
- if (!releaseTag) {
349
- console.error('nx.json release.releaseTag config is missing');
350
- failures++;
351
- }
352
- if (releaseTag && stringProperty(releaseTag, 'pattern') !== SMOO_NX_RELEASE_TAG_PATTERN) {
353
- console.error(`nx.json release.releaseTag.pattern must be ${SMOO_NX_RELEASE_TAG_PATTERN}`);
354
- failures++;
355
- }
356
- if (!changelog) {
357
- console.error('nx.json release.changelog config is missing');
358
- failures++;
359
- }
360
- if (changelog && changelog.workspaceChangelog !== false) {
361
- console.error('nx.json release.changelog.workspaceChangelog must be false');
362
- failures++;
363
- }
364
- if (!projectChangelogs) {
365
- console.error('nx.json release.changelog.projectChangelogs config is missing');
366
- failures++;
367
- }
368
- if (projectChangelogs && projectChangelogs.createRelease !== false) {
369
- console.error('nx.json release.changelog.projectChangelogs.createRelease must be false');
370
- failures++;
371
- }
372
- if (projectChangelogs && projectChangelogs.file !== false) {
373
- console.error('nx.json release.changelog.projectChangelogs.file must be false');
374
- failures++;
375
- }
376
- if (!renderOptions) {
377
- console.error('nx.json release.changelog.projectChangelogs.renderOptions config is missing');
260
+ for (const issue of checkReleaseConfigPolicy(root)) {
261
+ console.error(issue.message);
378
262
  failures++;
379
263
  }
380
264
  return failures;
@@ -387,7 +271,7 @@ export function validateNxProjectNames(root: string): number {
387
271
  return 0;
388
272
  }
389
273
  let failures = 0;
390
- for (const pkg of getWorkspacePackages(root)) {
274
+ for (const pkg of getWorkspacePackageManifests(root)) {
391
275
  const suggestedName = suggestNxProjectName(rootName, pkg.name);
392
276
  if (!suggestedName) {
393
277
  continue;
@@ -483,19 +367,10 @@ export function validatePublicPackageMetadata(root: string): number {
483
367
  return failures;
484
368
  }
485
369
 
486
- export function validateWorkspaceDependencies(root: string, options: WorkspaceDependencyDefaultOptions = {}): number {
487
- const workspaceNames = new Set(getWorkspacePackages(root).map((pkg) => pkg.name));
370
+ export function validateWorkspaceDependencies(root: string, options: PackageTargetPolicyOptions = {}): number {
488
371
  let failures = 0;
372
+ const workspaceNames = new Set(getWorkspacePackages(root).map((pkg) => pkg.name));
489
373
  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);
499
374
  for (const field of workspaceDependencyFields) {
500
375
  const dependencies = recordProperty(pkg.json, field);
501
376
  if (!dependencies) {
@@ -508,174 +383,29 @@ export function validateWorkspaceDependencies(root: string, options: WorkspaceDe
508
383
  }
509
384
  }
510
385
  }
511
- failures += validatePackageScriptPolicy(pkg.json, pkg.path, workspaceNames, { resolvedTargets });
512
386
  }
513
- if (failures === 0) {
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
- }
387
+ for (const issue of checkPackageTargetPolicy(root, options)) {
388
+ console.error(`${issue.path}: ${issue.message}`);
389
+ failures++;
588
390
  }
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;
391
+ for (const issue of checkTypecheckTestPolicy(root)) {
392
+ console.error(`${issue.path}: ${issue.message}`);
393
+ failures++;
600
394
  }
601
- const scripts = recordProperty(pkg, 'scripts');
602
- if (!scripts) {
603
- return 0;
395
+ for (const issue of checkWorkspaceBoundedTestTargetPolicy(root)) {
396
+ console.error(`${issue.path}: ${issue.message}`);
397
+ failures++;
604
398
  }
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
- }
399
+ if (failures === 0) {
400
+ console.log('Workspace dependency policy is valid.');
675
401
  }
676
402
  return failures;
677
403
  }
678
404
 
405
+ // ---------------------------------------------------------------------------
406
+ // Helpers kept in CLI (not Nx-specific)
407
+ // ---------------------------------------------------------------------------
408
+
679
409
  function fixWorkspaceDependencyRanges(pkg: Record<string, unknown>, workspaceNames: Set<string>): boolean {
680
410
  let changed = false;
681
411
  for (const field of workspaceDependencyFields) {
@@ -720,991 +450,35 @@ function validateRootNxScriptInference(rootPackage: Record<string, unknown>): nu
720
450
  return 1;
721
451
  }
722
452
 
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`);
453
+ function suggestNxProjectName(rootPackageName: string, packageName: string): string | null {
454
+ const rootScope = npmScope(rootPackageName);
455
+ if (!rootScope || npmScope(packageName) !== rootScope) {
456
+ return null;
910
457
  }
458
+ return unscopedPackageName(packageName);
911
459
  }
912
460
 
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`);
461
+ function npmScope(packageName: string): string | null {
462
+ const match = /^(@[^/]+)\//.exec(packageName);
463
+ return match?.[1] ?? null;
923
464
  }
924
465
 
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;
466
+ function unscopedPackageName(packageName: string): string {
467
+ return packageName.startsWith('@') ? packageName.slice(packageName.indexOf('/') + 1) : packageName;
959
468
  }
960
469
 
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';
470
+ function recordKeysAreSorted(record: Record<string, unknown>): boolean {
471
+ const keys = Object.keys(record);
472
+ return keys.every((key, index) => index === 0 || keys[index - 1] <= key);
964
473
  }
965
474
 
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) {
475
+ function sortRecordInPlace(record: Record<string, unknown>): boolean {
476
+ if (recordKeysAreSorted(record)) {
970
477
  return false;
971
478
  }
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];
479
+ const entries = Object.entries(record).sort(([a], [b]) => a.localeCompare(b));
480
+ for (const key of Object.keys(record)) {
481
+ delete record[key];
1708
482
  }
1709
483
  for (const [key, value] of entries) {
1710
484
  record[key] = value;
@@ -1712,43 +486,6 @@ function sortRecordInPlace(record: Record<string, unknown>): boolean {
1712
486
  return true;
1713
487
  }
1714
488
 
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
-
1744
- function setBooleanProperty(record: Record<string, unknown>, key: string, value: boolean): boolean {
1745
- if (record[key] === value) {
1746
- return false;
1747
- }
1748
- record[key] = value;
1749
- return true;
1750
- }
1751
-
1752
489
  function normalizeExportConditionOrder(value: unknown): boolean {
1753
490
  if (!isRecord(value)) {
1754
491
  return false;