@smoothbricks/nx-plugin 0.1.0 → 0.2.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 (74) hide show
  1. package/README.md +35 -8
  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.map +1 -1
  15. package/dist/generators/bun-test-tracing/generator.js +5 -4
  16. package/dist/generators/create-package/generator.d.ts +4 -0
  17. package/dist/generators/create-package/generator.d.ts.map +1 -0
  18. package/dist/generators/create-package/generator.js +204 -0
  19. package/dist/generators/make-public/generator.d.ts +4 -0
  20. package/dist/generators/make-public/generator.d.ts.map +1 -0
  21. package/dist/generators/make-public/generator.js +55 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +78 -7
  24. package/dist/package-target-policy.d.ts +31 -0
  25. package/dist/package-target-policy.d.ts.map +1 -0
  26. package/dist/package-target-policy.js +937 -0
  27. package/dist/release-config-policy.d.ts +32 -0
  28. package/dist/release-config-policy.d.ts.map +1 -0
  29. package/dist/release-config-policy.js +225 -0
  30. package/dist/typecheck-test-policy.d.ts +56 -0
  31. package/dist/typecheck-test-policy.d.ts.map +1 -0
  32. package/dist/typecheck-test-policy.js +640 -0
  33. package/dist/version-actions.cjs +78 -0
  34. package/dist/version-actions.d.cts +6 -0
  35. package/dist/version-actions.d.cts.map +1 -0
  36. package/dist/workspace-config-policy.d.ts +33 -0
  37. package/dist/workspace-config-policy.d.ts.map +1 -0
  38. package/dist/workspace-config-policy.js +334 -0
  39. package/executors.json +9 -0
  40. package/generators.json +15 -0
  41. package/package.json +45 -11
  42. package/src/bounded-test-policy.test.ts +257 -0
  43. package/src/bounded-test-policy.ts +257 -0
  44. package/src/bun-bootstrap.test.ts +52 -0
  45. package/src/executors/bounded-exec/executor.test.ts +258 -0
  46. package/src/executors/bounded-exec/executor.ts +228 -0
  47. package/src/executors/bounded-exec/schema.json +68 -0
  48. package/src/executors/bounded-exec/schema.ts +10 -0
  49. package/src/generators/bounded-test-targets/generator.test.ts +136 -0
  50. package/src/generators/bounded-test-targets/generator.ts +67 -0
  51. package/src/generators/bounded-test-targets/schema.d.ts +3 -0
  52. package/src/generators/bounded-test-targets/schema.json +14 -0
  53. package/src/generators/bun-test-tracing/generator.test.ts +25 -1
  54. package/src/generators/bun-test-tracing/generator.ts +6 -5
  55. package/src/generators/create-package/generator.test.ts +206 -0
  56. package/src/generators/create-package/generator.ts +251 -0
  57. package/src/generators/create-package/schema.d.ts +5 -0
  58. package/src/generators/create-package/schema.json +24 -0
  59. package/src/generators/make-public/generator.test.ts +137 -0
  60. package/src/generators/make-public/generator.ts +83 -0
  61. package/src/generators/make-public/schema.d.ts +3 -0
  62. package/src/generators/make-public/schema.json +13 -0
  63. package/src/index.test.ts +85 -18
  64. package/src/index.ts +99 -8
  65. package/src/package-target-policy.test.ts +785 -0
  66. package/src/package-target-policy.ts +1140 -0
  67. package/src/release-config-policy.test.ts +280 -0
  68. package/src/release-config-policy.ts +258 -0
  69. package/src/typecheck-test-policy.test.ts +797 -0
  70. package/src/typecheck-test-policy.ts +752 -0
  71. package/src/version-actions.cts +94 -0
  72. package/src/workspace-config-policy.test.ts +376 -0
  73. package/src/workspace-config-policy.ts +390 -0
  74. package/src/test-suite-tracer.ts +0 -0
@@ -0,0 +1,280 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
3
+ import { tmpdir } from 'node:os';
4
+ import { dirname, join } from 'node:path';
5
+
6
+ import { readJson, writeJson } from 'nx/src/devkit-exports.js';
7
+ import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports.js';
8
+
9
+ import {
10
+ applyReleaseConfig,
11
+ applyReleaseConfigPolicy,
12
+ applyReleaseConfigTree,
13
+ checkReleaseConfig,
14
+ checkReleaseConfigPolicy,
15
+ checkReleaseConfigTree,
16
+ SMOO_NX_RELEASE_TAG_PATTERN,
17
+ SMOO_NX_VERSION_ACTIONS,
18
+ } from './release-config-policy.js';
19
+
20
+ // ---------------------------------------------------------------------------
21
+ // Helpers
22
+ // ---------------------------------------------------------------------------
23
+
24
+ function validReleaseNxJson(): Record<string, unknown> {
25
+ return {
26
+ release: {
27
+ projectsRelationship: 'independent',
28
+ version: {
29
+ specifierSource: 'conventional-commits',
30
+ currentVersionResolver: 'git-tag',
31
+ fallbackCurrentVersionResolver: 'disk',
32
+ versionActions: SMOO_NX_VERSION_ACTIONS,
33
+ },
34
+ releaseTag: {
35
+ pattern: SMOO_NX_RELEASE_TAG_PATTERN,
36
+ },
37
+ changelog: {
38
+ workspaceChangelog: false,
39
+ projectChangelogs: {
40
+ createRelease: false,
41
+ file: false,
42
+ renderOptions: {
43
+ authors: true,
44
+ applyUsernameToAuthors: true,
45
+ },
46
+ },
47
+ },
48
+ },
49
+ };
50
+ }
51
+
52
+ // ---------------------------------------------------------------------------
53
+ // Layer 1: Pure core function tests
54
+ // ---------------------------------------------------------------------------
55
+
56
+ describe('pure core: checkReleaseConfig', () => {
57
+ it('returns no issues for valid config', () => {
58
+ expect(checkReleaseConfig(validReleaseNxJson())).toEqual([]);
59
+ });
60
+
61
+ it('uses nx.json as path in issues', () => {
62
+ const issues = checkReleaseConfig({});
63
+ for (const issue of issues) {
64
+ expect(issue.path).toBe('nx.json');
65
+ }
66
+ });
67
+
68
+ it('detects missing release config', () => {
69
+ const issues = checkReleaseConfig({});
70
+ expect(issues.some((i) => i.message.includes('release config is missing'))).toBe(true);
71
+ });
72
+
73
+ it('detects wrong release config values', () => {
74
+ const issues = checkReleaseConfig({
75
+ release: {
76
+ projectsRelationship: 'fixed',
77
+ version: {
78
+ specifierSource: 'prompt',
79
+ currentVersionResolver: 'disk',
80
+ fallbackCurrentVersionResolver: 'registry',
81
+ versionActions: 'wrong/path',
82
+ preVersionCommand: 'nx run-many -t build',
83
+ },
84
+ releaseTag: {
85
+ pattern: 'v{version}',
86
+ },
87
+ changelog: {
88
+ workspaceChangelog: true,
89
+ projectChangelogs: {
90
+ createRelease: 'github',
91
+ file: '{projectRoot}/CHANGELOG.md',
92
+ renderOptions: {},
93
+ },
94
+ },
95
+ },
96
+ });
97
+ const messages = issues.map((i) => i.message);
98
+ expect(messages).toContainEqual('release.projectsRelationship must be independent');
99
+ expect(messages).toContainEqual('release.version.specifierSource must be conventional-commits');
100
+ expect(messages).toContainEqual('release.version.currentVersionResolver must be git-tag');
101
+ expect(messages).toContainEqual('release.version.fallbackCurrentVersionResolver must be disk');
102
+ expect(messages).toContainEqual(`release.version.versionActions must be ${SMOO_NX_VERSION_ACTIONS}`);
103
+ expect(messages).toContainEqual(
104
+ 'release.version.preVersionCommand must not be defined; smoo builds npm-missing packages before publish',
105
+ );
106
+ expect(messages).toContainEqual(`release.releaseTag.pattern must be ${SMOO_NX_RELEASE_TAG_PATTERN}`);
107
+ expect(messages).toContainEqual('release.changelog.workspaceChangelog must be false');
108
+ expect(messages).toContainEqual('release.changelog.projectChangelogs.createRelease must be false');
109
+ expect(messages).toContainEqual('release.changelog.projectChangelogs.file must be false');
110
+ });
111
+ });
112
+
113
+ describe('pure core: applyReleaseConfig', () => {
114
+ it('returns false for already-valid config', () => {
115
+ const nxJson = validReleaseNxJson();
116
+ expect(applyReleaseConfig(nxJson)).toBe(false);
117
+ });
118
+
119
+ it('applies full release config defaults', () => {
120
+ const nxJson: Record<string, unknown> = {};
121
+ expect(applyReleaseConfig(nxJson)).toBe(true);
122
+
123
+ const release = nxJson.release as Record<string, unknown>;
124
+ expect(release.projectsRelationship).toBe('independent');
125
+ const version = release.version as Record<string, unknown>;
126
+ expect(version.specifierSource).toBe('conventional-commits');
127
+ expect(version.currentVersionResolver).toBe('git-tag');
128
+ expect(version.fallbackCurrentVersionResolver).toBe('disk');
129
+ expect(version.versionActions).toBe(SMOO_NX_VERSION_ACTIONS);
130
+ expect(version.preVersionCommand).toBeUndefined();
131
+ const releaseTag = release.releaseTag as Record<string, unknown>;
132
+ expect(releaseTag.pattern).toBe(SMOO_NX_RELEASE_TAG_PATTERN);
133
+ const changelog = release.changelog as Record<string, unknown>;
134
+ expect(changelog.workspaceChangelog).toBe(false);
135
+ const projectChangelogs = changelog.projectChangelogs as Record<string, unknown>;
136
+ expect(projectChangelogs.createRelease).toBe(false);
137
+ expect(projectChangelogs.file).toBe(false);
138
+ const renderOptions = projectChangelogs.renderOptions as Record<string, unknown>;
139
+ expect(renderOptions.authors).toBe(true);
140
+ expect(renderOptions.applyUsernameToAuthors).toBe(true);
141
+ });
142
+
143
+ it('removes preVersionCommand', () => {
144
+ const nxJson = validReleaseNxJson();
145
+ const version = (nxJson.release as Record<string, unknown>).version as Record<string, unknown>;
146
+ version.preVersionCommand = 'nx run-many -t build';
147
+ expect(applyReleaseConfig(nxJson)).toBe(true);
148
+ expect(version.preVersionCommand).toBeUndefined();
149
+ });
150
+ });
151
+
152
+ // ---------------------------------------------------------------------------
153
+ // Layer 2: Tree-based function tests
154
+ // ---------------------------------------------------------------------------
155
+
156
+ describe('Tree: checkReleaseConfigTree', () => {
157
+ it('returns issue when nx.json missing', () => {
158
+ const tree = createTreeWithEmptyWorkspace();
159
+ tree.delete('nx.json');
160
+ const issues = checkReleaseConfigTree(tree);
161
+ expect(issues).toHaveLength(1);
162
+ expect(issues[0].message).toBe('nx.json not found');
163
+ });
164
+
165
+ it('detects missing release config', () => {
166
+ const tree = createTreeWithEmptyWorkspace();
167
+ writeJson(tree, 'nx.json', {});
168
+
169
+ const issues = checkReleaseConfigTree(tree);
170
+ expect(issues.some((i) => i.message.includes('release config is missing'))).toBe(true);
171
+ });
172
+
173
+ it('returns no issues for valid config', () => {
174
+ const tree = createTreeWithEmptyWorkspace();
175
+ writeJson(tree, 'nx.json', validReleaseNxJson());
176
+
177
+ expect(checkReleaseConfigTree(tree)).toEqual([]);
178
+ });
179
+ });
180
+
181
+ describe('Tree: applyReleaseConfigTree', () => {
182
+ it('returns false when nx.json missing', () => {
183
+ const tree = createTreeWithEmptyWorkspace();
184
+ tree.delete('nx.json');
185
+ expect(applyReleaseConfigTree(tree)).toBe(false);
186
+ });
187
+
188
+ it('applies release config defaults and writes back to tree', () => {
189
+ const tree = createTreeWithEmptyWorkspace();
190
+ writeJson(tree, 'nx.json', {});
191
+
192
+ expect(applyReleaseConfigTree(tree)).toBe(true);
193
+
194
+ const nxJson = readJson(tree, 'nx.json');
195
+ const release = nxJson.release as Record<string, unknown>;
196
+ expect(release.projectsRelationship).toBe('independent');
197
+ const version = release.version as Record<string, unknown>;
198
+ expect(version.specifierSource).toBe('conventional-commits');
199
+
200
+ // Tree version now passes check
201
+ expect(checkReleaseConfigTree(tree)).toEqual([]);
202
+ });
203
+
204
+ it('returns false when config already valid', () => {
205
+ const tree = createTreeWithEmptyWorkspace();
206
+ writeJson(tree, 'nx.json', validReleaseNxJson());
207
+
208
+ expect(applyReleaseConfigTree(tree)).toBe(false);
209
+ });
210
+
211
+ it('removes preVersionCommand via tree', () => {
212
+ const tree = createTreeWithEmptyWorkspace();
213
+ const nxJson = validReleaseNxJson();
214
+ const version = (nxJson.release as Record<string, unknown>).version as Record<string, unknown>;
215
+ version.preVersionCommand = 'nx run-many -t build';
216
+ writeJson(tree, 'nx.json', nxJson);
217
+
218
+ expect(applyReleaseConfigTree(tree)).toBe(true);
219
+
220
+ const fixed = readJson(tree, 'nx.json');
221
+ const fixedVersion = (fixed.release as Record<string, unknown>).version as Record<string, unknown>;
222
+ expect(fixedVersion.preVersionCommand).toBeUndefined();
223
+
224
+ expect(checkReleaseConfigTree(tree)).toEqual([]);
225
+ });
226
+ });
227
+
228
+ // ---------------------------------------------------------------------------
229
+ // Layer 3: Filesystem wrapper integration test
230
+ // ---------------------------------------------------------------------------
231
+
232
+ describe('filesystem: checkReleaseConfigPolicy / applyReleaseConfigPolicy', () => {
233
+ it('round-trips check/apply on real temp directory', async () => {
234
+ const root = await mkdtemp(join(tmpdir(), 'smoothbricks-release-policy-'));
235
+ try {
236
+ await writeJsonFile(join(root, 'nx.json'), {});
237
+
238
+ const issues = checkReleaseConfigPolicy(root);
239
+ expect(issues.length).toBeGreaterThan(0);
240
+ // Filesystem wrapper uses absolute paths
241
+ expect(issues[0].path).toBe(join(root, 'nx.json'));
242
+
243
+ expect(applyReleaseConfigPolicy(root)).toBe(true);
244
+
245
+ const nxJson = JSON.parse(await readFile(join(root, 'nx.json'), 'utf8'));
246
+ const release = nxJson.release;
247
+ expect(release.projectsRelationship).toBe('independent');
248
+ expect(release.version.specifierSource).toBe('conventional-commits');
249
+ expect(release.version.versionActions).toBe(SMOO_NX_VERSION_ACTIONS);
250
+
251
+ // No issues after fix
252
+ expect(checkReleaseConfigPolicy(root)).toEqual([]);
253
+ expect(applyReleaseConfigPolicy(root)).toBe(false);
254
+ } finally {
255
+ await rm(root, { recursive: true, force: true });
256
+ }
257
+ });
258
+
259
+ it('reports issue when nx.json is missing', async () => {
260
+ const root = await mkdtemp(join(tmpdir(), 'smoothbricks-release-policy-'));
261
+ try {
262
+ const issues = checkReleaseConfigPolicy(root);
263
+ expect(issues).toHaveLength(1);
264
+ expect(issues[0].message).toBe('nx.json not found or invalid');
265
+
266
+ expect(applyReleaseConfigPolicy(root)).toBe(false);
267
+ } finally {
268
+ await rm(root, { recursive: true, force: true });
269
+ }
270
+ });
271
+ });
272
+
273
+ // ---------------------------------------------------------------------------
274
+ // Test-local file helper (for filesystem integration tests only)
275
+ // ---------------------------------------------------------------------------
276
+
277
+ async function writeJsonFile(path: string, value: unknown): Promise<void> {
278
+ await mkdir(dirname(path), { recursive: true });
279
+ await writeFile(path, `${JSON.stringify(value, null, 2)}\n`);
280
+ }
@@ -0,0 +1,258 @@
1
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import type { Tree } from 'nx/src/devkit-exports.js';
4
+ import { readJson, updateJson } from 'nx/src/devkit-exports.js';
5
+
6
+ import type { NxPolicyIssue } from './workspace-config-policy.js';
7
+
8
+ export const SMOO_NX_VERSION_ACTIONS = '@smoothbricks/nx-plugin/version-actions';
9
+ export const SMOO_NX_RELEASE_TAG_PATTERN = '{projectName}@{version}';
10
+
11
+ /**
12
+ * Check release config policy on an in-memory nx.json object.
13
+ */
14
+ export function checkReleaseConfig(nxJson: Record<string, unknown>): NxPolicyIssue[] {
15
+ const issues: NxPolicyIssue[] = [];
16
+
17
+ const release = recordProperty(nxJson, 'release');
18
+ if (!release) {
19
+ issues.push({ path: 'nx.json', message: 'release config is missing' });
20
+ return issues;
21
+ }
22
+
23
+ if (stringProperty(release, 'projectsRelationship') !== 'independent') {
24
+ issues.push({ path: 'nx.json', message: 'release.projectsRelationship must be independent' });
25
+ }
26
+
27
+ const version = recordProperty(release, 'version');
28
+ if (!version) {
29
+ issues.push({ path: 'nx.json', message: 'release.version config is missing' });
30
+ }
31
+ if (version && stringProperty(version, 'specifierSource') !== 'conventional-commits') {
32
+ issues.push({ path: 'nx.json', message: 'release.version.specifierSource must be conventional-commits' });
33
+ }
34
+ if (version && stringProperty(version, 'currentVersionResolver') !== 'git-tag') {
35
+ issues.push({ path: 'nx.json', message: 'release.version.currentVersionResolver must be git-tag' });
36
+ }
37
+ if (version && stringProperty(version, 'fallbackCurrentVersionResolver') !== 'disk') {
38
+ issues.push({ path: 'nx.json', message: 'release.version.fallbackCurrentVersionResolver must be disk' });
39
+ }
40
+ if (version && stringProperty(version, 'versionActions') !== SMOO_NX_VERSION_ACTIONS) {
41
+ issues.push({
42
+ path: 'nx.json',
43
+ message: `release.version.versionActions must be ${SMOO_NX_VERSION_ACTIONS}`,
44
+ });
45
+ }
46
+ if (version && stringProperty(version, 'preVersionCommand')) {
47
+ issues.push({
48
+ path: 'nx.json',
49
+ message: 'release.version.preVersionCommand must not be defined; smoo builds npm-missing packages before publish',
50
+ });
51
+ }
52
+
53
+ const releaseTag = recordProperty(release, 'releaseTag');
54
+ if (!releaseTag) {
55
+ issues.push({ path: 'nx.json', message: 'release.releaseTag config is missing' });
56
+ }
57
+ if (releaseTag && stringProperty(releaseTag, 'pattern') !== SMOO_NX_RELEASE_TAG_PATTERN) {
58
+ issues.push({
59
+ path: 'nx.json',
60
+ message: `release.releaseTag.pattern must be ${SMOO_NX_RELEASE_TAG_PATTERN}`,
61
+ });
62
+ }
63
+
64
+ const changelog = recordProperty(release, 'changelog');
65
+ if (!changelog) {
66
+ issues.push({ path: 'nx.json', message: 'release.changelog config is missing' });
67
+ }
68
+ if (changelog && changelog.workspaceChangelog !== false) {
69
+ issues.push({ path: 'nx.json', message: 'release.changelog.workspaceChangelog must be false' });
70
+ }
71
+
72
+ const projectChangelogs = changelog ? recordProperty(changelog, 'projectChangelogs') : null;
73
+ if (!projectChangelogs) {
74
+ issues.push({ path: 'nx.json', message: 'release.changelog.projectChangelogs config is missing' });
75
+ }
76
+ if (projectChangelogs && projectChangelogs.createRelease !== false) {
77
+ issues.push({
78
+ path: 'nx.json',
79
+ message: 'release.changelog.projectChangelogs.createRelease must be false',
80
+ });
81
+ }
82
+ if (projectChangelogs && projectChangelogs.file !== false) {
83
+ issues.push({
84
+ path: 'nx.json',
85
+ message: 'release.changelog.projectChangelogs.file must be false',
86
+ });
87
+ }
88
+
89
+ const renderOptions = projectChangelogs ? recordProperty(projectChangelogs, 'renderOptions') : null;
90
+ if (!renderOptions) {
91
+ issues.push({
92
+ path: 'nx.json',
93
+ message: 'release.changelog.projectChangelogs.renderOptions config is missing',
94
+ });
95
+ }
96
+
97
+ return issues;
98
+ }
99
+
100
+ /**
101
+ * Apply release config policy to an in-memory nx.json object.
102
+ * Mutates in place, returns whether anything changed.
103
+ */
104
+ export function applyReleaseConfig(nxJson: Record<string, unknown>): boolean {
105
+ let changed = false;
106
+
107
+ const release = getOrCreateRecord(nxJson, 'release');
108
+ changed = setStringProperty(release, 'projectsRelationship', 'independent') || changed;
109
+
110
+ const version = getOrCreateRecord(release, 'version');
111
+ changed = setStringProperty(version, 'specifierSource', 'conventional-commits') || changed;
112
+ changed = setStringProperty(version, 'currentVersionResolver', 'git-tag') || changed;
113
+ changed = setStringProperty(version, 'fallbackCurrentVersionResolver', 'disk') || changed;
114
+ changed = setStringProperty(version, 'versionActions', SMOO_NX_VERSION_ACTIONS) || changed;
115
+
116
+ if ('preVersionCommand' in version) {
117
+ delete version.preVersionCommand;
118
+ changed = true;
119
+ }
120
+
121
+ const releaseTag = getOrCreateRecord(release, 'releaseTag');
122
+ changed = setStringProperty(releaseTag, 'pattern', SMOO_NX_RELEASE_TAG_PATTERN) || changed;
123
+
124
+ const changelog = getOrCreateRecord(release, 'changelog');
125
+ changed = setBooleanProperty(changelog, 'workspaceChangelog', false) || changed;
126
+
127
+ const projectChangelogs = getOrCreateRecord(changelog, 'projectChangelogs');
128
+ changed = setBooleanProperty(projectChangelogs, 'createRelease', false) || changed;
129
+ changed = setBooleanProperty(projectChangelogs, 'file', false) || changed;
130
+
131
+ const renderOptions = getOrCreateRecord(projectChangelogs, 'renderOptions');
132
+ if (typeof renderOptions.authors !== 'boolean') {
133
+ renderOptions.authors = true;
134
+ changed = true;
135
+ }
136
+ if (typeof renderOptions.applyUsernameToAuthors !== 'boolean') {
137
+ renderOptions.applyUsernameToAuthors = true;
138
+ changed = true;
139
+ }
140
+
141
+ return changed;
142
+ }
143
+
144
+ /**
145
+ * Check release config policy using an Nx Tree.
146
+ */
147
+ export function checkReleaseConfigTree(tree: Tree): NxPolicyIssue[] {
148
+ if (!tree.exists('nx.json')) {
149
+ return [{ path: 'nx.json', message: 'nx.json not found' }];
150
+ }
151
+ return checkReleaseConfig(readJson(tree, 'nx.json'));
152
+ }
153
+
154
+ /**
155
+ * Apply release config policy using an Nx Tree.
156
+ * Returns whether anything changed.
157
+ */
158
+ export function applyReleaseConfigTree(tree: Tree): boolean {
159
+ if (!tree.exists('nx.json')) {
160
+ return false;
161
+ }
162
+ let changed = false;
163
+ updateJson(tree, 'nx.json', (nxJson: Record<string, unknown>) => {
164
+ changed = applyReleaseConfig(nxJson);
165
+ return nxJson;
166
+ });
167
+ return changed;
168
+ }
169
+
170
+ /**
171
+ * Check nx.json release config policy, returns issues found.
172
+ * Only checks release-specific config — workspace config policy is checked separately.
173
+ */
174
+ export function checkReleaseConfigPolicy(root: string): NxPolicyIssue[] {
175
+ const nxJsonPath = join(root, 'nx.json');
176
+ const nxJson = readJsonObject(nxJsonPath);
177
+ if (!nxJson) {
178
+ return [{ path: nxJsonPath, message: 'nx.json not found or invalid' }];
179
+ }
180
+ const coreIssues = checkReleaseConfig(nxJson);
181
+ // Remap 'nx.json' paths to absolute paths for filesystem callers
182
+ return coreIssues.map((issue) => ({
183
+ ...issue,
184
+ path: issue.path === 'nx.json' ? nxJsonPath : issue.path,
185
+ }));
186
+ }
187
+
188
+ /**
189
+ * Fix nx.json release config policy. Returns whether anything changed.
190
+ */
191
+ export function applyReleaseConfigPolicy(root: string): boolean {
192
+ const nxJsonPath = join(root, 'nx.json');
193
+ const nxJson = readJsonObject(nxJsonPath);
194
+ if (!nxJson) {
195
+ return false;
196
+ }
197
+ const changed = applyReleaseConfig(nxJson);
198
+ if (changed) {
199
+ writeJsonObject(nxJsonPath, nxJson);
200
+ }
201
+ return changed;
202
+ }
203
+
204
+ function readJsonObject(path: string): Record<string, unknown> | null {
205
+ if (!existsSync(path)) {
206
+ return null;
207
+ }
208
+ try {
209
+ const parsed: unknown = JSON.parse(readFileSync(path, 'utf8'));
210
+ return isRecord(parsed) ? parsed : null;
211
+ } catch {
212
+ return null;
213
+ }
214
+ }
215
+
216
+ function writeJsonObject(path: string, value: Record<string, unknown>): void {
217
+ writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`);
218
+ }
219
+
220
+ function isRecord(value: unknown): value is Record<string, unknown> {
221
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
222
+ }
223
+
224
+ function stringProperty(record: Record<string, unknown>, key: string): string | null {
225
+ const value = record[key];
226
+ return typeof value === 'string' && value.length > 0 ? value : null;
227
+ }
228
+
229
+ function recordProperty(record: Record<string, unknown>, key: string): Record<string, unknown> | null {
230
+ const value = record[key];
231
+ return isRecord(value) ? value : null;
232
+ }
233
+
234
+ function getOrCreateRecord(record: Record<string, unknown>, key: string): Record<string, unknown> {
235
+ const value = record[key];
236
+ if (isRecord(value)) {
237
+ return value;
238
+ }
239
+ const next: Record<string, unknown> = {};
240
+ record[key] = next;
241
+ return next;
242
+ }
243
+
244
+ function setStringProperty(record: Record<string, unknown>, key: string, value: string): boolean {
245
+ if (record[key] === value) {
246
+ return false;
247
+ }
248
+ record[key] = value;
249
+ return true;
250
+ }
251
+
252
+ function setBooleanProperty(record: Record<string, unknown>, key: string, value: boolean): boolean {
253
+ if (record[key] === value) {
254
+ return false;
255
+ }
256
+ record[key] = value;
257
+ return true;
258
+ }