-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathnx.ts
More file actions
86 lines (80 loc) · 1.96 KB
/
nx.ts
File metadata and controls
86 lines (80 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import path from 'node:path';
import {
executeProcess,
fileExists,
interpolate,
stringifyError,
toArray,
} from '@code-pushup/utils';
import type { MonorepoToolHandler } from '../tools.js';
export const nxHandler: MonorepoToolHandler = {
tool: 'nx',
async isConfigured(options) {
return (
(await fileExists(path.join(options.cwd, 'nx.json'))) &&
(
await executeProcess({
command: 'npx',
args: ['nx', 'report'],
cwd: options.cwd,
ignoreExitCode: true,
})
).code === 0
);
},
async listProjects({ cwd, task, nxProjectsFilter }) {
const { stdout } = await executeProcess({
command: 'npx',
args: [
'nx',
'show',
'projects',
...toArray(nxProjectsFilter).map(arg => interpolate(arg, { task })),
'--json',
],
cwd,
});
const projects = parseProjects(stdout);
return projects.toSorted().map(project => ({
name: project,
bin: `npx nx run ${project}:${task} --`,
}));
},
createRunManyCommand(options, projects) {
const projectNames: string[] =
projects.only ?? projects.all.map(({ name }) => name);
return [
'npx',
'nx',
'run-many',
`--targets=${options.task}`,
`--parallel=${options.parallel}`,
`--projects=${projectNames.join(',')}`,
'--',
].join(' ');
},
};
function parseProjects(stdout: string): string[] {
// eslint-disable-next-line functional/no-let
let json: unknown;
try {
json = JSON.parse(stdout);
} catch (error) {
throw new Error(
`Invalid non-JSON output from 'nx show projects' - ${stringifyError(
error,
)}`,
);
}
if (
Array.isArray(json) &&
json.every((item): item is string => typeof item === 'string')
) {
return json;
}
throw new Error(
`Invalid JSON output from 'nx show projects', expected array of strings, received ${JSON.stringify(
json,
)}`,
);
}