11import path from 'node:path' ;
2- import type { CategoryRef } from '@code-pushup/models' ;
3- import {
4- mergeCategoriesBySlug ,
5- singleQuote ,
6- toUnixPath ,
7- } from '@code-pushup/utils' ;
2+ import { exists , toUnixPath } from '@code-pushup/utils' ;
3+ import { addCategories } from './codegen-categories.js' ;
84import type {
95 ConfigFileFormat ,
106 ImportDeclarationStructure ,
@@ -17,7 +13,7 @@ const CORE_CONFIG_IMPORT: ImportDeclarationStructure = {
1713 isTypeOnly : true ,
1814} ;
1915
20- class CodeBuilder {
16+ export class CodeBuilder {
2117 private lines : string [ ] = [ ] ;
2218
2319 addLine ( text : string , depth = 0 ) : void {
@@ -45,6 +41,7 @@ export function generateConfigSource(
4541) : string {
4642 const builder = new CodeBuilder ( ) ;
4743 addImports ( builder , collectImports ( plugins , format ) ) ;
44+ addPluginDeclarations ( builder , plugins ) ;
4845 if ( format === 'ts' ) {
4946 builder . addLine ( 'export default {' ) ;
5047 addPlugins ( builder , plugins ) ;
@@ -66,6 +63,7 @@ export function generatePresetSource(
6663) : string {
6764 const builder = new CodeBuilder ( ) ;
6865 addImports ( builder , collectImports ( plugins , format ) ) ;
66+ addPluginDeclarations ( builder , plugins ) ;
6967 addPresetExport ( builder , plugins , format ) ;
7068 return builder . toString ( ) ;
7169}
@@ -137,6 +135,20 @@ function addImports(
137135 }
138136}
139137
138+ function addPluginDeclarations (
139+ builder : CodeBuilder ,
140+ plugins : PluginCodegenResult [ ] ,
141+ ) : void {
142+ const declarations = plugins
143+ . map ( ( { pluginDeclaration } ) => pluginDeclaration )
144+ . filter ( exists )
145+ . map ( d => `const ${ d . identifier } = ${ d . expression } ;` ) ;
146+ if ( declarations . length > 0 ) {
147+ builder . addLines ( declarations ) ;
148+ builder . addEmptyLine ( ) ;
149+ }
150+ }
151+
140152function addPlugins (
141153 builder : CodeBuilder ,
142154 plugins : PluginCodegenResult [ ] ,
@@ -183,37 +195,3 @@ function addPresetExport(
183195 builder . addLine ( '};' , 1 ) ;
184196 builder . addLine ( '}' ) ;
185197}
186-
187- function addCategories (
188- builder : CodeBuilder ,
189- plugins : PluginCodegenResult [ ] ,
190- depth = 1 ,
191- ) : void {
192- const categories = mergeCategoriesBySlug (
193- plugins . flatMap ( p => p . categories ?? [ ] ) ,
194- ) ;
195- if ( categories . length === 0 ) {
196- return ;
197- }
198- builder . addLine ( 'categories: [' , depth ) ;
199- categories . forEach ( ( { slug, title, description, docsUrl, refs } ) => {
200- builder . addLine ( '{' , depth + 1 ) ;
201- builder . addLine ( `slug: '${ slug } ',` , depth + 2 ) ;
202- builder . addLine ( `title: ${ singleQuote ( title ) } ,` , depth + 2 ) ;
203- if ( description ) {
204- builder . addLine ( `description: ${ singleQuote ( description ) } ,` , depth + 2 ) ;
205- }
206- if ( docsUrl ) {
207- builder . addLine ( `docsUrl: ${ singleQuote ( docsUrl ) } ,` , depth + 2 ) ;
208- }
209- builder . addLine ( 'refs: [' , depth + 2 ) ;
210- builder . addLines ( refs . map ( formatCategoryRef ) , depth + 3 ) ;
211- builder . addLine ( '],' , depth + 2 ) ;
212- builder . addLine ( '},' , depth + 1 ) ;
213- } ) ;
214- builder . addLine ( '],' , depth ) ;
215- }
216-
217- function formatCategoryRef ( ref : CategoryRef ) : string {
218- return `{ type: '${ ref . type } ', plugin: '${ ref . plugin } ', slug: '${ ref . slug } ', weight: ${ ref . weight } },` ;
219- }
0 commit comments