@smoothbricks/nx-plugin 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,8 @@ export type { NxPolicyIssue };
4
4
  export interface ResolvedProjectTargets {
5
5
  targets: ReadonlySet<string>;
6
6
  buildDependsOn?: readonly string[];
7
+ targetExecutors?: ReadonlyMap<string, string>;
8
+ targetScripts?: ReadonlyMap<string, string>;
7
9
  }
8
10
  export interface PackageTargetPolicyOptions {
9
11
  resolvedTargetsByProject?: ReadonlyMap<string, ReadonlySet<string> | ResolvedProjectTargets>;
@@ -1 +1 @@
1
- {"version":3,"file":"package-target-policy.d.ts","sourceRoot":"","sources":["../src/package-target-policy.ts"],"names":[],"mappings":"AAGA,OAAO,EAAyB,KAAK,IAAI,EAAc,MAAM,0BAA0B,CAAC;AAGxF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAElE,YAAY,EAAE,aAAa,EAAE,CAAC;AAE9B,MAAM,WAAW,sBAAsB;IACrC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC7B,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACpC;AAED,MAAM,WAAW,0BAA0B;IACzC,wBAAwB,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,sBAAsB,CAAC,CAAC;CAC9F;AAED,eAAO,MAAM,yBAAyB,UAWrC,CAAC;AA0HF,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI,CAGhF;AA0ID,wBAAgB,UAAU,CACxB,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,OAAO,EACnB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GACtB,MAAM,CAMR;AA8jBD;;;GAGG;AACH,wBAAgB,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hH;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,0BAA+B,GAAG,OAAO,CA6BxG"}
@@ -141,11 +141,17 @@ function resolvedProjectBuildDependsOn(resolvedProject) {
141
141
  }
142
142
  return resolvedProject.buildDependsOn;
143
143
  }
144
+ function resolvedProjectTargetExecutor(resolvedProject, targetName) {
145
+ return isResolvedProjectTargets(resolvedProject) ? resolvedProject.targetExecutors?.get(targetName) : undefined;
146
+ }
147
+ function resolvedProjectTargetScript(resolvedProject, targetName) {
148
+ return isResolvedProjectTargets(resolvedProject) ? resolvedProject.targetScripts?.get(targetName) : undefined;
149
+ }
144
150
  function isResolvedProjectTargets(value) {
145
151
  return isRecord(value) && value.targets instanceof Set;
146
152
  }
147
153
  function targetExistsInResolvedProject(targetName, resolvedTargets) {
148
- return resolvedTargets?.has(targetName) === true;
154
+ return resolvedProjectTargetNames(resolvedTargets)?.has(targetName) === true;
149
155
  }
150
156
  function isBuildOutputDependencyPattern(dependency) {
151
157
  return BUILD_OUTPUT_DEPENDENCIES.includes(dependency);
@@ -218,6 +224,13 @@ export function nxRunAlias(projectName, targetName, continuous, command) {
218
224
  const flags = continuous ? ` --outputStyle=${nxRunOutputStyle(command)}` : '';
219
225
  return `nx run ${projectName}:${targetName}${flags}`;
220
226
  }
227
+ function nxRunAliasMatches(rawAlias, expectedAlias) {
228
+ const expected = parseNxRunAlias(expectedAlias);
229
+ return (!!rawAlias &&
230
+ !!expected &&
231
+ rawAlias.projectName === expected.projectName &&
232
+ rawAlias.targetName === expected.targetName);
233
+ }
221
234
  function nxRunOutputStyle(command) {
222
235
  if (command && /(?:^|\s)(?:astro|vite)\s+dev(?:\s|$)/.test(command)) {
223
236
  return 'dynamic-legacy';
@@ -523,6 +536,13 @@ function validatePackageScriptPolicy(pkg, packagePath, workspaceNames, options =
523
536
  continue;
524
537
  }
525
538
  const alias = parseNxRunAlias(rawCommand);
539
+ if (isResolvedSelfRecursiveScript(scriptName, alias, projectName, options.resolvedTargets)) {
540
+ issues.push({
541
+ path: packagePath,
542
+ message: `${packagePath}: scripts.${scriptName} recursively delegates to its own inferred Nx target; remove the script so the resolved Nx target can run`,
543
+ });
544
+ continue;
545
+ }
526
546
  const rewrite = alias
527
547
  ? { targetName: alias.targetName, continuous: isContinuousTarget(alias.targetName, '') }
528
548
  : classifyScriptRewrite(scriptName, rawCommand);
@@ -547,7 +567,7 @@ function validatePackageScriptPolicy(pkg, packagePath, workspaceNames, options =
547
567
  const command = targetOptions ? stringProperty(targetOptions, 'command') : null;
548
568
  const rewriteCommand = 'command' in rewrite ? rewrite.command : null;
549
569
  const expectedAlias = nxRunAlias(projectName, rewrite.targetName, rewrite.continuous, command ?? rewriteCommand);
550
- if (rawCommand !== expectedAlias) {
570
+ if (alias ? !nxRunAliasMatches(alias, expectedAlias) : rawCommand !== expectedAlias) {
551
571
  issues.push({
552
572
  path: packagePath,
553
573
  message: `${packagePath}: scripts.${scriptName} must delegate to ${expectedAlias}`,
@@ -829,6 +849,11 @@ function applyPackageScriptPolicyForPkg(pkg, _packagePath, workspaceNames, optio
829
849
  continue;
830
850
  }
831
851
  const parsedAlias = parseNxRunAlias(rawCommand);
852
+ if (isResolvedSelfRecursiveScript(scriptName, parsedAlias, projectName, options.resolvedTargets)) {
853
+ delete scripts[scriptName];
854
+ changed = true;
855
+ continue;
856
+ }
832
857
  if (parsedAlias?.projectName === projectName && parsedAlias.targetName === 'test') {
833
858
  continue;
834
859
  }
@@ -881,6 +906,12 @@ function applyPackageScriptPolicyForPkg(pkg, _packagePath, workspaceNames, optio
881
906
  }
882
907
  return changed;
883
908
  }
909
+ function isResolvedSelfRecursiveScript(scriptName, alias, projectName, resolvedTargets) {
910
+ return (alias?.projectName === projectName &&
911
+ alias.targetName === scriptName &&
912
+ resolvedProjectTargetExecutor(resolvedTargets, scriptName) === 'nx:run-script' &&
913
+ resolvedProjectTargetScript(resolvedTargets, scriptName) === scriptName);
914
+ }
884
915
  export function checkPackageTargets(pkg, packagePath, resolvedTargets) {
885
916
  return [...validateExplicitNxTargets(pkg, packagePath, resolvedTargets)];
886
917
  }
@@ -906,7 +937,7 @@ export function checkPackageTargetPolicy(root, options = {}) {
906
937
  issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedTargets));
907
938
  issues.push(...validateTestEntrypointPresence(root, packagePath, pkg));
908
939
  issues.push(...validateBuildZigPolicy(root, packagePath));
909
- issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets }));
940
+ issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets: resolvedProject }));
910
941
  }
911
942
  return issues;
912
943
  }
@@ -927,7 +958,8 @@ export function applyPackageTargetPolicy(root, options = {}) {
927
958
  packageChanged = removePackageColonTargets(pkg) || packageChanged;
928
959
  packageChanged = removeRedundantNoopBuildTarget(pkg, resolvedProject) || packageChanged;
929
960
  packageChanged =
930
- applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, { resolvedTargets }) || packageChanged;
961
+ applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, { resolvedTargets: resolvedProject }) ||
962
+ packageChanged;
931
963
  if (packageChanged) {
932
964
  writeJsonObject(packageJsonPath, pkg);
933
965
  changed = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoothbricks/nx-plugin",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./loader.js",
@@ -262,6 +262,141 @@ describe('package target policy', () => {
262
262
  }
263
263
  });
264
264
 
265
+ it('removes self-recursive scripts using resolved Nx target metadata', async () => {
266
+ const root = await mkdtemp(join(tmpdir(), 'smoothbricks-pkg-target-'));
267
+ try {
268
+ await writeJson(join(root, 'package.json'), {
269
+ name: '@scope/root',
270
+ private: true,
271
+ workspaces: ['packages/*'],
272
+ });
273
+ await writeJson(join(root, 'packages/app/package.json'), {
274
+ name: '@scope/app',
275
+ dependencies: { '@scope/lib': 'workspace:*' },
276
+ scripts: {
277
+ build: 'nx run app:build',
278
+ test: 'bun test',
279
+ },
280
+ nx: {
281
+ name: 'app',
282
+ targets: {
283
+ 'tsdown-js': {
284
+ executor: 'nx:run-commands',
285
+ options: { command: 'tsdown', cwd: '{projectRoot}' },
286
+ },
287
+ },
288
+ },
289
+ });
290
+ await writeJson(join(root, 'packages/lib/package.json'), {
291
+ name: '@scope/lib',
292
+ });
293
+ const resolvedTargetsByProject = new Map<string, ResolvedProjectTargets>([
294
+ [
295
+ 'app',
296
+ {
297
+ targets: new Set(['build', 'test', 'tsdown-js']),
298
+ targetExecutors: new Map([
299
+ ['build', 'nx:run-script'],
300
+ ['test', 'nx:run-script'],
301
+ ['tsdown-js', 'nx:run-commands'],
302
+ ]),
303
+ targetScripts: new Map([
304
+ ['build', 'build'],
305
+ ['test', 'test'],
306
+ ]),
307
+ },
308
+ ],
309
+ ]);
310
+
311
+ const issues = checkPackageTargetPolicy(root, { resolvedTargetsByProject });
312
+ expect(issues.some((i) => i.message.includes('scripts.build recursively delegates'))).toBe(true);
313
+ expect(applyPackageTargetPolicy(root, { resolvedTargetsByProject })).toBe(true);
314
+
315
+ const app = JSON.parse(await readFile(join(root, 'packages/app/package.json'), 'utf8'));
316
+ expect(app.scripts.build).toBeUndefined();
317
+ expect(app.scripts.test).toBe('bun test');
318
+ } finally {
319
+ await rm(root, { recursive: true, force: true });
320
+ }
321
+ });
322
+
323
+ it('keeps self-alias scripts when resolved target comes from project configuration', async () => {
324
+ const root = await mkdtemp(join(tmpdir(), 'smoothbricks-pkg-target-'));
325
+ try {
326
+ await writeJson(join(root, 'package.json'), {
327
+ name: '@scope/root',
328
+ private: true,
329
+ workspaces: ['packages/*'],
330
+ });
331
+ await writeJson(join(root, 'packages/app/package.json'), {
332
+ name: '@scope/app',
333
+ dependencies: { '@scope/lib': 'workspace:*' },
334
+ scripts: {
335
+ build: 'nx run app:build',
336
+ },
337
+ nx: { name: 'app' },
338
+ });
339
+ await writeJson(join(root, 'packages/lib/package.json'), {
340
+ name: '@scope/lib',
341
+ });
342
+ const resolvedTargetsByProject = new Map<string, ResolvedProjectTargets>([
343
+ [
344
+ 'app',
345
+ {
346
+ targets: new Set(['build']),
347
+ targetExecutors: new Map([['build', 'nx:run-commands']]),
348
+ },
349
+ ],
350
+ ]);
351
+
352
+ expect(applyPackageTargetPolicy(root, { resolvedTargetsByProject })).toBe(false);
353
+
354
+ const app = JSON.parse(await readFile(join(root, 'packages/app/package.json'), 'utf8'));
355
+ expect(app.scripts.build).toBe('nx run app:build');
356
+ } finally {
357
+ await rm(root, { recursive: true, force: true });
358
+ }
359
+ });
360
+
361
+ it('accepts stream output style on non-continuous nx aliases', async () => {
362
+ const root = await mkdtemp(join(tmpdir(), 'smoothbricks-pkg-target-'));
363
+ try {
364
+ await writeJson(join(root, 'package.json'), {
365
+ name: '@scope/root',
366
+ private: true,
367
+ workspaces: ['packages/*'],
368
+ });
369
+ await writeJson(join(root, 'packages/app/package.json'), {
370
+ name: '@scope/app',
371
+ dependencies: { '@scope/lib': 'workspace:*' },
372
+ scripts: {
373
+ deploy: 'nx run app:deploy:production --outputStyle=stream',
374
+ },
375
+ nx: {
376
+ name: 'app',
377
+ targets: {
378
+ deploy: {
379
+ executor: 'nx:run-commands',
380
+ options: { command: 'wrangler deploy --env production', cwd: '{projectRoot}' },
381
+ configurations: {
382
+ production: { command: 'wrangler deploy --env production' },
383
+ },
384
+ },
385
+ },
386
+ },
387
+ });
388
+ await writeJson(join(root, 'packages/lib/package.json'), {
389
+ name: '@scope/lib',
390
+ });
391
+
392
+ const issues = checkPackageTargetPolicy(root);
393
+ expect(issues.some((i) => i.message.includes('scripts.deploy must delegate'))).toBe(false);
394
+ expect(applyPackageTargetPolicy(root)).toBe(false);
395
+ } finally {
396
+ await rm(root, { recursive: true, force: true });
397
+ }
398
+ });
399
+
265
400
  it('moves env assignments into target options', async () => {
266
401
  const root = await mkdtemp(join(tmpdir(), 'smoothbricks-pkg-target-'));
267
402
  try {
@@ -11,6 +11,8 @@ export type { NxPolicyIssue };
11
11
  export interface ResolvedProjectTargets {
12
12
  targets: ReadonlySet<string>;
13
13
  buildDependsOn?: readonly string[];
14
+ targetExecutors?: ReadonlyMap<string, string>;
15
+ targetScripts?: ReadonlyMap<string, string>;
14
16
  }
15
17
 
16
18
  export interface PackageTargetPolicyOptions {
@@ -200,14 +202,31 @@ function resolvedProjectBuildDependsOn(
200
202
  return resolvedProject.buildDependsOn;
201
203
  }
202
204
 
205
+ function resolvedProjectTargetExecutor(
206
+ resolvedProject: ReadonlySet<string> | ResolvedProjectTargets | undefined,
207
+ targetName: string,
208
+ ): string | undefined {
209
+ return isResolvedProjectTargets(resolvedProject) ? resolvedProject.targetExecutors?.get(targetName) : undefined;
210
+ }
211
+
212
+ function resolvedProjectTargetScript(
213
+ resolvedProject: ReadonlySet<string> | ResolvedProjectTargets | undefined,
214
+ targetName: string,
215
+ ): string | undefined {
216
+ return isResolvedProjectTargets(resolvedProject) ? resolvedProject.targetScripts?.get(targetName) : undefined;
217
+ }
218
+
203
219
  function isResolvedProjectTargets(
204
220
  value: ReadonlySet<string> | ResolvedProjectTargets | undefined,
205
221
  ): value is ResolvedProjectTargets {
206
222
  return isRecord(value) && value.targets instanceof Set;
207
223
  }
208
224
 
209
- function targetExistsInResolvedProject(targetName: string, resolvedTargets?: ReadonlySet<string>): boolean {
210
- return resolvedTargets?.has(targetName) === true;
225
+ function targetExistsInResolvedProject(
226
+ targetName: string,
227
+ resolvedTargets?: ReadonlySet<string> | ResolvedProjectTargets,
228
+ ): boolean {
229
+ return resolvedProjectTargetNames(resolvedTargets)?.has(targetName) === true;
211
230
  }
212
231
 
213
232
  function isBuildOutputDependencyPattern(dependency: string): boolean {
@@ -304,6 +323,19 @@ export function nxRunAlias(
304
323
  return `nx run ${projectName}:${targetName}${flags}`;
305
324
  }
306
325
 
326
+ function nxRunAliasMatches(
327
+ rawAlias: { projectName: string; targetName: string } | null,
328
+ expectedAlias: string,
329
+ ): boolean {
330
+ const expected = parseNxRunAlias(expectedAlias);
331
+ return (
332
+ !!rawAlias &&
333
+ !!expected &&
334
+ rawAlias.projectName === expected.projectName &&
335
+ rawAlias.targetName === expected.targetName
336
+ );
337
+ }
338
+
307
339
  function nxRunOutputStyle(command?: string | null): 'stream' | 'dynamic-legacy' {
308
340
  if (command && /(?:^|\s)(?:astro|vite)\s+dev(?:\s|$)/.test(command)) {
309
341
  return 'dynamic-legacy';
@@ -652,7 +684,7 @@ function validatePackageScriptPolicy(
652
684
  pkg: Record<string, unknown>,
653
685
  packagePath: string,
654
686
  workspaceNames: ReadonlySet<string>,
655
- options: { resolvedTargets?: ReadonlySet<string> } = {},
687
+ options: { resolvedTargets?: ReadonlySet<string> | ResolvedProjectTargets } = {},
656
688
  ): NxPolicyIssue[] {
657
689
  if (!hasWorkspaceDependency(pkg, workspaceNames)) {
658
690
  return [];
@@ -670,6 +702,13 @@ function validatePackageScriptPolicy(
670
702
  continue;
671
703
  }
672
704
  const alias = parseNxRunAlias(rawCommand);
705
+ if (isResolvedSelfRecursiveScript(scriptName, alias, projectName, options.resolvedTargets)) {
706
+ issues.push({
707
+ path: packagePath,
708
+ message: `${packagePath}: scripts.${scriptName} recursively delegates to its own inferred Nx target; remove the script so the resolved Nx target can run`,
709
+ });
710
+ continue;
711
+ }
673
712
  const rewrite = alias
674
713
  ? { targetName: alias.targetName, continuous: isContinuousTarget(alias.targetName, '') }
675
714
  : classifyScriptRewrite(scriptName, rawCommand);
@@ -694,7 +733,7 @@ function validatePackageScriptPolicy(
694
733
  const command = targetOptions ? stringProperty(targetOptions, 'command') : null;
695
734
  const rewriteCommand = 'command' in rewrite ? rewrite.command : null;
696
735
  const expectedAlias = nxRunAlias(projectName, rewrite.targetName, rewrite.continuous, command ?? rewriteCommand);
697
- if (rawCommand !== expectedAlias) {
736
+ if (alias ? !nxRunAliasMatches(alias, expectedAlias) : rawCommand !== expectedAlias) {
698
737
  issues.push({
699
738
  path: packagePath,
700
739
  message: `${packagePath}: scripts.${scriptName} must delegate to ${expectedAlias}`,
@@ -989,7 +1028,7 @@ function applyPackageScriptPolicyForPkg(
989
1028
  pkg: Record<string, unknown>,
990
1029
  _packagePath: string,
991
1030
  workspaceNames: ReadonlySet<string>,
992
- options: { resolvedTargets?: ReadonlySet<string> } = {},
1031
+ options: { resolvedTargets?: ReadonlySet<string> | ResolvedProjectTargets } = {},
993
1032
  ): boolean {
994
1033
  if (!hasWorkspaceDependency(pkg, workspaceNames)) {
995
1034
  return false;
@@ -1010,6 +1049,11 @@ function applyPackageScriptPolicyForPkg(
1010
1049
  continue;
1011
1050
  }
1012
1051
  const parsedAlias = parseNxRunAlias(rawCommand);
1052
+ if (isResolvedSelfRecursiveScript(scriptName, parsedAlias, projectName, options.resolvedTargets)) {
1053
+ delete scripts[scriptName];
1054
+ changed = true;
1055
+ continue;
1056
+ }
1013
1057
  if (parsedAlias?.projectName === projectName && parsedAlias.targetName === 'test') {
1014
1058
  continue;
1015
1059
  }
@@ -1065,6 +1109,20 @@ function applyPackageScriptPolicyForPkg(
1065
1109
  return changed;
1066
1110
  }
1067
1111
 
1112
+ function isResolvedSelfRecursiveScript(
1113
+ scriptName: string,
1114
+ alias: { projectName: string; targetName: string } | null,
1115
+ projectName: string | null,
1116
+ resolvedTargets?: ReadonlySet<string> | ResolvedProjectTargets,
1117
+ ): boolean {
1118
+ return (
1119
+ alias?.projectName === projectName &&
1120
+ alias.targetName === scriptName &&
1121
+ resolvedProjectTargetExecutor(resolvedTargets, scriptName) === 'nx:run-script' &&
1122
+ resolvedProjectTargetScript(resolvedTargets, scriptName) === scriptName
1123
+ );
1124
+ }
1125
+
1068
1126
  export function checkPackageTargets(
1069
1127
  pkg: Record<string, unknown>,
1070
1128
  packagePath: string,
@@ -1103,7 +1161,7 @@ export function checkPackageTargetPolicy(root: string, options: PackageTargetPol
1103
1161
  issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedTargets));
1104
1162
  issues.push(...validateTestEntrypointPresence(root, packagePath, pkg));
1105
1163
  issues.push(...validateBuildZigPolicy(root, packagePath));
1106
- issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets }));
1164
+ issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets: resolvedProject }));
1107
1165
  }
1108
1166
 
1109
1167
  return issues;
@@ -1128,7 +1186,8 @@ export function applyPackageTargetPolicy(root: string, options: PackageTargetPol
1128
1186
  packageChanged = removePackageColonTargets(pkg) || packageChanged;
1129
1187
  packageChanged = removeRedundantNoopBuildTarget(pkg, resolvedProject) || packageChanged;
1130
1188
  packageChanged =
1131
- applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, { resolvedTargets }) || packageChanged;
1189
+ applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, { resolvedTargets: resolvedProject }) ||
1190
+ packageChanged;
1132
1191
 
1133
1192
  if (packageChanged) {
1134
1193
  writeJsonObject(packageJsonPath, pkg);