@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.
- package/README.md +93 -2
- package/dist/bounded-test-policy.d.ts +32 -0
- package/dist/bounded-test-policy.d.ts.map +1 -0
- package/dist/bounded-test-policy.js +184 -0
- package/dist/executors/bounded-exec/executor.d.ts +15 -0
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -0
- package/dist/executors/bounded-exec/executor.js +179 -0
- package/dist/executors/bounded-exec/schema.d.ts +11 -0
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -0
- package/dist/executors/bounded-exec/schema.js +1 -0
- package/dist/generators/bounded-test-targets/generator.d.ts +7 -0
- package/dist/generators/bounded-test-targets/generator.d.ts.map +1 -0
- package/dist/generators/bounded-test-targets/generator.js +40 -0
- package/dist/generators/bun-test-tracing/generator.d.ts +10 -0
- package/dist/generators/bun-test-tracing/generator.d.ts.map +1 -0
- package/dist/generators/bun-test-tracing/generator.js +247 -0
- package/dist/generators/create-package/generator.d.ts +4 -0
- package/dist/generators/create-package/generator.d.ts.map +1 -0
- package/dist/generators/create-package/generator.js +204 -0
- package/dist/generators/make-public/generator.d.ts +4 -0
- package/dist/generators/make-public/generator.d.ts.map +1 -0
- package/dist/generators/make-public/generator.js +55 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +197 -0
- package/dist/package-target-policy.d.ts +31 -0
- package/dist/package-target-policy.d.ts.map +1 -0
- package/dist/package-target-policy.js +930 -0
- package/dist/release-config-policy.d.ts +32 -0
- package/dist/release-config-policy.d.ts.map +1 -0
- package/dist/release-config-policy.js +225 -0
- package/dist/typecheck-test-policy.d.ts +56 -0
- package/dist/typecheck-test-policy.d.ts.map +1 -0
- package/dist/typecheck-test-policy.js +640 -0
- package/dist/version-actions.cjs +78 -0
- package/dist/version-actions.d.cts +6 -0
- package/dist/version-actions.d.cts.map +1 -0
- package/dist/workspace-config-policy.d.ts +33 -0
- package/dist/workspace-config-policy.d.ts.map +1 -0
- package/dist/workspace-config-policy.js +334 -0
- package/executors.json +9 -0
- package/generators.json +24 -0
- package/loader.js +16 -0
- package/package.json +89 -4
- package/src/bounded-test-policy.test.ts +257 -0
- package/src/bounded-test-policy.ts +257 -0
- package/src/bun-bootstrap.test.ts +52 -0
- package/src/executors/bounded-exec/executor.test.ts +258 -0
- package/src/executors/bounded-exec/executor.ts +228 -0
- package/src/executors/bounded-exec/schema.json +68 -0
- package/src/executors/bounded-exec/schema.ts +10 -0
- package/src/generators/bounded-test-targets/generator.test.ts +136 -0
- package/src/generators/bounded-test-targets/generator.ts +67 -0
- package/src/generators/bounded-test-targets/schema.d.ts +3 -0
- package/src/generators/bounded-test-targets/schema.json +14 -0
- package/src/generators/bun-test-tracing/generator.test.ts +243 -0
- package/src/generators/bun-test-tracing/generator.ts +348 -0
- package/src/generators/bun-test-tracing/schema.d.ts +8 -0
- package/src/generators/bun-test-tracing/schema.json +28 -0
- package/src/generators/create-package/generator.test.ts +206 -0
- package/src/generators/create-package/generator.ts +251 -0
- package/src/generators/create-package/schema.d.ts +5 -0
- package/src/generators/create-package/schema.json +24 -0
- package/src/generators/make-public/generator.test.ts +137 -0
- package/src/generators/make-public/generator.ts +83 -0
- package/src/generators/make-public/schema.d.ts +3 -0
- package/src/generators/make-public/schema.json +13 -0
- package/src/index.test.ts +200 -0
- package/src/index.ts +242 -0
- package/src/package-target-policy.test.ts +778 -0
- package/src/package-target-policy.ts +1127 -0
- package/src/release-config-policy.test.ts +280 -0
- package/src/release-config-policy.ts +258 -0
- package/src/typecheck-test-policy.test.ts +797 -0
- package/src/typecheck-test-policy.ts +752 -0
- package/src/version-actions.cts +94 -0
- package/src/workspace-config-policy.test.ts +376 -0
- package/src/workspace-config-policy.ts +390 -0
|
@@ -0,0 +1,930 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { getProjects, readJson, updateJson } from 'nx/src/devkit-exports.js';
|
|
4
|
+
import { boundedTestScriptAlias } from './bounded-test-policy.js';
|
|
5
|
+
export const BUILD_OUTPUT_DEPENDENCIES = [
|
|
6
|
+
'*-js',
|
|
7
|
+
'*-web',
|
|
8
|
+
'*-html',
|
|
9
|
+
'*-css',
|
|
10
|
+
'*-ios',
|
|
11
|
+
'*-android',
|
|
12
|
+
'*-native',
|
|
13
|
+
'*-napi',
|
|
14
|
+
'*-bun',
|
|
15
|
+
'*-wasm',
|
|
16
|
+
];
|
|
17
|
+
const workspaceDependencyFields = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];
|
|
18
|
+
function isRecord(value) {
|
|
19
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
20
|
+
}
|
|
21
|
+
function stringProperty(record, key) {
|
|
22
|
+
const value = record[key];
|
|
23
|
+
return typeof value === 'string' && value.length > 0 ? value : null;
|
|
24
|
+
}
|
|
25
|
+
function recordProperty(record, key) {
|
|
26
|
+
const value = record[key];
|
|
27
|
+
return isRecord(value) ? value : null;
|
|
28
|
+
}
|
|
29
|
+
function getOrCreateRecord(record, key) {
|
|
30
|
+
const value = record[key];
|
|
31
|
+
if (isRecord(value)) {
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
const next = {};
|
|
35
|
+
record[key] = next;
|
|
36
|
+
return next;
|
|
37
|
+
}
|
|
38
|
+
function setStringProperty(record, key, value) {
|
|
39
|
+
if (record[key] === value) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
record[key] = value;
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
function setStringArrayProperty(record, key, value) {
|
|
46
|
+
const current = record[key];
|
|
47
|
+
if (stringArrayEquals(current, value)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
record[key] = value;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
function stringArrayEquals(value, expected) {
|
|
54
|
+
return (Array.isArray(value) && value.length === expected.length && value.every((entry, index) => entry === expected[index]));
|
|
55
|
+
}
|
|
56
|
+
function readJsonObject(path) {
|
|
57
|
+
if (!existsSync(path)) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
61
|
+
return isRecord(parsed) ? parsed : null;
|
|
62
|
+
}
|
|
63
|
+
function writeJsonObject(path, value) {
|
|
64
|
+
writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`);
|
|
65
|
+
}
|
|
66
|
+
function listWorkspacePackageJsonPaths(root) {
|
|
67
|
+
const rootPackagePath = join(root, 'package.json');
|
|
68
|
+
if (!existsSync(rootPackagePath)) {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
const rootPackage = readJsonObject(rootPackagePath);
|
|
72
|
+
if (!rootPackage) {
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
const workspacePatterns = Array.isArray(rootPackage.workspaces)
|
|
76
|
+
? rootPackage.workspaces.filter((entry) => typeof entry === 'string')
|
|
77
|
+
: [];
|
|
78
|
+
const paths = [];
|
|
79
|
+
for (const pattern of workspacePatterns) {
|
|
80
|
+
if (!pattern.endsWith('/*')) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const parent = join(root, pattern.slice(0, -2));
|
|
84
|
+
if (!existsSync(parent)) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
for (const entry of readdirSync(parent, { withFileTypes: true })) {
|
|
88
|
+
if (!entry.isDirectory()) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const packageJsonPath = join(parent, entry.name, 'package.json');
|
|
92
|
+
if (existsSync(packageJsonPath)) {
|
|
93
|
+
paths.push(packageJsonPath);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return paths.sort((a, b) => a.localeCompare(b));
|
|
98
|
+
}
|
|
99
|
+
function getWorkspacePackageNames(root) {
|
|
100
|
+
const names = new Set();
|
|
101
|
+
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
102
|
+
const pkg = readJsonObject(packageJsonPath);
|
|
103
|
+
if (!pkg) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const name = stringProperty(pkg, 'name');
|
|
107
|
+
if (name) {
|
|
108
|
+
names.add(name);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return names;
|
|
112
|
+
}
|
|
113
|
+
function packagePathFromJsonPath(root, packageJsonPath) {
|
|
114
|
+
const absolute = packageJsonPath.replace(/[/\\]package\.json$/, '');
|
|
115
|
+
const rootNormalized = root.replace(/[/\\]$/, '');
|
|
116
|
+
if (absolute.startsWith(rootNormalized)) {
|
|
117
|
+
const relative = absolute.slice(rootNormalized.length + 1);
|
|
118
|
+
return relative || '.';
|
|
119
|
+
}
|
|
120
|
+
return absolute;
|
|
121
|
+
}
|
|
122
|
+
export function packageNxProjectName(pkg) {
|
|
123
|
+
const nx = recordProperty(pkg, 'nx');
|
|
124
|
+
return (nx ? stringProperty(nx, 'name') : null) ?? stringProperty(pkg, 'name');
|
|
125
|
+
}
|
|
126
|
+
function applyPackageNxConfig(pkg, options) {
|
|
127
|
+
const nx = getOrCreateRecord(pkg, 'nx');
|
|
128
|
+
const changed = setStringProperty(nx, 'name', options.projectName);
|
|
129
|
+
const targets = options.targets === true ? getOrCreateRecord(nx, 'targets') : null;
|
|
130
|
+
return { nx, targets, changed };
|
|
131
|
+
}
|
|
132
|
+
function resolvedProjectTargetNames(resolvedProject) {
|
|
133
|
+
if (!resolvedProject) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
return isResolvedProjectTargets(resolvedProject) ? resolvedProject.targets : resolvedProject;
|
|
137
|
+
}
|
|
138
|
+
function resolvedProjectBuildDependsOn(resolvedProject) {
|
|
139
|
+
if (!isResolvedProjectTargets(resolvedProject)) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
return resolvedProject.buildDependsOn;
|
|
143
|
+
}
|
|
144
|
+
function isResolvedProjectTargets(value) {
|
|
145
|
+
return isRecord(value) && value.targets instanceof Set;
|
|
146
|
+
}
|
|
147
|
+
function targetExistsInResolvedProject(targetName, resolvedTargets) {
|
|
148
|
+
return resolvedTargets?.has(targetName) === true;
|
|
149
|
+
}
|
|
150
|
+
function isBuildOutputDependencyPattern(dependency) {
|
|
151
|
+
return BUILD_OUTPUT_DEPENDENCIES.includes(dependency);
|
|
152
|
+
}
|
|
153
|
+
function expectedTargetDependencies(targetName) {
|
|
154
|
+
return targetName === 'preview' ? ['build'] : ['^build'];
|
|
155
|
+
}
|
|
156
|
+
function applyTargetDependencyPolicy(target, targetName) {
|
|
157
|
+
return setStringArrayProperty(target, 'dependsOn', expectedTargetDependencies(targetName));
|
|
158
|
+
}
|
|
159
|
+
function targetDependsOn(target, expected) {
|
|
160
|
+
const dependsOn = target.dependsOn;
|
|
161
|
+
return Array.isArray(dependsOn) && expected.every((entry) => dependsOn.includes(entry));
|
|
162
|
+
}
|
|
163
|
+
function isNoopTarget(target) {
|
|
164
|
+
const executor = stringProperty(target, 'executor');
|
|
165
|
+
if (executor !== null && executor !== 'nx:noop') {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
const options = recordProperty(target, 'options');
|
|
169
|
+
return !options || stringProperty(options, 'command') === null;
|
|
170
|
+
}
|
|
171
|
+
function targetDependenciesMatchResolvedBuild(target, resolvedBuildDependsOn) {
|
|
172
|
+
if (!Array.isArray(target.dependsOn)) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
const expected = new Set(resolvedBuildDependsOn);
|
|
176
|
+
if (target.dependsOn.length !== expected.size) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
return target.dependsOn.every((dependency) => {
|
|
180
|
+
if (typeof dependency !== 'string') {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
return expected.has(dependency);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
function removeRedundantNoopBuildTarget(pkg, resolvedProject) {
|
|
187
|
+
const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
|
|
188
|
+
const resolvedBuildDependsOn = resolvedProjectBuildDependsOn(resolvedProject);
|
|
189
|
+
if (!resolvedTargets?.has('build') || !resolvedBuildDependsOn) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
const nx = recordProperty(pkg, 'nx');
|
|
193
|
+
const targets = nx ? recordProperty(nx, 'targets') : null;
|
|
194
|
+
const build = targets ? recordProperty(targets, 'build') : null;
|
|
195
|
+
if (!targets ||
|
|
196
|
+
!build ||
|
|
197
|
+
!isNoopTarget(build) ||
|
|
198
|
+
!targetDependenciesMatchResolvedBuild(build, resolvedBuildDependsOn)) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
delete targets.build;
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
function isNxRunAlias(command) {
|
|
205
|
+
return /^nx\s+run\s+\S+:\S+/.test(command.trim());
|
|
206
|
+
}
|
|
207
|
+
function parseNxRunAlias(command) {
|
|
208
|
+
const match = /^nx\s+run\s+([^:\s]+):([^\s]+)(?:\s|$)/.exec(command.trim());
|
|
209
|
+
if (!match?.[1] || !match[2]) {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
return { projectName: match[1], targetName: match[2] };
|
|
213
|
+
}
|
|
214
|
+
export function nxRunAlias(projectName, targetName, continuous) {
|
|
215
|
+
if (targetName === 'test') {
|
|
216
|
+
return boundedTestScriptAlias(projectName);
|
|
217
|
+
}
|
|
218
|
+
const flags = continuous || targetName === 'test' ? ' --tui=false --outputStyle=stream' : '';
|
|
219
|
+
return `nx run ${projectName}:${targetName}${flags}`;
|
|
220
|
+
}
|
|
221
|
+
function isContinuousTarget(targetName, command) {
|
|
222
|
+
return (/(?:^|:|-)(?:dev|serve|preview|watch)(?:$|:|-)/.test(targetName) ||
|
|
223
|
+
/(?:^|\s)(?:dev|serve|preview|--watch|-w)(?:\s|$)/.test(command));
|
|
224
|
+
}
|
|
225
|
+
function isSafeNxScriptCommand(command) {
|
|
226
|
+
const trimmed = command.trim();
|
|
227
|
+
return (/^bun\s+test(?:\s|$)/.test(trimmed) ||
|
|
228
|
+
/^tsc-bun-test(?:\s|$)/.test(trimmed) ||
|
|
229
|
+
/^vitest(?:\s|$)/.test(trimmed) ||
|
|
230
|
+
/^tsc\s+(?:--build|--noEmit)(?:\s|$)/.test(trimmed) ||
|
|
231
|
+
/^tsdown(?:\s|$)/.test(trimmed) ||
|
|
232
|
+
/^vite\s+(?:build|dev|preview)(?:\s|$)/.test(trimmed) ||
|
|
233
|
+
/^astro\s+(?:build|dev|preview|check)(?:\s|$)/.test(trimmed) ||
|
|
234
|
+
/^zig\s+build(?:\s|$)/.test(trimmed) ||
|
|
235
|
+
/^bun\s+[./\w-]*build[\w.-]*\.ts(?:\s|$)/.test(trimmed) ||
|
|
236
|
+
/(?:^|\s)(?:bench|benchmark)(?:\s|$)/.test(trimmed) ||
|
|
237
|
+
/^wrangler\s+build(?:\s|$)/.test(trimmed));
|
|
238
|
+
}
|
|
239
|
+
function isBlockedScriptCommand(scriptName, command) {
|
|
240
|
+
if (/^(?:deploy|db|release|sync|subtree|publish|pack)(?::|$)/.test(scriptName)) {
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
const trimmed = parseEnvPrefixedCommand(command).command;
|
|
244
|
+
return /^(?:deploy|db|release|sync|subtree|publish|pack)(?:\s|$)/.test(trimmed) || trimmed === 'astro';
|
|
245
|
+
}
|
|
246
|
+
function isScriptRunnerCommand(command, scriptName) {
|
|
247
|
+
if (!command) {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
const escaped = scriptName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
251
|
+
return new RegExp(`^(?:bun|npm)\\s+run\\s+${escaped}(?:\\s|$)`).test(command.trim());
|
|
252
|
+
}
|
|
253
|
+
function parseEnvPrefixedCommand(command) {
|
|
254
|
+
const env = {};
|
|
255
|
+
let rest = command.trimStart();
|
|
256
|
+
while (true) {
|
|
257
|
+
const match = /^([A-Za-z_][A-Za-z0-9_]*)=/.exec(rest);
|
|
258
|
+
if (!match?.[1]) {
|
|
259
|
+
return { command: rest.trim(), env };
|
|
260
|
+
}
|
|
261
|
+
let index = match[0].length;
|
|
262
|
+
let value = '';
|
|
263
|
+
const quote = rest[index];
|
|
264
|
+
if (quote === '"' || quote === "'") {
|
|
265
|
+
index += 1;
|
|
266
|
+
const end = rest.indexOf(quote, index);
|
|
267
|
+
if (end === -1) {
|
|
268
|
+
return { command: command.trim(), env: {} };
|
|
269
|
+
}
|
|
270
|
+
value = rest.slice(index, end);
|
|
271
|
+
index = end + 1;
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
const end = rest.slice(index).search(/\s/);
|
|
275
|
+
const valueEnd = end === -1 ? rest.length : index + end;
|
|
276
|
+
value = rest.slice(index, valueEnd);
|
|
277
|
+
index = valueEnd;
|
|
278
|
+
}
|
|
279
|
+
if (index < rest.length && !/\s/.test(rest[index] ?? '')) {
|
|
280
|
+
return { command: command.trim(), env: {} };
|
|
281
|
+
}
|
|
282
|
+
env[match[1]] = value;
|
|
283
|
+
rest = rest.slice(index).trimStart();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
function targetNameForCommand(command) {
|
|
287
|
+
const trimmed = command.trim();
|
|
288
|
+
if (/^tsc\s+--build\s+tsconfig\.lib\.json(?:\s|$)/.test(trimmed)) {
|
|
289
|
+
return 'tsc-js';
|
|
290
|
+
}
|
|
291
|
+
if (/^tsdown(?:\s|$)/.test(trimmed)) {
|
|
292
|
+
return 'tsdown-js';
|
|
293
|
+
}
|
|
294
|
+
const zigStep = /^zig\s+build\s+([A-Za-z0-9_-]+)(?:\s|$)/.exec(trimmed)?.[1];
|
|
295
|
+
if (zigStep) {
|
|
296
|
+
return `zig-${zigStep}`;
|
|
297
|
+
}
|
|
298
|
+
if (/^wrangler\s+build(?:\s|$)/.test(trimmed)) {
|
|
299
|
+
return 'build';
|
|
300
|
+
}
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
function rewriteTargetName(scriptName, command) {
|
|
304
|
+
return targetNameForCommand(command) ?? (scriptName.includes(':') ? null : scriptName);
|
|
305
|
+
}
|
|
306
|
+
function replacementTargetName(targetName, command, resolvedTargets) {
|
|
307
|
+
if (command) {
|
|
308
|
+
const commandTargetName = targetNameForCommand(command);
|
|
309
|
+
if (commandTargetName) {
|
|
310
|
+
return commandTargetName;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
const suffix = targetName.slice(targetName.lastIndexOf(':') + 1);
|
|
314
|
+
if (suffix && targetExistsInResolvedProject(suffix, resolvedTargets)) {
|
|
315
|
+
return suffix;
|
|
316
|
+
}
|
|
317
|
+
const dashed = targetName.replaceAll(':', '-');
|
|
318
|
+
if (targetExistsInResolvedProject(dashed, resolvedTargets)) {
|
|
319
|
+
return dashed;
|
|
320
|
+
}
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
function classifyScriptRewrite(scriptName, command) {
|
|
324
|
+
if (isNxRunAlias(command) || isBlockedScriptCommand(scriptName, command)) {
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
const parsed = parseEnvPrefixedCommand(command);
|
|
328
|
+
const targetName = rewriteTargetName(scriptName, parsed.command);
|
|
329
|
+
if (!targetName || !isSafeNxScriptCommand(parsed.command)) {
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
return {
|
|
333
|
+
targetName,
|
|
334
|
+
continuous: isContinuousTarget(targetName, parsed.command),
|
|
335
|
+
command: parsed.command,
|
|
336
|
+
env: parsed.env,
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
function scriptTargetAliasesForProject(pkg, projectName) {
|
|
340
|
+
if (!projectName) {
|
|
341
|
+
return new Map();
|
|
342
|
+
}
|
|
343
|
+
const scripts = recordProperty(pkg, 'scripts');
|
|
344
|
+
if (!scripts) {
|
|
345
|
+
return new Map();
|
|
346
|
+
}
|
|
347
|
+
const aliases = new Map();
|
|
348
|
+
for (const [scriptName, rawCommand] of Object.entries(scripts)) {
|
|
349
|
+
if (typeof rawCommand !== 'string' || !scriptName.includes(':')) {
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
const alias = parseNxRunAlias(rawCommand);
|
|
353
|
+
if (alias?.projectName === projectName && !alias.targetName.includes(':')) {
|
|
354
|
+
aliases.set(scriptName, alias.targetName);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return aliases;
|
|
358
|
+
}
|
|
359
|
+
function hasWorkspaceDependency(pkg, workspaceNames) {
|
|
360
|
+
for (const field of workspaceDependencyFields) {
|
|
361
|
+
const dependencies = recordProperty(pkg, field);
|
|
362
|
+
if (!dependencies) {
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
for (const name of Object.keys(dependencies)) {
|
|
366
|
+
if (workspaceNames.has(name)) {
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
function targetCommand(target) {
|
|
374
|
+
const options = recordProperty(target, 'options');
|
|
375
|
+
return options ? stringProperty(options, 'command') : null;
|
|
376
|
+
}
|
|
377
|
+
function rewriteTargetDependencies(target, renamedTargets) {
|
|
378
|
+
if (!Array.isArray(target.dependsOn)) {
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
let changed = false;
|
|
382
|
+
target.dependsOn = target.dependsOn.map((dependency) => {
|
|
383
|
+
if (typeof dependency !== 'string') {
|
|
384
|
+
return dependency;
|
|
385
|
+
}
|
|
386
|
+
const next = renamedTargets.get(dependency);
|
|
387
|
+
if (!next) {
|
|
388
|
+
return dependency;
|
|
389
|
+
}
|
|
390
|
+
changed = true;
|
|
391
|
+
return next;
|
|
392
|
+
});
|
|
393
|
+
return changed;
|
|
394
|
+
}
|
|
395
|
+
function hasTestEntrypoint(pkg) {
|
|
396
|
+
const scripts = recordProperty(pkg, 'scripts');
|
|
397
|
+
if (typeof scripts?.test === 'string') {
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
const nx = recordProperty(pkg, 'nx');
|
|
401
|
+
const targets = nx ? recordProperty(nx, 'targets') : null;
|
|
402
|
+
return Boolean(targets && isRecord(targets.test));
|
|
403
|
+
}
|
|
404
|
+
function packageHasTestFiles(root, packagePath) {
|
|
405
|
+
return directoryContainsTestFiles(join(root, packagePath));
|
|
406
|
+
}
|
|
407
|
+
function directoryContainsTestFiles(path) {
|
|
408
|
+
if (!existsSync(path)) {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
for (const entry of readdirSync(path, { withFileTypes: true })) {
|
|
412
|
+
if (entry.name === 'node_modules' || entry.name === 'dist' || entry.name === 'coverage' || entry.name === '.git') {
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
const entryPath = join(path, entry.name);
|
|
416
|
+
if (entry.isDirectory()) {
|
|
417
|
+
if (directoryContainsTestFiles(entryPath)) {
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
const normalizedPath = entryPath.replaceAll('\\', '/');
|
|
423
|
+
if (normalizedPath.includes('/__tests__/')) {
|
|
424
|
+
return true;
|
|
425
|
+
}
|
|
426
|
+
if (/\.(?:test|spec)\.[cm]?[jt]sx?$/.test(entry.name)) {
|
|
427
|
+
return true;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
function validateBuildZigPolicy(root, packagePath) {
|
|
433
|
+
const path = join(root, packagePath, 'build.zig');
|
|
434
|
+
if (!existsSync(path)) {
|
|
435
|
+
return [];
|
|
436
|
+
}
|
|
437
|
+
if (/\bb\.step\s*\(/.test(readFileSync(path, 'utf8'))) {
|
|
438
|
+
return [];
|
|
439
|
+
}
|
|
440
|
+
return [
|
|
441
|
+
{ path: join(root, packagePath), message: `${packagePath}/build.zig must define at least one b.step(...) target` },
|
|
442
|
+
];
|
|
443
|
+
}
|
|
444
|
+
function validateExplicitNxTargets(pkg, packagePath, resolvedTargets) {
|
|
445
|
+
const nx = recordProperty(pkg, 'nx');
|
|
446
|
+
const targets = nx ? recordProperty(nx, 'targets') : null;
|
|
447
|
+
if (!targets) {
|
|
448
|
+
return [];
|
|
449
|
+
}
|
|
450
|
+
const issues = [];
|
|
451
|
+
for (const [targetName, rawTarget] of Object.entries(targets)) {
|
|
452
|
+
if (targetName.includes(':')) {
|
|
453
|
+
issues.push({
|
|
454
|
+
path: packagePath,
|
|
455
|
+
message: `package.json nx.targets.${targetName} must not use colon target names. ` +
|
|
456
|
+
'Nx CLI syntax already uses project:target:configuration; use a concrete tool-output target name and keep colon names only as package-script aliases.',
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
if (!isRecord(rawTarget)) {
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
issues.push(...validateTargetDependencies(rawTarget, `${packagePath}: nx.targets.${targetName}`, resolvedTargets));
|
|
463
|
+
}
|
|
464
|
+
return issues;
|
|
465
|
+
}
|
|
466
|
+
function validateTargetDependencies(target, label, resolvedTargets) {
|
|
467
|
+
if (!Array.isArray(target.dependsOn)) {
|
|
468
|
+
return [];
|
|
469
|
+
}
|
|
470
|
+
const issues = [];
|
|
471
|
+
for (const dependency of target.dependsOn) {
|
|
472
|
+
if (typeof dependency !== 'string') {
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
if (dependency.includes(':')) {
|
|
476
|
+
issues.push({
|
|
477
|
+
path: label.split(':')[0]?.trim() ?? label,
|
|
478
|
+
message: `${label}.dependsOn must not include colon target dependency ${dependency}`,
|
|
479
|
+
});
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (label.endsWith('nx.targets.build') &&
|
|
483
|
+
!dependency.startsWith('^') &&
|
|
484
|
+
!isBuildOutputDependencyPattern(dependency) &&
|
|
485
|
+
!targetExistsInResolvedProject(dependency, resolvedTargets)) {
|
|
486
|
+
issues.push({
|
|
487
|
+
path: label.split(':')[0]?.trim() ?? label,
|
|
488
|
+
message: `${label}.dependsOn references missing target ${dependency}`,
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
return issues;
|
|
493
|
+
}
|
|
494
|
+
function validateTestEntrypointPresence(root, packagePath, pkg) {
|
|
495
|
+
if (packagePath === '.') {
|
|
496
|
+
return [];
|
|
497
|
+
}
|
|
498
|
+
if (!packageHasTestFiles(root, packagePath) || hasTestEntrypoint(pkg)) {
|
|
499
|
+
return [];
|
|
500
|
+
}
|
|
501
|
+
return [{ path: packagePath, message: `${packagePath}: test files require scripts.test or nx.targets.test` }];
|
|
502
|
+
}
|
|
503
|
+
function validatePackageScriptPolicy(pkg, packagePath, workspaceNames, options = {}) {
|
|
504
|
+
if (!hasWorkspaceDependency(pkg, workspaceNames)) {
|
|
505
|
+
return [];
|
|
506
|
+
}
|
|
507
|
+
const scripts = recordProperty(pkg, 'scripts');
|
|
508
|
+
if (!scripts) {
|
|
509
|
+
return [];
|
|
510
|
+
}
|
|
511
|
+
const nx = recordProperty(pkg, 'nx');
|
|
512
|
+
const projectName = nx ? stringProperty(nx, 'name') : stringProperty(pkg, 'name');
|
|
513
|
+
const targets = nx ? recordProperty(nx, 'targets') : null;
|
|
514
|
+
const issues = [];
|
|
515
|
+
for (const [scriptName, rawCommand] of Object.entries(scripts)) {
|
|
516
|
+
if (typeof rawCommand !== 'string') {
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
const alias = parseNxRunAlias(rawCommand);
|
|
520
|
+
const rewrite = alias
|
|
521
|
+
? { targetName: alias.targetName, continuous: isContinuousTarget(alias.targetName, '') }
|
|
522
|
+
: classifyScriptRewrite(scriptName, rawCommand);
|
|
523
|
+
if (!rewrite || (alias && projectName && alias.projectName !== projectName)) {
|
|
524
|
+
if (alias && projectName && alias.projectName !== projectName) {
|
|
525
|
+
issues.push({
|
|
526
|
+
path: packagePath,
|
|
527
|
+
message: `${packagePath}: scripts.${scriptName} must delegate to project ${projectName}`,
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
continue;
|
|
531
|
+
}
|
|
532
|
+
if (!projectName) {
|
|
533
|
+
issues.push({
|
|
534
|
+
path: packagePath,
|
|
535
|
+
message: `${packagePath}: package scripts that use workspace dependencies require package.json nx.name`,
|
|
536
|
+
});
|
|
537
|
+
continue;
|
|
538
|
+
}
|
|
539
|
+
const expectedAlias = nxRunAlias(projectName, rewrite.targetName, rewrite.continuous);
|
|
540
|
+
if (rawCommand !== expectedAlias) {
|
|
541
|
+
issues.push({
|
|
542
|
+
path: packagePath,
|
|
543
|
+
message: `${packagePath}: scripts.${scriptName} must delegate to ${expectedAlias}`,
|
|
544
|
+
});
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
547
|
+
const target = targets ? recordProperty(targets, rewrite.targetName) : null;
|
|
548
|
+
if (!target &&
|
|
549
|
+
rewrite.targetName !== scriptName &&
|
|
550
|
+
targetExistsInResolvedProject(rewrite.targetName, options.resolvedTargets)) {
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
if (rewrite.targetName.includes(':')) {
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
if (rewrite.targetName === 'test') {
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
const targetOptions = target ? recordProperty(target, 'options') : null;
|
|
560
|
+
const command = targetOptions ? stringProperty(targetOptions, 'command') : null;
|
|
561
|
+
if (!target || stringProperty(target, 'executor') !== 'nx:run-commands' || !targetOptions || !command) {
|
|
562
|
+
issues.push({
|
|
563
|
+
path: packagePath,
|
|
564
|
+
message: `${packagePath}: nx.targets.${rewrite.targetName} must use nx:run-commands with options.command`,
|
|
565
|
+
});
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
if (stringProperty(targetOptions, 'cwd') !== '{projectRoot}') {
|
|
569
|
+
issues.push({
|
|
570
|
+
path: packagePath,
|
|
571
|
+
message: `${packagePath}: nx.targets.${rewrite.targetName}.options.cwd must be {projectRoot}`,
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
const deps = expectedTargetDependencies(rewrite.targetName);
|
|
575
|
+
if (!targetDependsOn(target, deps)) {
|
|
576
|
+
issues.push({
|
|
577
|
+
path: packagePath,
|
|
578
|
+
message: `${packagePath}: nx.targets.${rewrite.targetName}.dependsOn must include ${deps.join(', ')}`,
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
if (rewrite.continuous && target.continuous !== true) {
|
|
582
|
+
issues.push({
|
|
583
|
+
path: packagePath,
|
|
584
|
+
message: `${packagePath}: nx.targets.${rewrite.targetName}.continuous must be true`,
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
if (isScriptRunnerCommand(command, scriptName)) {
|
|
588
|
+
issues.push({
|
|
589
|
+
path: packagePath,
|
|
590
|
+
message: `${packagePath}: nx.targets.${rewrite.targetName}.options.command must not call scripts.${scriptName}`,
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return issues;
|
|
595
|
+
}
|
|
596
|
+
function migratePackageColonTargets(pkg, resolvedTargets) {
|
|
597
|
+
const nx = recordProperty(pkg, 'nx');
|
|
598
|
+
const targets = nx ? recordProperty(nx, 'targets') : null;
|
|
599
|
+
if (!targets) {
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
const projectName = packageNxProjectName(pkg);
|
|
603
|
+
const scripts = recordProperty(pkg, 'scripts');
|
|
604
|
+
let changed = false;
|
|
605
|
+
const renamedTargets = new Map();
|
|
606
|
+
for (const [targetName, rawTarget] of Object.entries(targets)) {
|
|
607
|
+
if (!targetName.includes(':') || !isRecord(rawTarget)) {
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
const nextTargetName = replacementTargetName(targetName, targetCommand(rawTarget), resolvedTargets);
|
|
611
|
+
if (!nextTargetName || nextTargetName.includes(':')) {
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
if (!targetExistsInResolvedProject(nextTargetName, resolvedTargets)) {
|
|
615
|
+
targets[nextTargetName] = rawTarget;
|
|
616
|
+
}
|
|
617
|
+
delete targets[targetName];
|
|
618
|
+
renamedTargets.set(targetName, nextTargetName);
|
|
619
|
+
changed = true;
|
|
620
|
+
}
|
|
621
|
+
if (renamedTargets.size === 0) {
|
|
622
|
+
return changed;
|
|
623
|
+
}
|
|
624
|
+
for (const target of Object.values(targets)) {
|
|
625
|
+
if (isRecord(target)) {
|
|
626
|
+
changed = rewriteTargetDependencies(target, renamedTargets) || changed;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
if (scripts && projectName) {
|
|
630
|
+
for (const [scriptName, rawCommand] of Object.entries(scripts)) {
|
|
631
|
+
if (typeof rawCommand !== 'string') {
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
const alias = parseNxRunAlias(rawCommand);
|
|
635
|
+
if (!alias || alias.projectName !== projectName) {
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
const nextTargetName = renamedTargets.get(alias.targetName);
|
|
639
|
+
if (!nextTargetName) {
|
|
640
|
+
continue;
|
|
641
|
+
}
|
|
642
|
+
scripts[scriptName] = nxRunAlias(projectName, nextTargetName, isContinuousTarget(nextTargetName, ''));
|
|
643
|
+
changed = true;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return changed;
|
|
647
|
+
}
|
|
648
|
+
function collectWorkspaceNamesTree(tree) {
|
|
649
|
+
const names = new Set();
|
|
650
|
+
for (const [, config] of getProjects(tree)) {
|
|
651
|
+
const pkgPath = `${config.root}/package.json`;
|
|
652
|
+
if (!tree.exists(pkgPath)) {
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
const pkg = readJson(tree, pkgPath);
|
|
656
|
+
const name = stringProperty(pkg, 'name');
|
|
657
|
+
if (name) {
|
|
658
|
+
names.add(name);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
return names;
|
|
662
|
+
}
|
|
663
|
+
function directoryContainsTestFilesTree(tree, path) {
|
|
664
|
+
if (!tree.exists(path)) {
|
|
665
|
+
return false;
|
|
666
|
+
}
|
|
667
|
+
for (const entry of tree.children(path)) {
|
|
668
|
+
if (entry === 'node_modules' || entry === 'dist' || entry === 'coverage' || entry === '.git') {
|
|
669
|
+
continue;
|
|
670
|
+
}
|
|
671
|
+
const entryPath = `${path}/${entry}`;
|
|
672
|
+
if (tree.isFile(entryPath)) {
|
|
673
|
+
const normalizedPath = entryPath.replaceAll('\\', '/');
|
|
674
|
+
if (normalizedPath.includes('/__tests__/')) {
|
|
675
|
+
return true;
|
|
676
|
+
}
|
|
677
|
+
if (/\.(?:test|spec)\.[cm]?[jt]sx?$/.test(entry)) {
|
|
678
|
+
return true;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
else {
|
|
682
|
+
if (directoryContainsTestFilesTree(tree, entryPath)) {
|
|
683
|
+
return true;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
return false;
|
|
688
|
+
}
|
|
689
|
+
function validateBuildZigPolicyTree(tree, packagePath) {
|
|
690
|
+
const path = `${packagePath}/build.zig`;
|
|
691
|
+
if (!tree.exists(path)) {
|
|
692
|
+
return [];
|
|
693
|
+
}
|
|
694
|
+
const content = tree.read(path, 'utf-8');
|
|
695
|
+
if (content && /\bb\.step\s*\(/.test(content)) {
|
|
696
|
+
return [];
|
|
697
|
+
}
|
|
698
|
+
return [{ path: packagePath, message: `${packagePath}/build.zig must define at least one b.step(...) target` }];
|
|
699
|
+
}
|
|
700
|
+
function validateTestEntrypointPresenceTree(tree, packagePath, pkg) {
|
|
701
|
+
if (packagePath === '.') {
|
|
702
|
+
return [];
|
|
703
|
+
}
|
|
704
|
+
if (!directoryContainsTestFilesTree(tree, packagePath) || hasTestEntrypoint(pkg)) {
|
|
705
|
+
return [];
|
|
706
|
+
}
|
|
707
|
+
return [{ path: packagePath, message: `${packagePath}: test files require scripts.test or nx.targets.test` }];
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Check package target policy using an Nx Tree.
|
|
711
|
+
* Uses `getProjects()` for project discovery instead of manual filesystem walking.
|
|
712
|
+
*/
|
|
713
|
+
export function checkPackageTargetPolicyTree(tree, options = {}) {
|
|
714
|
+
const issues = [];
|
|
715
|
+
const workspaceNames = collectWorkspaceNamesTree(tree);
|
|
716
|
+
for (const [projectName, config] of getProjects(tree)) {
|
|
717
|
+
const pkgPath = `${config.root}/package.json`;
|
|
718
|
+
if (!tree.exists(pkgPath)) {
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
const pkg = readJson(tree, pkgPath);
|
|
722
|
+
const packagePath = config.root;
|
|
723
|
+
const resolvedProject = options.resolvedTargetsByProject?.get(projectName);
|
|
724
|
+
const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
|
|
725
|
+
issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedTargets));
|
|
726
|
+
issues.push(...validateTestEntrypointPresenceTree(tree, packagePath, pkg));
|
|
727
|
+
issues.push(...validateBuildZigPolicyTree(tree, packagePath));
|
|
728
|
+
issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets }));
|
|
729
|
+
}
|
|
730
|
+
return issues;
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Apply package target policy using an Nx Tree.
|
|
734
|
+
* Uses `getProjects()` for project discovery and `updateJson()` for writes.
|
|
735
|
+
* Returns whether anything changed.
|
|
736
|
+
*/
|
|
737
|
+
export function applyPackageTargetPolicyTree(tree, options = {}) {
|
|
738
|
+
let changed = false;
|
|
739
|
+
const workspaceNames = collectWorkspaceNamesTree(tree);
|
|
740
|
+
for (const [projectName, config] of getProjects(tree)) {
|
|
741
|
+
const pkgPath = `${config.root}/package.json`;
|
|
742
|
+
if (!tree.exists(pkgPath)) {
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
const packagePath = config.root;
|
|
746
|
+
const resolvedProject = options.resolvedTargetsByProject?.get(projectName);
|
|
747
|
+
const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
|
|
748
|
+
updateJson(tree, pkgPath, (pkg) => {
|
|
749
|
+
let packageChanged = migratePackageColonTargets(pkg, resolvedTargets);
|
|
750
|
+
packageChanged = rewriteColonTargetDependenciesInPackage(pkg, resolvedTargets) || packageChanged;
|
|
751
|
+
packageChanged = removePackageColonTargets(pkg) || packageChanged;
|
|
752
|
+
packageChanged = removeRedundantNoopBuildTarget(pkg, resolvedProject) || packageChanged;
|
|
753
|
+
packageChanged =
|
|
754
|
+
applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, { resolvedTargets }) || packageChanged;
|
|
755
|
+
if (packageChanged) {
|
|
756
|
+
changed = true;
|
|
757
|
+
}
|
|
758
|
+
return pkg;
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
return changed;
|
|
762
|
+
}
|
|
763
|
+
function removePackageColonTargets(pkg) {
|
|
764
|
+
const nx = recordProperty(pkg, 'nx');
|
|
765
|
+
const targets = nx ? recordProperty(nx, 'targets') : null;
|
|
766
|
+
if (!targets) {
|
|
767
|
+
return false;
|
|
768
|
+
}
|
|
769
|
+
let changed = false;
|
|
770
|
+
for (const targetName of Object.keys(targets)) {
|
|
771
|
+
if (targetName.includes(':')) {
|
|
772
|
+
delete targets[targetName];
|
|
773
|
+
changed = true;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return changed;
|
|
777
|
+
}
|
|
778
|
+
function rewriteColonTargetDependenciesInPackage(pkg, resolvedTargets) {
|
|
779
|
+
const nx = recordProperty(pkg, 'nx');
|
|
780
|
+
const targets = nx ? recordProperty(nx, 'targets') : null;
|
|
781
|
+
if (!targets) {
|
|
782
|
+
return false;
|
|
783
|
+
}
|
|
784
|
+
const projectName = packageNxProjectName(pkg);
|
|
785
|
+
const scriptAliases = scriptTargetAliasesForProject(pkg, projectName);
|
|
786
|
+
let changed = false;
|
|
787
|
+
for (const target of Object.values(targets)) {
|
|
788
|
+
if (!isRecord(target) || !Array.isArray(target.dependsOn)) {
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
target.dependsOn = target.dependsOn.map((dependency) => {
|
|
792
|
+
if (typeof dependency !== 'string' || !dependency.includes(':')) {
|
|
793
|
+
return dependency;
|
|
794
|
+
}
|
|
795
|
+
const next = scriptAliases.get(dependency) ?? replacementTargetName(dependency, null, resolvedTargets);
|
|
796
|
+
if (!next) {
|
|
797
|
+
return dependency;
|
|
798
|
+
}
|
|
799
|
+
changed = true;
|
|
800
|
+
return next;
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
return changed;
|
|
804
|
+
}
|
|
805
|
+
function applyPackageScriptPolicyForPkg(pkg, _packagePath, workspaceNames, options = {}) {
|
|
806
|
+
if (!hasWorkspaceDependency(pkg, workspaceNames)) {
|
|
807
|
+
return false;
|
|
808
|
+
}
|
|
809
|
+
const scripts = recordProperty(pkg, 'scripts');
|
|
810
|
+
if (!scripts) {
|
|
811
|
+
return false;
|
|
812
|
+
}
|
|
813
|
+
const projectName = packageNxProjectName(pkg);
|
|
814
|
+
if (!projectName) {
|
|
815
|
+
return false;
|
|
816
|
+
}
|
|
817
|
+
const nxConfig = applyPackageNxConfig(pkg, { projectName, targets: true });
|
|
818
|
+
const targets = nxConfig.targets;
|
|
819
|
+
let changed = nxConfig.changed;
|
|
820
|
+
for (const [scriptName, rawCommand] of Object.entries(scripts)) {
|
|
821
|
+
if (typeof rawCommand !== 'string') {
|
|
822
|
+
continue;
|
|
823
|
+
}
|
|
824
|
+
const parsedAlias = parseNxRunAlias(rawCommand);
|
|
825
|
+
if (parsedAlias?.projectName === projectName && parsedAlias.targetName === 'test') {
|
|
826
|
+
continue;
|
|
827
|
+
}
|
|
828
|
+
const rewrite = classifyScriptRewrite(scriptName, rawCommand);
|
|
829
|
+
if (!rewrite) {
|
|
830
|
+
continue;
|
|
831
|
+
}
|
|
832
|
+
const targetName = rewrite.targetName;
|
|
833
|
+
const alias = nxRunAlias(projectName, targetName, rewrite.continuous);
|
|
834
|
+
if (targetName === 'test') {
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
const existingTarget = recordProperty(targets, targetName);
|
|
838
|
+
if (!existingTarget &&
|
|
839
|
+
targetName !== scriptName &&
|
|
840
|
+
targetExistsInResolvedProject(targetName, options.resolvedTargets)) {
|
|
841
|
+
if (scripts[scriptName] !== alias) {
|
|
842
|
+
scripts[scriptName] = alias;
|
|
843
|
+
changed = true;
|
|
844
|
+
}
|
|
845
|
+
continue;
|
|
846
|
+
}
|
|
847
|
+
const existingOptions = existingTarget ? recordProperty(existingTarget, 'options') : null;
|
|
848
|
+
const existingCommand = existingOptions ? stringProperty(existingOptions, 'command') : null;
|
|
849
|
+
const command = isScriptRunnerCommand(existingCommand, scriptName)
|
|
850
|
+
? rewrite.command
|
|
851
|
+
: (existingCommand ?? rewrite.command);
|
|
852
|
+
const target = existingTarget ?? {};
|
|
853
|
+
changed = setStringProperty(target, 'executor', 'nx:run-commands') || changed;
|
|
854
|
+
changed = applyTargetDependencyPolicy(target, targetName) || changed;
|
|
855
|
+
if (rewrite.continuous && target.continuous !== true) {
|
|
856
|
+
target.continuous = true;
|
|
857
|
+
changed = true;
|
|
858
|
+
}
|
|
859
|
+
const targetOptions = getOrCreateRecord(target, 'options');
|
|
860
|
+
changed = setStringProperty(targetOptions, 'command', command) || changed;
|
|
861
|
+
changed = setStringProperty(targetOptions, 'cwd', '{projectRoot}') || changed;
|
|
862
|
+
for (const [name, value] of Object.entries(rewrite.env)) {
|
|
863
|
+
const env = getOrCreateRecord(targetOptions, 'env');
|
|
864
|
+
changed = setStringProperty(env, name, value) || changed;
|
|
865
|
+
}
|
|
866
|
+
if (targets[targetName] !== target) {
|
|
867
|
+
targets[targetName] = target;
|
|
868
|
+
changed = true;
|
|
869
|
+
}
|
|
870
|
+
if (scripts[scriptName] !== alias) {
|
|
871
|
+
scripts[scriptName] = alias;
|
|
872
|
+
changed = true;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
return changed;
|
|
876
|
+
}
|
|
877
|
+
export function checkPackageTargets(pkg, packagePath, resolvedTargets) {
|
|
878
|
+
return [...validateExplicitNxTargets(pkg, packagePath, resolvedTargets)];
|
|
879
|
+
}
|
|
880
|
+
export function applyPackageTargets(pkg, packagePath, workspaceNames, options = {}) {
|
|
881
|
+
let changed = migratePackageColonTargets(pkg, options.resolvedTargets);
|
|
882
|
+
changed = rewriteColonTargetDependenciesInPackage(pkg, options.resolvedTargets) || changed;
|
|
883
|
+
changed = removePackageColonTargets(pkg) || changed;
|
|
884
|
+
changed = applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, options) || changed;
|
|
885
|
+
return changed;
|
|
886
|
+
}
|
|
887
|
+
export function checkPackageTargetPolicy(root, options = {}) {
|
|
888
|
+
const issues = [];
|
|
889
|
+
const workspaceNames = getWorkspacePackageNames(root);
|
|
890
|
+
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
891
|
+
const pkg = readJsonObject(packageJsonPath);
|
|
892
|
+
if (!pkg) {
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
const packagePath = packagePathFromJsonPath(root, packageJsonPath);
|
|
896
|
+
const projectName = packageNxProjectName(pkg);
|
|
897
|
+
const resolvedProject = projectName ? options.resolvedTargetsByProject?.get(projectName) : undefined;
|
|
898
|
+
const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
|
|
899
|
+
issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedTargets));
|
|
900
|
+
issues.push(...validateTestEntrypointPresence(root, packagePath, pkg));
|
|
901
|
+
issues.push(...validateBuildZigPolicy(root, packagePath));
|
|
902
|
+
issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets }));
|
|
903
|
+
}
|
|
904
|
+
return issues;
|
|
905
|
+
}
|
|
906
|
+
export function applyPackageTargetPolicy(root, options = {}) {
|
|
907
|
+
let changed = false;
|
|
908
|
+
const workspaceNames = getWorkspacePackageNames(root);
|
|
909
|
+
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
910
|
+
const pkg = readJsonObject(packageJsonPath);
|
|
911
|
+
if (!pkg) {
|
|
912
|
+
continue;
|
|
913
|
+
}
|
|
914
|
+
const packagePath = packagePathFromJsonPath(root, packageJsonPath);
|
|
915
|
+
const projectName = packageNxProjectName(pkg);
|
|
916
|
+
const resolvedProject = projectName ? options.resolvedTargetsByProject?.get(projectName) : undefined;
|
|
917
|
+
const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
|
|
918
|
+
let packageChanged = migratePackageColonTargets(pkg, resolvedTargets);
|
|
919
|
+
packageChanged = rewriteColonTargetDependenciesInPackage(pkg, resolvedTargets) || packageChanged;
|
|
920
|
+
packageChanged = removePackageColonTargets(pkg) || packageChanged;
|
|
921
|
+
packageChanged = removeRedundantNoopBuildTarget(pkg, resolvedProject) || packageChanged;
|
|
922
|
+
packageChanged =
|
|
923
|
+
applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, { resolvedTargets }) || packageChanged;
|
|
924
|
+
if (packageChanged) {
|
|
925
|
+
writeJsonObject(packageJsonPath, pkg);
|
|
926
|
+
changed = true;
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
return changed;
|
|
930
|
+
}
|