-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwizard.ts
More file actions
180 lines (160 loc) · 4.77 KB
/
wizard.ts
File metadata and controls
180 lines (160 loc) · 4.77 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
import path from 'node:path';
import {
type MonorepoTool,
asyncSequential,
formatAsciiTable,
getGitRoot,
logger,
toUnixPath,
} from '@code-pushup/utils';
import { promptCiProvider, resolveCi } from './ci.js';
import {
computeRelativePresetImport,
generateConfigSource,
generatePresetSource,
generateProjectSource,
} from './codegen.js';
import {
promptConfigFormat,
readPackageJson,
resolveFilename,
} from './config-format.js';
import { resolveGitignore } from './gitignore.js';
import {
addCodePushUpCommand,
listProjects,
promptSetupMode,
} from './monorepo.js';
import { promptPluginOptions, promptPluginSelection } from './prompts.js';
import type {
CliArgs,
FileChange,
PluginCodegenResult,
PluginSetupBinding,
ScopedPluginResult,
Tree,
WriteContext,
} from './types.js';
import { createTree } from './virtual-fs.js';
/**
* Runs the interactive setup wizard that generates a Code PushUp config file.
*
* All file changes are buffered in a virtual tree rooted at the git root,
* then flushed to disk in one step (or skipped on `--dry-run`).
*/
export async function runSetupWizard(
bindings: PluginSetupBinding[],
cliArgs: CliArgs,
): Promise<void> {
const targetDir = cliArgs['target-dir'] ?? process.cwd();
const context = await promptSetupMode(targetDir, cliArgs);
const selectedBindings = await promptPluginSelection(
bindings,
targetDir,
cliArgs,
);
const format = await promptConfigFormat(targetDir, cliArgs);
const ciProvider = await promptCiProvider(cliArgs);
const tree = createTree(await getGitRoot());
const resolved: ScopedPluginResult[] = await asyncSequential(
selectedBindings,
async binding => ({
scope: binding.scope ?? 'project',
result: await resolveBinding(binding, cliArgs, targetDir, tree),
}),
);
const packageJson = await readPackageJson(targetDir);
const isEsm = packageJson.type === 'module';
const configFilename = resolveFilename('code-pushup.config', format, isEsm);
const writeContext: WriteContext = { tree, format, configFilename, isEsm };
await (context.mode === 'monorepo' && context.tool != null
? writeMonorepoConfigs(writeContext, resolved, targetDir, context.tool)
: writeStandaloneConfig(
writeContext,
resolved.map(r => r.result),
));
await resolveGitignore(tree);
await resolveCi(tree, ciProvider, context);
logChanges(tree.listChanges());
if (cliArgs['dry-run']) {
logger.info('Dry run — no files written.');
return;
}
await tree.flush();
logger.info('Setup complete.');
logger.newline();
logNextSteps([
['npx code-pushup', 'Collect your first report'],
['https://github.com/code-pushup/cli#readme', 'Documentation'],
]);
}
async function resolveBinding(
binding: PluginSetupBinding,
cliArgs: CliArgs,
targetDir: string,
tree: Pick<Tree, 'read' | 'write'>,
): Promise<PluginCodegenResult> {
const descriptors = binding.prompts ? await binding.prompts(targetDir) : [];
const answers =
descriptors.length > 0
? await promptPluginOptions(descriptors, cliArgs)
: {};
return binding.generateConfig(answers, tree);
}
async function writeStandaloneConfig(
{ tree, format, configFilename }: WriteContext,
results: PluginCodegenResult[],
): Promise<void> {
await tree.write(configFilename, generateConfigSource(results, format));
}
async function writeMonorepoConfigs(
{ tree, format, configFilename, isEsm }: WriteContext,
resolved: ScopedPluginResult[],
targetDir: string,
tool: MonorepoTool,
): Promise<void> {
const projectResults = resolved
.filter(r => r.scope === 'project')
.map(r => r.result);
const rootResults = resolved
.filter(r => r.scope === 'root')
.map(r => r.result);
const presetFilename = resolveFilename('code-pushup.preset', format, isEsm);
await tree.write(
presetFilename,
generatePresetSource(projectResults, format),
);
if (rootResults.length > 0) {
await tree.write(configFilename, generateConfigSource(rootResults, format));
}
const projects = await listProjects(targetDir, tool);
await Promise.all(
projects.map(async project => {
const importPath = computeRelativePresetImport(
project.relativeDir,
presetFilename,
);
await tree.write(
toUnixPath(path.join(project.relativeDir, configFilename)),
generateProjectSource(project.name, importPath),
);
await addCodePushUpCommand(tree, project, tool);
}),
);
}
function logChanges(changes: FileChange[]): void {
changes.forEach(change => {
logger.info(`${change.type} ${change.path}`);
});
}
function logNextSteps(steps: [string, string][]): void {
logger.info(
formatAsciiTable(
{
title: 'Next steps:',
rows: steps,
},
{ borderless: true },
),
);
}