-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcodegen.ts
More file actions
219 lines (201 loc) · 5.92 KB
/
codegen.ts
File metadata and controls
219 lines (201 loc) · 5.92 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import path from 'node:path';
import type { CategoryRef } from '@code-pushup/models';
import {
mergeCategoriesBySlug,
singleQuote,
toUnixPath,
} from '@code-pushup/utils';
import type {
ConfigFileFormat,
ImportDeclarationStructure,
PluginCodegenResult,
} from './types.js';
const CORE_CONFIG_IMPORT: ImportDeclarationStructure = {
moduleSpecifier: '@code-pushup/models',
namedImports: ['CoreConfig'],
isTypeOnly: true,
};
class CodeBuilder {
private lines: string[] = [];
addLine(text: string, depth = 0): void {
this.lines.push(`${' '.repeat(depth)}${text}`);
}
addLines(texts: string[], depth = 0): void {
texts.forEach(text => {
this.addLine(text, depth);
});
}
addEmptyLine(): void {
this.lines.push('');
}
toString(): string {
return `${this.lines.join('\n')}\n`;
}
}
export function generateConfigSource(
plugins: PluginCodegenResult[],
format: ConfigFileFormat,
): string {
const builder = new CodeBuilder();
addImports(builder, collectImports(plugins, format));
if (format === 'ts') {
builder.addLine('export default {');
addPlugins(builder, plugins);
addCategories(builder, plugins);
builder.addLine('} satisfies CoreConfig;');
} else {
builder.addLine("/** @type {import('@code-pushup/models').CoreConfig} */");
builder.addLine('export default {');
addPlugins(builder, plugins);
addCategories(builder, plugins);
builder.addLine('};');
}
return builder.toString();
}
export function generatePresetSource(
plugins: PluginCodegenResult[],
format: ConfigFileFormat,
): string {
const builder = new CodeBuilder();
addImports(builder, collectImports(plugins, format));
addPresetExport(builder, plugins, format);
return builder.toString();
}
export function generateProjectSource(
projectName: string,
presetImportPath: string,
): string {
const builder = new CodeBuilder();
builder.addLine(
formatImport({
moduleSpecifier: presetImportPath,
namedImports: ['createConfig'],
}),
);
builder.addEmptyLine();
builder.addLine(`export default await createConfig('${projectName}');`);
return builder.toString();
}
export function computeRelativePresetImport(
projectRelativeDir: string,
presetFilename: string,
): string {
const relativePath = path.relative(projectRelativeDir, presetFilename);
const importPath = toUnixPath(relativePath).replace(/\.ts$/, '.js');
return importPath.startsWith('.') ? importPath : `./${importPath}`;
}
function formatImport({
moduleSpecifier,
defaultImport,
namedImports,
isTypeOnly,
}: ImportDeclarationStructure): string {
const named = namedImports?.length ? `{ ${namedImports.join(', ')} }` : '';
const bindings = [defaultImport, named].filter(Boolean).join(', ');
const from = bindings ? `${bindings} from ` : '';
const type = isTypeOnly ? 'type ' : '';
return `import ${type}${from}'${moduleSpecifier}';`;
}
function sortImports(
imports: ImportDeclarationStructure[],
): ImportDeclarationStructure[] {
return imports.toSorted((a, b) =>
a.moduleSpecifier.localeCompare(b.moduleSpecifier),
);
}
function collectImports(
plugins: PluginCodegenResult[],
format: ConfigFileFormat,
): ImportDeclarationStructure[] {
const pluginImports = plugins.flatMap(({ imports }) => imports);
if (format === 'ts') {
return sortImports([CORE_CONFIG_IMPORT, ...pluginImports]);
}
return sortImports(pluginImports.map(({ isTypeOnly: _, ...rest }) => rest));
}
function addImports(
builder: CodeBuilder,
imports: ImportDeclarationStructure[],
): void {
if (imports.length > 0) {
builder.addLines(imports.map(formatImport));
builder.addEmptyLine();
}
}
function addPlugins(
builder: CodeBuilder,
plugins: PluginCodegenResult[],
depth = 1,
): void {
builder.addLine('plugins: [', depth);
if (plugins.length === 0) {
builder.addLine('// TODO: register some plugins', depth + 1);
} else {
builder.addLines(
plugins.map(({ pluginInit }) => `${pluginInit},`),
depth + 1,
);
}
builder.addLine('],', depth);
}
function addPresetExport(
builder: CodeBuilder,
plugins: PluginCodegenResult[],
format: ConfigFileFormat,
): void {
if (format === 'ts') {
builder.addLines([
'/**',
' * Creates a Code PushUp config for a project.',
' * @param project Project name',
' */',
'export async function createConfig(project: string): Promise<CoreConfig> {',
]);
} else {
builder.addLines([
'/**',
' * Creates a Code PushUp config for a project.',
' * @param {string} project Project name',
" * @returns {Promise<import('@code-pushup/models').CoreConfig>}",
' */',
'export async function createConfig(project) {',
]);
}
builder.addLine('return {', 1);
addPlugins(builder, plugins, 2);
addCategories(builder, plugins, 2);
builder.addLine('};', 1);
builder.addLine('}');
}
function addCategories(
builder: CodeBuilder,
plugins: PluginCodegenResult[],
depth = 1,
): void {
const categories = mergeCategoriesBySlug(
plugins.flatMap(p => p.categories ?? []),
);
if (categories.length === 0) {
return;
}
builder.addLine('categories: [', depth);
categories.forEach(({ slug, title, description, docsUrl, refs }) => {
builder.addLine('{', depth + 1);
builder.addLine(`slug: '${slug}',`, depth + 2);
builder.addLine(`title: ${singleQuote(title)},`, depth + 2);
if (description) {
builder.addLine(`description: ${singleQuote(description)},`, depth + 2);
}
if (docsUrl) {
builder.addLine(`docsUrl: ${singleQuote(docsUrl)},`, depth + 2);
}
builder.addLine('refs: [', depth + 2);
builder.addLines(refs.map(formatCategoryRef), depth + 3);
builder.addLine('],', depth + 2);
builder.addLine('},', depth + 1);
});
builder.addLine('],', depth);
}
function formatCategoryRef(ref: CategoryRef): string {
return `{ type: '${ref.type}', plugin: '${ref.plugin}', slug: '${ref.slug}', weight: ${ref.weight} },`;
}