@smoothbricks/nx-plugin 0.3.1 → 0.3.3

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,13 +4,20 @@ import { join } from 'node:path';
4
4
  import { getProjects, readJson, type Tree, updateJson } from 'nx/src/devkit-exports.js';
5
5
 
6
6
  import { boundedTestScriptAlias } from './bounded-test-policy.js';
7
- import type { NxPolicyIssue } from './workspace-config-policy.js';
8
-
7
+ import {
8
+ BUILD_OUTPUT_DEPENDENCIES,
9
+ LINUX_PLATFORM_TARGET_GLOBS,
10
+ MACOS_PLATFORM_TARGET_GLOBS,
11
+ type NxPolicyIssue,
12
+ } from './workspace-config-policy.js';
13
+
14
+ export { BUILD_OUTPUT_DEPENDENCIES, PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
9
15
  export type { NxPolicyIssue };
10
16
 
11
17
  export interface ResolvedProjectTargets {
12
18
  targets: ReadonlySet<string>;
13
19
  buildDependsOn?: readonly string[];
20
+ targetDependencies?: ReadonlyMap<string, readonly string[]>;
14
21
  targetExecutors?: ReadonlyMap<string, string>;
15
22
  targetScripts?: ReadonlyMap<string, string>;
16
23
  }
@@ -19,19 +26,6 @@ export interface PackageTargetPolicyOptions {
19
26
  resolvedTargetsByProject?: ReadonlyMap<string, ReadonlySet<string> | ResolvedProjectTargets>;
20
27
  }
21
28
 
22
- export const BUILD_OUTPUT_DEPENDENCIES = [
23
- '*-js',
24
- '*-web',
25
- '*-html',
26
- '*-css',
27
- '*-ios',
28
- '*-android',
29
- '*-native',
30
- '*-napi',
31
- '*-bun',
32
- '*-wasm',
33
- ];
34
-
35
29
  const workspaceDependencyFields = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];
36
30
 
37
31
  function isRecord(value: unknown): value is Record<string, unknown> {
@@ -230,7 +224,7 @@ function targetExistsInResolvedProject(
230
224
  }
231
225
 
232
226
  function isBuildOutputDependencyPattern(dependency: string): boolean {
233
- return BUILD_OUTPUT_DEPENDENCIES.includes(dependency);
227
+ return BUILD_OUTPUT_DEPENDENCIES.some((pattern) => pattern === dependency);
234
228
  }
235
229
 
236
230
  function expectedTargetDependencies(targetName: string): string[] {
@@ -360,7 +354,6 @@ function isSafeNxScriptCommand(command: string): boolean {
360
354
  /^tsdown(?:\s|$)/.test(trimmed) ||
361
355
  /^vite\s+(?:build|dev|preview)(?:\s|$)/.test(trimmed) ||
362
356
  /^astro\s+(?:build|dev|preview|check)(?:\s|$)/.test(trimmed) ||
363
- /^zig\s+build(?:\s|$)/.test(trimmed) ||
364
357
  /^bun\s+[./\w-]*build[\w.-]*\.ts(?:\s|$)/.test(trimmed) ||
365
358
  /(?:^|\s)(?:bench|benchmark)(?:\s|$)/.test(trimmed) ||
366
359
  /^wrangler\s+build(?:\s|$)/.test(trimmed)
@@ -424,10 +417,6 @@ function targetNameForCommand(command: string): string | null {
424
417
  if (/^tsdown(?:\s|$)/.test(trimmed)) {
425
418
  return 'tsdown-js';
426
419
  }
427
- const zigStep = /^zig\s+build\s+([A-Za-z0-9_-]+)(?:\s|$)/.exec(trimmed)?.[1];
428
- if (zigStep) {
429
- return `zig-${zigStep}`;
430
- }
431
420
  if (/^wrangler\s+build(?:\s|$)/.test(trimmed)) {
432
421
  return 'build';
433
422
  }
@@ -590,30 +579,17 @@ function directoryContainsTestFiles(path: string): boolean {
590
579
  return false;
591
580
  }
592
581
 
593
- function validateBuildZigPolicy(root: string, packagePath: string): NxPolicyIssue[] {
594
- const path = join(root, packagePath, 'build.zig');
595
- if (!existsSync(path)) {
596
- return [];
597
- }
598
- if (/\bb\.step\s*\(/.test(readFileSync(path, 'utf8'))) {
599
- return [];
600
- }
601
- return [
602
- { path: join(root, packagePath), message: `${packagePath}/build.zig must define at least one b.step(...) target` },
603
- ];
604
- }
605
-
606
582
  function validateExplicitNxTargets(
607
583
  pkg: Record<string, unknown>,
608
584
  packagePath: string,
609
- resolvedTargets?: ReadonlySet<string>,
585
+ resolvedProject?: ReadonlySet<string> | ResolvedProjectTargets,
610
586
  ): NxPolicyIssue[] {
587
+ const issues = validatePlatformTargetDependencies(packagePath, resolvedProject);
611
588
  const nx = recordProperty(pkg, 'nx');
612
589
  const targets = nx ? recordProperty(nx, 'targets') : null;
613
590
  if (!targets) {
614
- return [];
591
+ return issues;
615
592
  }
616
- const issues: NxPolicyIssue[] = [];
617
593
  for (const [targetName, rawTarget] of Object.entries(targets)) {
618
594
  if (targetName.includes(':')) {
619
595
  issues.push({
@@ -626,7 +602,76 @@ function validateExplicitNxTargets(
626
602
  if (!isRecord(rawTarget)) {
627
603
  continue;
628
604
  }
629
- issues.push(...validateTargetDependencies(rawTarget, `${packagePath}: nx.targets.${targetName}`, resolvedTargets));
605
+ issues.push(
606
+ ...validateTargetDependencies(
607
+ rawTarget,
608
+ `${packagePath}: nx.targets.${targetName}`,
609
+ resolvedProjectTargetNames(resolvedProject),
610
+ ),
611
+ );
612
+ }
613
+ return issues;
614
+ }
615
+
616
+ type PlatformTargetFamily = 'macos' | 'linux';
617
+
618
+ function platformTargetFamily(targetName: string): PlatformTargetFamily | null {
619
+ if (MACOS_PLATFORM_TARGET_GLOBS.some((glob) => targetName.endsWith(glob.slice(1)))) {
620
+ return 'macos';
621
+ }
622
+ if (LINUX_PLATFORM_TARGET_GLOBS.some((glob) => targetName.endsWith(glob.slice(1)))) {
623
+ return 'linux';
624
+ }
625
+ return null;
626
+ }
627
+
628
+ function matchingResolvedTargets(dependency: string, targets: ReadonlySet<string>): string[] {
629
+ if (!dependency.startsWith('*')) {
630
+ return targets.has(dependency) ? [dependency] : [];
631
+ }
632
+ const suffix = dependency.slice(1);
633
+ return [...targets].filter((targetName) => targetName.endsWith(suffix));
634
+ }
635
+
636
+ function validatePlatformTargetDependencies(
637
+ packagePath: string,
638
+ resolvedProject?: ReadonlySet<string> | ResolvedProjectTargets,
639
+ ): NxPolicyIssue[] {
640
+ if (!isResolvedProjectTargets(resolvedProject) || !resolvedProject.targetDependencies) {
641
+ return [];
642
+ }
643
+ const issues: NxPolicyIssue[] = [];
644
+ for (const platformTarget of resolvedProject.targets) {
645
+ const family = platformTargetFamily(platformTarget);
646
+ if (!family) {
647
+ continue;
648
+ }
649
+ const visited = new Set<string>();
650
+ const pending = [platformTarget];
651
+ while (pending.length > 0) {
652
+ const targetName = pending.pop();
653
+ if (!targetName || visited.has(targetName)) {
654
+ continue;
655
+ }
656
+ visited.add(targetName);
657
+ for (const dependency of resolvedProject.targetDependencies.get(targetName) ?? []) {
658
+ if (dependency.startsWith('^')) {
659
+ continue;
660
+ }
661
+ for (const resolvedDependency of matchingResolvedTargets(dependency, resolvedProject.targets)) {
662
+ if (platformTargetFamily(resolvedDependency) !== family) {
663
+ issues.push({
664
+ path: packagePath,
665
+ message:
666
+ `${packagePath}: platform target ${platformTarget} dependency closure references ` +
667
+ `non-${family} sibling target ${resolvedDependency} via ${targetName}`,
668
+ });
669
+ continue;
670
+ }
671
+ pending.push(resolvedDependency);
672
+ }
673
+ }
674
+ }
630
675
  }
631
676
  return issues;
632
677
  }
@@ -889,18 +934,6 @@ function directoryContainsTestFilesTree(tree: Tree, path: string): boolean {
889
934
  return false;
890
935
  }
891
936
 
892
- function validateBuildZigPolicyTree(tree: Tree, packagePath: string): NxPolicyIssue[] {
893
- const path = `${packagePath}/build.zig`;
894
- if (!tree.exists(path)) {
895
- return [];
896
- }
897
- const content = tree.read(path, 'utf-8');
898
- if (content && /\bb\.step\s*\(/.test(content)) {
899
- return [];
900
- }
901
- return [{ path: packagePath, message: `${packagePath}/build.zig must define at least one b.step(...) target` }];
902
- }
903
-
904
937
  function validateTestEntrypointPresenceTree(
905
938
  tree: Tree,
906
939
  packagePath: string,
@@ -933,9 +966,8 @@ export function checkPackageTargetPolicyTree(tree: Tree, options: PackageTargetP
933
966
  const resolvedProject = options.resolvedTargetsByProject?.get(projectName);
934
967
  const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
935
968
 
936
- issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedTargets));
969
+ issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedProject));
937
970
  issues.push(...validateTestEntrypointPresenceTree(tree, packagePath, pkg));
938
- issues.push(...validateBuildZigPolicyTree(tree, packagePath));
939
971
  issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets }));
940
972
  }
941
973
 
@@ -1156,11 +1188,9 @@ export function checkPackageTargetPolicy(root: string, options: PackageTargetPol
1156
1188
  const packagePath = packagePathFromJsonPath(root, packageJsonPath);
1157
1189
  const projectName = packageNxProjectName(pkg);
1158
1190
  const resolvedProject = projectName ? options.resolvedTargetsByProject?.get(projectName) : undefined;
1159
- const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
1160
1191
 
1161
- issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedTargets));
1192
+ issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedProject));
1162
1193
  issues.push(...validateTestEntrypointPresence(root, packagePath, pkg));
1163
- issues.push(...validateBuildZigPolicy(root, packagePath));
1164
1194
  issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets: resolvedProject }));
1165
1195
  }
1166
1196
 
@@ -19,10 +19,12 @@ const afterAllProjectsVersioned: AfterAllProjectsVersioned = async (cwd, options
19
19
  // - https://github.com/oven-sh/bun/issues/18906
20
20
  // - https://github.com/oven-sh/bun/issues/20477
21
21
  // - https://github.com/oven-sh/bun/issues/20829
22
- // Nx runs `bun install --lockfile-only`, but Bun currently leaves workspace
23
- // versions stale in bun.lock. `bun pm pack` then rewrites `workspace:*` using
24
- // those stale lockfile versions instead of the current package.json versions.
25
- const updated = syncBunLockfileVersions(cwd);
22
+ //
23
+ // After version bumps, rewrite bun.lock for pack/publish:
24
+ // - stable package.json versions stay as-is
25
+ // - unpublished package.json prereleases (prepare-next) map to last stable tag
26
+ // so a releasing package does not embed unpublishable -next deps via workspace:*
27
+ const updated = syncBunLockfileVersionsForPublish(cwd);
26
28
  if (updated === 0) {
27
29
  return result;
28
30
  }
@@ -37,7 +39,7 @@ baseVersionActions.afterAllProjectsVersioned = afterAllProjectsVersioned;
37
39
 
38
40
  export = baseVersionActions;
39
41
 
40
- function syncBunLockfileVersions(root: string): number {
42
+ function syncBunLockfileVersionsForPublish(root: string): number {
41
43
  const lockfilePath = join(root, 'bun.lock');
42
44
  if (!existsSync(lockfilePath)) {
43
45
  throw new Error('bun.lock not found');
@@ -46,13 +48,7 @@ function syncBunLockfileVersions(root: string): number {
46
48
  let lockfile = readFileSync(lockfilePath, 'utf8');
47
49
  let updated = 0;
48
50
  for (const pkg of packages) {
49
- // If the package.json version is a prerelease (e.g. 0.2.2-next.0) it may
50
- // never have been published. Use the latest stable git tag version so
51
- // that `bun pm pack` writes an installable dependency range for consumers.
52
- const targetVersion = pkg.version.includes('-')
53
- ? (latestStableTagVersion(root, pkg.projectName) ?? pkg.version)
54
- : pkg.version;
55
-
51
+ const targetVersion = publishLockfileVersion(root, pkg.projectName, pkg.version);
56
52
  const relativePath = pkg.path.replaceAll('\\', '/');
57
53
  const escaped = escapeRegex(relativePath);
58
54
  const pattern = new RegExp(`("${escaped}":\\s*\\{[^}]*"version":\\s*")([^"]+)(")`);
@@ -67,9 +63,8 @@ function syncBunLockfileVersions(root: string): number {
67
63
  continue;
68
64
  }
69
65
  lockfile = lockfile.replace(pattern, `$1${targetVersion}$3`);
70
- console.log(
71
- `fix: ${relativePath}: ${lockVersion} -> ${targetVersion}${targetVersion !== pkg.version ? ` (latest stable tag; package.json has ${pkg.version})` : ''}`,
72
- );
66
+ const suffix = targetVersion !== pkg.version ? ` (latest stable tag; package.json has ${pkg.version})` : '';
67
+ console.log(`fix: ${relativePath}: ${lockVersion} -> ${targetVersion}${suffix}`);
73
68
  updated++;
74
69
  }
75
70
  if (updated > 0) {
@@ -81,6 +76,35 @@ function syncBunLockfileVersions(root: string): number {
81
76
  return updated;
82
77
  }
83
78
 
79
+ function publishLockfileVersion(root: string, projectName: string, packageVersion: string): string {
80
+ if (!packageVersion.includes('-')) {
81
+ return packageVersion;
82
+ }
83
+ return latestStableTagVersion(root, projectName) ?? packageVersion;
84
+ }
85
+
86
+ function latestStableTagVersion(root: string, projectName: string): string | null {
87
+ try {
88
+ const output = execSync(`git tag --list '${projectName}@*' --sort=-v:refname`, {
89
+ cwd: root,
90
+ encoding: 'utf8',
91
+ stdio: ['pipe', 'pipe', 'pipe'],
92
+ });
93
+ const prefix = `${projectName}@`;
94
+ for (const line of output.split('\n')) {
95
+ const tag = line.trim();
96
+ if (!tag.startsWith(prefix)) continue;
97
+ const version = tag.slice(prefix.length);
98
+ if (version && !version.includes('-')) {
99
+ return version;
100
+ }
101
+ }
102
+ return null;
103
+ } catch {
104
+ return null;
105
+ }
106
+ }
107
+
84
108
  interface WorkspacePackage {
85
109
  path: string;
86
110
  name: string;
@@ -117,25 +141,3 @@ function isRecord(value: unknown): value is Record<string, unknown> {
117
141
  function escapeRegex(value: string): string {
118
142
  return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
119
143
  }
120
-
121
- function latestStableTagVersion(root: string, projectName: string): string | null {
122
- try {
123
- const output = execSync(`git tag --list '${projectName}@*' --sort=-v:refname`, {
124
- cwd: root,
125
- encoding: 'utf8',
126
- stdio: ['pipe', 'pipe', 'pipe'],
127
- });
128
- const prefix = `${projectName}@`;
129
- for (const line of output.split('\n')) {
130
- const tag = line.trim();
131
- if (!tag.startsWith(prefix)) continue;
132
- const version = tag.slice(prefix.length);
133
- if (version && !version.includes('-')) {
134
- return version;
135
- }
136
- }
137
- return null;
138
- } catch {
139
- return null;
140
- }
141
- }
@@ -10,9 +10,13 @@ import {
10
10
  applyWorkspaceConfig,
11
11
  applyWorkspaceConfigPolicy,
12
12
  applyWorkspaceConfigTree,
13
+ BUILD_OUTPUT_DEPENDENCIES,
13
14
  checkWorkspaceConfig,
14
15
  checkWorkspaceConfigPolicy,
15
16
  checkWorkspaceConfigTree,
17
+ LINUX_PLATFORM_TARGET_GLOBS,
18
+ MACOS_PLATFORM_TARGET_GLOBS,
19
+ PLATFORM_TARGET_GLOBS,
16
20
  } from './workspace-config-policy.js';
17
21
 
18
22
  // ---------------------------------------------------------------------------
@@ -56,6 +60,25 @@ function validPlugins(): unknown[] {
56
60
  // Layer 1: Pure core function tests
57
61
  // ---------------------------------------------------------------------------
58
62
 
63
+ describe('target family conventions', () => {
64
+ it('separates platform-only families from ordinary build outputs', () => {
65
+ expect(MACOS_PLATFORM_TARGET_GLOBS).toEqual(['*-macos', '*-ios']);
66
+ expect(LINUX_PLATFORM_TARGET_GLOBS).toEqual(['*-linux']);
67
+ expect(PLATFORM_TARGET_GLOBS).toEqual(['*-macos', '*-ios', '*-linux']);
68
+ expect(BUILD_OUTPUT_DEPENDENCIES).toEqual([
69
+ '*-js',
70
+ '*-web',
71
+ '*-html',
72
+ '*-css',
73
+ '*-android',
74
+ '*-native',
75
+ '*-napi',
76
+ '*-bun',
77
+ '*-wasm',
78
+ ]);
79
+ });
80
+ });
81
+
59
82
  describe('pure core: checkWorkspaceConfig', () => {
60
83
  it('returns no issues for valid config', () => {
61
84
  const issues = checkWorkspaceConfig(validNxJson());
@@ -13,13 +13,16 @@ export const BUILD_OUTPUT_DEPENDENCIES = [
13
13
  '*-web',
14
14
  '*-html',
15
15
  '*-css',
16
- '*-ios',
17
16
  '*-android',
18
17
  '*-native',
19
18
  '*-napi',
20
19
  '*-bun',
21
20
  '*-wasm',
22
- ];
21
+ ] as const;
22
+
23
+ export const MACOS_PLATFORM_TARGET_GLOBS = ['*-macos', '*-ios'] as const;
24
+ export const LINUX_PLATFORM_TARGET_GLOBS = ['*-linux'] as const;
25
+ export const PLATFORM_TARGET_GLOBS = [...MACOS_PLATFORM_TARGET_GLOBS, ...LINUX_PLATFORM_TARGET_GLOBS] as const;
23
26
 
24
27
  const nxJsTypescriptPlugin = '@nx/js/typescript';
25
28
  const smoothBricksNxPlugin = '@smoothbricks/nx-plugin';