@smoothbricks/nx-plugin 0.0.0-bootstrap.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +93 -2
  2. package/dist/bounded-test-policy.d.ts +32 -0
  3. package/dist/bounded-test-policy.d.ts.map +1 -0
  4. package/dist/bounded-test-policy.js +184 -0
  5. package/dist/executors/bounded-exec/executor.d.ts +15 -0
  6. package/dist/executors/bounded-exec/executor.d.ts.map +1 -0
  7. package/dist/executors/bounded-exec/executor.js +179 -0
  8. package/dist/executors/bounded-exec/schema.d.ts +11 -0
  9. package/dist/executors/bounded-exec/schema.d.ts.map +1 -0
  10. package/dist/executors/bounded-exec/schema.js +1 -0
  11. package/dist/generators/bounded-test-targets/generator.d.ts +7 -0
  12. package/dist/generators/bounded-test-targets/generator.d.ts.map +1 -0
  13. package/dist/generators/bounded-test-targets/generator.js +40 -0
  14. package/dist/generators/bun-test-tracing/generator.d.ts +10 -0
  15. package/dist/generators/bun-test-tracing/generator.d.ts.map +1 -0
  16. package/dist/generators/bun-test-tracing/generator.js +247 -0
  17. package/dist/generators/create-package/generator.d.ts +4 -0
  18. package/dist/generators/create-package/generator.d.ts.map +1 -0
  19. package/dist/generators/create-package/generator.js +204 -0
  20. package/dist/generators/make-public/generator.d.ts +4 -0
  21. package/dist/generators/make-public/generator.d.ts.map +1 -0
  22. package/dist/generators/make-public/generator.js +55 -0
  23. package/dist/index.d.ts +7 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +197 -0
  26. package/dist/package-target-policy.d.ts +31 -0
  27. package/dist/package-target-policy.d.ts.map +1 -0
  28. package/dist/package-target-policy.js +930 -0
  29. package/dist/release-config-policy.d.ts +32 -0
  30. package/dist/release-config-policy.d.ts.map +1 -0
  31. package/dist/release-config-policy.js +225 -0
  32. package/dist/typecheck-test-policy.d.ts +56 -0
  33. package/dist/typecheck-test-policy.d.ts.map +1 -0
  34. package/dist/typecheck-test-policy.js +640 -0
  35. package/dist/version-actions.cjs +78 -0
  36. package/dist/version-actions.d.cts +6 -0
  37. package/dist/version-actions.d.cts.map +1 -0
  38. package/dist/workspace-config-policy.d.ts +33 -0
  39. package/dist/workspace-config-policy.d.ts.map +1 -0
  40. package/dist/workspace-config-policy.js +334 -0
  41. package/executors.json +9 -0
  42. package/generators.json +24 -0
  43. package/loader.js +16 -0
  44. package/package.json +89 -4
  45. package/src/bounded-test-policy.test.ts +257 -0
  46. package/src/bounded-test-policy.ts +257 -0
  47. package/src/bun-bootstrap.test.ts +52 -0
  48. package/src/executors/bounded-exec/executor.test.ts +258 -0
  49. package/src/executors/bounded-exec/executor.ts +228 -0
  50. package/src/executors/bounded-exec/schema.json +68 -0
  51. package/src/executors/bounded-exec/schema.ts +10 -0
  52. package/src/generators/bounded-test-targets/generator.test.ts +136 -0
  53. package/src/generators/bounded-test-targets/generator.ts +67 -0
  54. package/src/generators/bounded-test-targets/schema.d.ts +3 -0
  55. package/src/generators/bounded-test-targets/schema.json +14 -0
  56. package/src/generators/bun-test-tracing/generator.test.ts +243 -0
  57. package/src/generators/bun-test-tracing/generator.ts +348 -0
  58. package/src/generators/bun-test-tracing/schema.d.ts +8 -0
  59. package/src/generators/bun-test-tracing/schema.json +28 -0
  60. package/src/generators/create-package/generator.test.ts +206 -0
  61. package/src/generators/create-package/generator.ts +251 -0
  62. package/src/generators/create-package/schema.d.ts +5 -0
  63. package/src/generators/create-package/schema.json +24 -0
  64. package/src/generators/make-public/generator.test.ts +137 -0
  65. package/src/generators/make-public/generator.ts +83 -0
  66. package/src/generators/make-public/schema.d.ts +3 -0
  67. package/src/generators/make-public/schema.json +13 -0
  68. package/src/index.test.ts +200 -0
  69. package/src/index.ts +242 -0
  70. package/src/package-target-policy.test.ts +778 -0
  71. package/src/package-target-policy.ts +1127 -0
  72. package/src/release-config-policy.test.ts +280 -0
  73. package/src/release-config-policy.ts +258 -0
  74. package/src/typecheck-test-policy.test.ts +797 -0
  75. package/src/typecheck-test-policy.ts +752 -0
  76. package/src/version-actions.cts +94 -0
  77. package/src/workspace-config-policy.test.ts +376 -0
  78. package/src/workspace-config-policy.ts +390 -0
@@ -0,0 +1,83 @@
1
+ // Promote a private package to npm publication. Invoked by `smoo g make-public <name>`.
2
+ // CLI wiring: packages/cli/src/generate/index.ts (variant registry)
3
+ import {
4
+ getProjects,
5
+ joinPathFragments,
6
+ normalizePath,
7
+ readJson,
8
+ type Tree,
9
+ updateJson,
10
+ } from 'nx/src/devkit-exports.js';
11
+
12
+ import type { MakePublicGeneratorSchema } from './schema.js';
13
+
14
+ export default async function generator(tree: Tree, schema: MakePublicGeneratorSchema): Promise<void> {
15
+ const resolved = resolveProject(tree, schema.project);
16
+ const packageJsonPath = joinPathFragments(resolved.root, 'package.json');
17
+
18
+ if (!tree.exists(packageJsonPath)) {
19
+ throw new Error(`Project ${resolved.name} is missing ${packageJsonPath}`);
20
+ }
21
+
22
+ const rootPkg = readJson<{ name?: string; license?: string; repository?: Record<string, unknown> }>(
23
+ tree,
24
+ 'package.json',
25
+ );
26
+
27
+ updateJson(tree, packageJsonPath, (pkg: Record<string, unknown>) => {
28
+ delete pkg.private;
29
+
30
+ if (!pkg.license && rootPkg.license) {
31
+ pkg.license = rootPkg.license;
32
+ }
33
+
34
+ pkg.publishConfig = { access: 'public' };
35
+
36
+ if (rootPkg.repository) {
37
+ pkg.repository = {
38
+ ...(typeof rootPkg.repository === 'object' ? rootPkg.repository : {}),
39
+ directory: resolved.root,
40
+ };
41
+ }
42
+
43
+ const nx = (pkg.nx ?? {}) as Record<string, unknown>;
44
+ const tags = Array.isArray(nx.tags) ? [...nx.tags] : [];
45
+ if (!tags.includes('npm:public')) {
46
+ tags.push('npm:public');
47
+ }
48
+ nx.tags = tags;
49
+ pkg.nx = nx;
50
+
51
+ if (pkg.version === '0.0.0') {
52
+ pkg.version = '0.1.0';
53
+ }
54
+
55
+ return pkg;
56
+ });
57
+ }
58
+
59
+ function resolveProject(tree: Tree, projectInput: string): { name: string; root: string } {
60
+ const normalizedInput = normalizeLookupValue(projectInput);
61
+
62
+ for (const [name, config] of getProjects(tree)) {
63
+ if (normalizeLookupValue(name) === normalizedInput || normalizeLookupValue(config.root) === normalizedInput) {
64
+ return { name, root: config.root };
65
+ }
66
+
67
+ const packageJsonPath = joinPathFragments(config.root, 'package.json');
68
+ if (!tree.exists(packageJsonPath)) {
69
+ continue;
70
+ }
71
+
72
+ const packageJson = readJson<{ name?: string }>(tree, packageJsonPath);
73
+ if (packageJson.name && normalizeLookupValue(packageJson.name) === normalizedInput) {
74
+ return { name, root: config.root };
75
+ }
76
+ }
77
+
78
+ throw new Error(`Could not resolve project ${projectInput}`);
79
+ }
80
+
81
+ function normalizeLookupValue(value: string): string {
82
+ return normalizePath(value.replace(/^\.\//, ''));
83
+ }
@@ -0,0 +1,3 @@
1
+ export interface MakePublicGeneratorSchema {
2
+ project: string;
3
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "$schema": "http://json-schema.org/schema",
3
+ "id": "MakePublic",
4
+ "title": "Promote a private package to public npm publication",
5
+ "type": "object",
6
+ "properties": {
7
+ "project": {
8
+ "type": "string",
9
+ "description": "Nx project name, package name, or project root to promote."
10
+ }
11
+ },
12
+ "required": ["project"]
13
+ }
@@ -0,0 +1,200 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
3
+ import { tmpdir } from 'node:os';
4
+ import { dirname, join } from 'node:path';
5
+
6
+ import type { CreateNodesContextV2, TargetConfiguration } from 'nx/src/devkit-exports.js';
7
+ import { createNodesV2 } from './index.js';
8
+
9
+ const [, inferTargets] = createNodesV2;
10
+ const buildOutputDependencies = [
11
+ '^build',
12
+ '*-js',
13
+ '*-web',
14
+ '*-html',
15
+ '*-css',
16
+ '*-ios',
17
+ '*-android',
18
+ '*-native',
19
+ '*-napi',
20
+ '*-bun',
21
+ '*-wasm',
22
+ ];
23
+
24
+ describe('@smoothbricks/nx-plugin inferred targets', () => {
25
+ it('infers validation and aggregate build targets without owning TypeScript lib build', async () => {
26
+ const workspace = await createWorkspace();
27
+ try {
28
+ await workspace.write(
29
+ 'packages/example/package.json',
30
+ '{"name":"example","scripts":{"test":"bun test --pass-with-no-tests"}}\n',
31
+ );
32
+ await workspace.write('packages/example/tsconfig.lib.json', '{}\n');
33
+ await workspace.write('packages/example/tsconfig.test.json', '{}\n');
34
+
35
+ const targets = await inferProjectTargets(workspace, 'packages/example/package.json');
36
+
37
+ expect(targets['tsc-js']).toBeUndefined();
38
+ expect(targets.build?.executor).toBe('nx:noop');
39
+ expect(targets.build?.cache).toBe(true);
40
+ expect(targets.build?.dependsOn).toEqual(buildOutputDependencies);
41
+
42
+ expect(targets['typecheck-tests']?.executor).toBe('nx:run-commands');
43
+ expect(targets['typecheck-tests']?.cache).toBe(true);
44
+ expect(targets['typecheck-tests']?.dependsOn).toEqual(['typecheck']);
45
+ expect(targets['typecheck-tests']?.options).toMatchObject({
46
+ command: 'tsc --noEmit -p tsconfig.test.json',
47
+ cwd: 'packages/example',
48
+ });
49
+
50
+ expect(targets['typecheck-tests:watch']?.executor).toBe('nx:run-commands');
51
+ expect(targets['typecheck-tests:watch']?.continuous).toBe(true);
52
+ expect(targets['typecheck-tests:watch']?.options).toMatchObject({
53
+ command: 'tsc --noEmit -p tsconfig.test.json --watch',
54
+ cwd: 'packages/example',
55
+ });
56
+
57
+ expect(targets['test:watch']?.executor).toBe('nx:run-commands');
58
+ expect(targets['test:watch']?.continuous).toBe(true);
59
+ expect(targets['test:watch']?.dependsOn).toEqual(['typecheck-tests']);
60
+ expect(targets['test:watch']?.options).toMatchObject({
61
+ command: 'bun test --watch --pass-with-no-tests',
62
+ cwd: 'packages/example',
63
+ });
64
+
65
+ expect(targets.lint?.executor).toBe('nx:noop');
66
+ expect(targets.lint?.cache).toBe(true);
67
+ expect(targets.lint?.dependsOn).toEqual(['typecheck-tests']);
68
+ } finally {
69
+ await workspace.cleanup();
70
+ }
71
+ });
72
+
73
+ it('infers vitest watch targets from explicit test commands', async () => {
74
+ const workspace = await createWorkspace();
75
+ try {
76
+ await workspace.write(
77
+ 'packages/example/package.json',
78
+ '{"name":"example","scripts":{"test":"vitest run --coverage"}}\n',
79
+ );
80
+ await workspace.write('packages/example/tsconfig.test.json', '{}\n');
81
+
82
+ const targets = await inferProjectTargets(workspace, 'packages/example/package.json');
83
+
84
+ expect(targets['test:watch']?.continuous).toBe(true);
85
+ expect(targets['test:watch']?.options).toMatchObject({
86
+ command: 'vitest --coverage',
87
+ cwd: 'packages/example',
88
+ });
89
+ } finally {
90
+ await workspace.cleanup();
91
+ }
92
+ });
93
+
94
+ it('infers zig step targets and excludes reserved non-build steps', async () => {
95
+ const workspace = await createWorkspace();
96
+ try {
97
+ await workspace.write('packages/ziggy/package.json', '{"name":"ziggy"}\n');
98
+ await workspace.write(
99
+ 'packages/ziggy/build.zig',
100
+ [
101
+ 'pub fn build(b: *std.Build) void {',
102
+ ' _ = b.step("wasm", "Build wasm");',
103
+ ' _ = b.step("native", "Build native");',
104
+ ' _ = b.step("test", "Run tests");',
105
+ ' _ = b.step("all", "Build everything");',
106
+ ' _ = b.step("clean", "Clean outputs");',
107
+ ' _ = b.step("install", "Install artifacts");',
108
+ '}',
109
+ '',
110
+ ].join('\n'),
111
+ );
112
+
113
+ const targets = await inferProjectTargets(workspace, 'packages/ziggy/package.json');
114
+
115
+ expect(Object.keys(targets).sort()).toEqual(['build', 'zig-native', 'zig-wasm']);
116
+ expect(targets.build?.dependsOn).toEqual(buildOutputDependencies);
117
+ expect(targets.build?.cache).toBe(true);
118
+ expect(targets['zig-wasm']?.cache).toBe(true);
119
+ expect(targets['zig-native']?.cache).toBe(true);
120
+ expect(targets['zig-wasm']?.options).toEqual({
121
+ command: 'zig build wasm',
122
+ cwd: 'packages/ziggy',
123
+ });
124
+ expect(targets['zig-native']?.options).toEqual({
125
+ command: 'zig build native',
126
+ cwd: 'packages/ziggy',
127
+ });
128
+ } finally {
129
+ await workspace.cleanup();
130
+ }
131
+ });
132
+
133
+ it('infers aggregate build for package-local output targets without owning them', async () => {
134
+ const workspace = await createWorkspace();
135
+ try {
136
+ await workspace.write(
137
+ 'packages/tsdown/package.json',
138
+ JSON.stringify({ name: 'tsdown', nx: { targets: { 'tsdown-js': { executor: 'nx:run-commands' } } } }),
139
+ );
140
+
141
+ const targets = await inferProjectTargets(workspace, 'packages/tsdown/package.json');
142
+
143
+ expect(targets['tsc-js']).toBeUndefined();
144
+ expect(targets['tsdown-js']).toBeUndefined();
145
+ expect(targets.build?.executor).toBe('nx:noop');
146
+ expect(targets.build?.dependsOn).toEqual(buildOutputDependencies);
147
+ } finally {
148
+ await workspace.cleanup();
149
+ }
150
+ });
151
+
152
+ it('leaves build.zig without b.step declarations to smoo validation', async () => {
153
+ const workspace = await createWorkspace();
154
+ try {
155
+ await workspace.write('packages/broken/package.json', '{"name":"broken"}\n');
156
+ await workspace.write('packages/broken/build.zig', 'pub fn build(_: *std.Build) void {}\n');
157
+
158
+ const targets = await inferProjectTargets(workspace, 'packages/broken/package.json');
159
+
160
+ expect(targets).toEqual({});
161
+ } finally {
162
+ await workspace.cleanup();
163
+ }
164
+ });
165
+ });
166
+
167
+ async function createWorkspace(): Promise<WorkspaceFixture> {
168
+ const root = await mkdtemp(join(tmpdir(), 'smoothbricks-nx-plugin-'));
169
+
170
+ return {
171
+ context: {
172
+ workspaceRoot: root,
173
+ nxJsonConfiguration: {},
174
+ },
175
+ async write(filePath: string, contents: string): Promise<void> {
176
+ const absolutePath = join(root, filePath);
177
+ await mkdir(dirname(absolutePath), { recursive: true });
178
+ await writeFile(absolutePath, contents);
179
+ },
180
+ async cleanup(): Promise<void> {
181
+ await rm(root, { recursive: true, force: true });
182
+ },
183
+ };
184
+ }
185
+
186
+ interface WorkspaceFixture {
187
+ context: CreateNodesContextV2;
188
+ write(filePath: string, contents: string): Promise<void>;
189
+ cleanup(): Promise<void>;
190
+ }
191
+
192
+ async function inferProjectTargets(
193
+ workspace: WorkspaceFixture,
194
+ packageJsonPath: string,
195
+ ): Promise<Record<string, TargetConfiguration>> {
196
+ const result = await inferTargets([packageJsonPath], undefined, workspace.context);
197
+ const projectRoot = dirname(packageJsonPath);
198
+
199
+ return result[0]?.[1].projects?.[projectRoot]?.targets ?? {};
200
+ }
package/src/index.ts ADDED
@@ -0,0 +1,242 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { dirname, join } from 'node:path';
4
+
5
+ import type { CreateNodesResultV2, CreateNodesV2, TargetConfiguration } from 'nx/src/devkit-exports.js';
6
+ import { AggregateCreateNodesError } from 'nx/src/project-graph/error-types.js';
7
+
8
+ import { BUILD_OUTPUT_DEPENDENCIES } from './workspace-config-policy.js';
9
+
10
+ const RESERVED_ZIG_STEPS = new Set(['all', 'clean', 'install', 'test']);
11
+ const ZIG_STEP_PATTERN = /\bb\.step\(\s*["']([^"']+)["']\s*,/g;
12
+ const VALID_ZIG_STEP_NAME = /^[A-Za-z0-9_-]+$/;
13
+ const BUILD_OUTPUT_TARGET_PATTERN = /-(?:js|web|html|css|ios|android|native|napi|bun|wasm)$/;
14
+
15
+ export const createNodesV2: CreateNodesV2 = [
16
+ '**/package.json',
17
+ async (projectConfigurationFiles, _options, context) => {
18
+ const results: CreateNodesResultV2 = [];
19
+ const errors: Array<[file: string | null, error: Error]> = [];
20
+
21
+ await Promise.all(
22
+ projectConfigurationFiles.map(async (packageJsonPath) => {
23
+ try {
24
+ results.push([packageJsonPath, await createProjectTargets(packageJsonPath, context.workspaceRoot)]);
25
+ } catch (error) {
26
+ errors.push([packageJsonPath, error instanceof Error ? error : new Error(String(error))]);
27
+ }
28
+ }),
29
+ );
30
+
31
+ if (errors.length > 0) {
32
+ throw new AggregateCreateNodesError(errors, results);
33
+ }
34
+
35
+ return results;
36
+ },
37
+ ];
38
+
39
+ export default { createNodesV2 };
40
+
41
+ interface PackageJson {
42
+ scripts?: Record<string, unknown>;
43
+ nx?: {
44
+ targets?: Record<string, unknown>;
45
+ };
46
+ }
47
+
48
+ async function createProjectTargets(packageJsonPath: string, workspaceRoot: string) {
49
+ const projectRoot = dirname(packageJsonPath);
50
+ const absoluteProjectRoot = join(workspaceRoot, projectRoot);
51
+ const packageJson = await readPackageJson(join(workspaceRoot, packageJsonPath));
52
+ const targets: Record<string, TargetConfiguration> = {};
53
+ const validationTargets: string[] = [];
54
+ const hasLibTsconfig = existsSync(join(absoluteProjectRoot, 'tsconfig.lib.json'));
55
+ let hasBuildOutputTarget = hasLibTsconfig || hasPackageLocalBuildOutputTarget(packageJson);
56
+
57
+ const hasTestTsconfig = existsSync(join(absoluteProjectRoot, 'tsconfig.test.json'));
58
+ if (hasTestTsconfig) {
59
+ targets['typecheck-tests'] = {
60
+ executor: 'nx:run-commands',
61
+ cache: true,
62
+ options: {
63
+ command: 'tsc --noEmit -p tsconfig.test.json',
64
+ cwd: projectRoot,
65
+ },
66
+ };
67
+ targets['typecheck-tests:watch'] = {
68
+ executor: 'nx:run-commands',
69
+ continuous: true,
70
+ options: {
71
+ command: 'tsc --noEmit -p tsconfig.test.json --watch',
72
+ cwd: projectRoot,
73
+ },
74
+ };
75
+ const inferredTestWatchCommand = inferTestWatchCommand(packageJson);
76
+ if (inferredTestWatchCommand) {
77
+ targets['test:watch'] = {
78
+ executor: 'nx:run-commands',
79
+ continuous: true,
80
+ dependsOn: ['typecheck-tests'],
81
+ options: {
82
+ command: inferredTestWatchCommand,
83
+ cwd: projectRoot,
84
+ },
85
+ };
86
+ }
87
+ validationTargets.push('typecheck-tests');
88
+ } else if (hasLibTsconfig) {
89
+ validationTargets.push('typecheck');
90
+ }
91
+
92
+ const zigSteps = await readZigSteps(absoluteProjectRoot, projectRoot);
93
+ for (const step of zigSteps) {
94
+ const targetName = `zig-${step}`;
95
+ targets[targetName] = {
96
+ executor: 'nx:run-commands',
97
+ cache: true,
98
+ inputs: ['{projectRoot}/src/**/*.zig', '{projectRoot}/build.zig', '{projectRoot}/build.zig.zon'],
99
+ outputs: [
100
+ '{projectRoot}/dist/**/*.wasm',
101
+ '{projectRoot}/dist/**/*.node',
102
+ '{projectRoot}/dist/**/*.dylib',
103
+ '{projectRoot}/dist/**/*.so',
104
+ '{projectRoot}/dist/**/*.dll',
105
+ '{projectRoot}/dist/**/*.a',
106
+ ],
107
+ options: {
108
+ command: `zig build ${step}`,
109
+ cwd: projectRoot,
110
+ },
111
+ };
112
+ hasBuildOutputTarget = true;
113
+ }
114
+
115
+ if (hasBuildOutputTarget) {
116
+ targets.build = {
117
+ executor: 'nx:noop',
118
+ cache: true,
119
+ dependsOn: ['^build', ...BUILD_OUTPUT_DEPENDENCIES],
120
+ };
121
+ }
122
+
123
+ if (targets['typecheck-tests']) {
124
+ if (hasLibTsconfig) {
125
+ targets['typecheck-tests'].dependsOn = ['typecheck'];
126
+ } else if (targets.build) {
127
+ targets['typecheck-tests'].dependsOn = ['build'];
128
+ }
129
+ }
130
+
131
+ if (validationTargets.length > 0) {
132
+ targets.lint = {
133
+ executor: 'nx:noop',
134
+ cache: true,
135
+ dependsOn: validationTargets,
136
+ };
137
+ }
138
+
139
+ return {
140
+ projects: {
141
+ [projectRoot]: { targets },
142
+ },
143
+ };
144
+ }
145
+
146
+ async function readPackageJson(packageJsonPath: string): Promise<PackageJson> {
147
+ return JSON.parse(await readFile(packageJsonPath, 'utf-8')) as PackageJson;
148
+ }
149
+
150
+ function inferTestWatchCommand(packageJson: PackageJson): string | null {
151
+ const scriptCommand = packageJson.scripts?.test;
152
+ if (typeof scriptCommand === 'string') {
153
+ const watchCommand = watchCommandFromTestCommand(scriptCommand);
154
+ if (watchCommand) {
155
+ return watchCommand;
156
+ }
157
+ }
158
+
159
+ const target = packageJson.nx?.targets?.test;
160
+ if (!isRecord(target)) {
161
+ return null;
162
+ }
163
+
164
+ const options = target.options;
165
+ if (!isRecord(options) || typeof options.command !== 'string') {
166
+ return null;
167
+ }
168
+
169
+ return watchCommandFromTestCommand(options.command);
170
+ }
171
+
172
+ function watchCommandFromTestCommand(command: string): string | null {
173
+ const parsed = parseEnvPrefixedCommand(command);
174
+ const trimmed = parsed.command.trim();
175
+
176
+ if (/^bun\s+test(?:\s|$)/.test(trimmed)) {
177
+ const suffix = trimmed.slice(trimmed.indexOf('test') + 'test'.length).trim();
178
+ return `${parsed.envPrefix}bun test --watch${suffix ? ` ${suffix}` : ''}`;
179
+ }
180
+
181
+ if (/^vitest(?:\s+run|\s+--run)?(?:\s|$)/.test(trimmed)) {
182
+ const suffix = trimmed.replace(/^vitest(?:\s+run|\s+--run)?/, '').trim();
183
+ return `${parsed.envPrefix}vitest${suffix ? ` ${suffix}` : ''}`;
184
+ }
185
+
186
+ return null;
187
+ }
188
+
189
+ function parseEnvPrefixedCommand(command: string): { command: string; envPrefix: string } {
190
+ const match = /^(?:\s*[A-Za-z_][A-Za-z0-9_]*=(?:"[^"]*"|'[^']*'|\S+)\s+)+/.exec(command);
191
+ if (!match?.[0]) {
192
+ return { command, envPrefix: '' };
193
+ }
194
+ return { command: command.slice(match[0].length), envPrefix: match[0] };
195
+ }
196
+
197
+ function isRecord(value: unknown): value is Record<string, unknown> {
198
+ return value !== null && typeof value === 'object';
199
+ }
200
+
201
+ function hasPackageLocalBuildOutputTarget(packageJson: PackageJson): boolean {
202
+ const targets = packageJson.nx?.targets;
203
+ if (!isRecord(targets)) {
204
+ return false;
205
+ }
206
+
207
+ return Object.keys(targets).some((targetName) => BUILD_OUTPUT_TARGET_PATTERN.test(targetName));
208
+ }
209
+
210
+ async function readZigSteps(absoluteProjectRoot: string, projectRoot: string): Promise<string[]> {
211
+ const buildZigPath = join(absoluteProjectRoot, 'build.zig');
212
+
213
+ if (!existsSync(buildZigPath)) {
214
+ return [];
215
+ }
216
+
217
+ const buildZig = await readFile(buildZigPath, 'utf-8');
218
+ const declaredSteps = Array.from(buildZig.matchAll(ZIG_STEP_PATTERN), (match) => match[1]);
219
+
220
+ if (declaredSteps.length === 0) {
221
+ return [];
222
+ }
223
+
224
+ const steps: string[] = [];
225
+ for (const step of declaredSteps) {
226
+ if (RESERVED_ZIG_STEPS.has(step)) {
227
+ continue;
228
+ }
229
+
230
+ if (!VALID_ZIG_STEP_NAME.test(step)) {
231
+ throw new Error(
232
+ `${projectRoot}/build.zig declares unsupported Zig step "${step}"; inferred target names cannot contain ':' or other special characters`,
233
+ );
234
+ }
235
+
236
+ if (!steps.includes(step)) {
237
+ steps.push(step);
238
+ }
239
+ }
240
+
241
+ return steps;
242
+ }