-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmonorepo.ts
More file actions
205 lines (188 loc) · 5.25 KB
/
monorepo.ts
File metadata and controls
205 lines (188 loc) · 5.25 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { select } from '@inquirer/prompts';
import { createRequire } from 'node:module';
import path from 'node:path';
import {
MONOREPO_TOOL_DETECTORS,
type MonorepoTool,
type WorkspacePackage,
detectMonorepoTool,
hasScript,
listPackages,
listWorkspaces,
logger,
readPnpmWorkspacePatterns,
stringifyError,
toUnixPath,
} from '@code-pushup/utils';
import type {
CliArgs,
ConfigContext,
SetupMode,
Tree,
WizardProject,
} from './types.js';
const TASK_NAME = 'code-pushup';
export async function promptSetupMode(
targetDir: string,
cliArgs: CliArgs,
): Promise<ConfigContext> {
switch (cliArgs.mode) {
case 'standalone':
return toContext(cliArgs.mode, null);
case 'monorepo': {
const tool = await detectMonorepoTool(targetDir);
return toContext(cliArgs.mode, tool);
}
case undefined: {
const tool = await detectMonorepoTool(targetDir);
const mode = cliArgs.yes ? inferMode(tool) : await promptMode(tool);
return toContext(mode, tool);
}
}
}
async function promptMode(tool: MonorepoTool | null): Promise<SetupMode> {
return select<SetupMode>({
message: 'Setup mode:',
choices: [
{ name: 'Standalone (single config)', value: 'standalone' },
{ name: 'Monorepo (per-project configs)', value: 'monorepo' },
],
default: inferMode(tool),
});
}
function inferMode(tool: MonorepoTool | null): SetupMode {
return tool ? 'monorepo' : 'standalone';
}
function toContext(mode: SetupMode, tool: MonorepoTool | null): ConfigContext {
if (mode === 'monorepo' && tool == null) {
logger.warn('No monorepo tool detected, falling back to standalone mode.');
return { mode: 'standalone', tool: null };
}
return { mode, tool };
}
export async function listProjects(
cwd: string,
tool: MonorepoTool,
): Promise<WizardProject[]> {
switch (tool) {
case 'nx':
return listNxProjects(cwd);
case 'pnpm':
return listPnpmProjects(cwd);
case 'turbo':
return listTurboProjects(cwd);
case 'yarn':
case 'npm':
return listWorkspaceProjects(cwd);
}
}
async function listNxProjects(cwd: string): Promise<WizardProject[]> {
const require = createRequire(path.join(cwd, 'package.json'));
const { readCachedProjectGraph, createProjectGraphAsync } =
require('@nx/devkit') as typeof import('@nx/devkit');
const graph = await loadProjectGraph(
readCachedProjectGraph,
createProjectGraphAsync,
);
return Object.values(graph.nodes).map(({ name, data }) => ({
name,
directory: path.join(cwd, data.root),
relativeDir: toUnixPath(data.root),
}));
}
async function listPnpmProjects(cwd: string): Promise<WizardProject[]> {
const patterns = await readPnpmWorkspacePatterns(cwd);
const packages = await listPackages(cwd, patterns);
return packages.map(pkg => toProject(cwd, pkg));
}
async function listTurboProjects(cwd: string): Promise<WizardProject[]> {
if (await MONOREPO_TOOL_DETECTORS.pnpm(cwd)) {
return listPnpmProjects(cwd);
}
return listWorkspaceProjects(cwd);
}
async function listWorkspaceProjects(cwd: string): Promise<WizardProject[]> {
const { workspaces } = await listWorkspaces(cwd);
return workspaces.map(pkg => toProject(cwd, pkg));
}
export async function addCodePushUpCommand(
tree: Tree,
project: WizardProject,
tool: MonorepoTool | null,
): Promise<void> {
if (tool === 'nx') {
const added = await addNxTarget(tree, project);
if (added) {
return;
}
}
await addPackageJsonScript(tree, project);
}
async function addNxTarget(
tree: Tree,
project: WizardProject,
): Promise<boolean> {
const filePath = toUnixPath(path.join(project.relativeDir, 'project.json'));
const raw = await tree.read(filePath);
if (raw == null) {
return false;
}
const config = JSON.parse(raw);
if (config.targets?.[TASK_NAME] != null) {
return true;
}
const updated = {
...config,
targets: {
...config.targets,
[TASK_NAME]: {
executor: 'nx:run-commands',
options: { command: 'npx code-pushup' },
},
},
};
await tree.write(filePath, `${JSON.stringify(updated, null, 2)}\n`);
return true;
}
async function addPackageJsonScript(
tree: Tree,
project: WizardProject,
): Promise<void> {
const filePath = toUnixPath(path.join(project.relativeDir, 'package.json'));
const raw = await tree.read(filePath);
if (raw == null) {
return;
}
const packageJson = JSON.parse(raw);
if (hasScript(packageJson, TASK_NAME)) {
return;
}
const updated = {
...packageJson,
scripts: {
...packageJson.scripts,
[TASK_NAME]: 'code-pushup',
},
};
await tree.write(filePath, `${JSON.stringify(updated, null, 2)}\n`);
}
function toProject(cwd: string, pkg: WorkspacePackage): WizardProject {
return {
name: pkg.name,
directory: pkg.directory,
relativeDir: toUnixPath(path.relative(cwd, pkg.directory)),
};
}
async function loadProjectGraph(
readCached: typeof import('@nx/devkit').readCachedProjectGraph,
createAsync: typeof import('@nx/devkit').createProjectGraphAsync,
) {
try {
return readCached();
} catch (error) {
logger.warn(
`Could not read cached project graph, falling back to async creation.\n${stringifyError(error)}`,
);
return createAsync({ exitOnError: false });
}
}