1+ import path from 'node:path' ;
12import type {
23 ConfigFileFormat ,
34 ImportDeclarationStructure ,
@@ -32,6 +33,60 @@ class CodeBuilder {
3233 }
3334}
3435
36+ export function generateConfigSource (
37+ plugins : PluginCodegenResult [ ] ,
38+ format : ConfigFileFormat ,
39+ ) : string {
40+ const builder = new CodeBuilder ( ) ;
41+ addImports ( builder , collectImports ( plugins , format ) ) ;
42+ if ( format === 'ts' ) {
43+ builder . addLine ( 'export default {' ) ;
44+ addPlugins ( builder , plugins ) ;
45+ builder . addLine ( '} satisfies CoreConfig;' ) ;
46+ } else {
47+ builder . addLine ( "/** @type {import('@code-pushup/models').CoreConfig} */" ) ;
48+ builder . addLine ( 'export default {' ) ;
49+ addPlugins ( builder , plugins ) ;
50+ builder . addLine ( '};' ) ;
51+ }
52+ return builder . toString ( ) ;
53+ }
54+
55+ export function generatePresetSource (
56+ plugins : PluginCodegenResult [ ] ,
57+ format : ConfigFileFormat ,
58+ ) : string {
59+ const builder = new CodeBuilder ( ) ;
60+ addImports ( builder , collectImports ( plugins , format ) ) ;
61+ addPresetExport ( builder , plugins , format ) ;
62+ return builder . toString ( ) ;
63+ }
64+
65+ export function generateProjectSource (
66+ projectName : string ,
67+ presetImportPath : string ,
68+ ) : string {
69+ const builder = new CodeBuilder ( ) ;
70+ builder . addLine (
71+ formatImport ( {
72+ moduleSpecifier : presetImportPath ,
73+ namedImports : [ 'createConfig' ] ,
74+ } ) ,
75+ ) ;
76+ builder . addEmptyLine ( ) ;
77+ builder . addLine ( `export default await createConfig('${ projectName } ');` ) ;
78+ return builder . toString ( ) ;
79+ }
80+
81+ export function computeRelativePresetImport (
82+ projectRelativeDir : string ,
83+ presetFilename : string ,
84+ ) : string {
85+ const relativePath = path . relative ( projectRelativeDir , presetFilename ) ;
86+ const importPath = relativePath . replace ( / \. t s $ / , '.js' ) ;
87+ return importPath . startsWith ( '.' ) ? importPath : `./${ importPath } ` ;
88+ }
89+
3590function formatImport ( {
3691 moduleSpecifier,
3792 defaultImport,
@@ -45,76 +100,77 @@ function formatImport({
45100 return `import ${ type } ${ from } '${ moduleSpecifier } ';` ;
46101}
47102
48- function collectTsImports (
49- plugins : PluginCodegenResult [ ] ,
103+ function sortImports (
104+ imports : ImportDeclarationStructure [ ] ,
50105) : ImportDeclarationStructure [ ] {
51- return [
52- CORE_CONFIG_IMPORT ,
53- ...plugins . flatMap ( ( { imports } ) => imports ) ,
54- ] . toSorted ( ( a , b ) => a . moduleSpecifier . localeCompare ( b . moduleSpecifier ) ) ;
106+ return imports . toSorted ( ( a , b ) =>
107+ a . moduleSpecifier . localeCompare ( b . moduleSpecifier ) ,
108+ ) ;
55109}
56110
57- function collectJsImports (
111+ function collectImports (
58112 plugins : PluginCodegenResult [ ] ,
113+ format : ConfigFileFormat ,
59114) : ImportDeclarationStructure [ ] {
60- return plugins
61- . flatMap ( ( { imports } ) => imports )
62- . map ( ( { isTypeOnly : _ , ...rest } ) => rest )
63- . toSorted ( ( a , b ) => a . moduleSpecifier . localeCompare ( b . moduleSpecifier ) ) ;
115+ const pluginImports = plugins . flatMap ( ( { imports } ) => imports ) ;
116+ if ( format === 'ts' ) {
117+ return sortImports ( [ CORE_CONFIG_IMPORT , ...pluginImports ] ) ;
118+ }
119+ return sortImports ( pluginImports . map ( ( { isTypeOnly : _ , ...rest } ) => rest ) ) ;
120+ }
121+
122+ function addImports (
123+ builder : CodeBuilder ,
124+ imports : ImportDeclarationStructure [ ] ,
125+ ) : void {
126+ if ( imports . length > 0 ) {
127+ builder . addLines ( imports . map ( formatImport ) ) ;
128+ builder . addEmptyLine ( ) ;
129+ }
64130}
65131
66132function addPlugins (
67133 builder : CodeBuilder ,
68134 plugins : PluginCodegenResult [ ] ,
135+ depth = 1 ,
69136) : void {
137+ builder . addLine ( 'plugins: [' , depth ) ;
70138 if ( plugins . length === 0 ) {
71- builder . addLine ( 'plugins: [' , 1 ) ;
72- builder . addLine ( '// TODO: register some plugins' , 2 ) ;
73- builder . addLine ( '],' , 1 ) ;
139+ builder . addLine ( '// TODO: register some plugins' , depth + 1 ) ;
74140 } else {
75- builder . addLine ( 'plugins: [' , 1 ) ;
76141 builder . addLines (
77142 plugins . map ( ( { pluginInit } ) => `${ pluginInit } ,` ) ,
78- 2 ,
143+ depth + 1 ,
79144 ) ;
80- builder . addLine ( '],' , 1 ) ;
81145 }
146+ builder . addLine ( '],' , depth ) ;
82147}
83148
84- export function generateConfigSource (
149+ function addPresetExport (
150+ builder : CodeBuilder ,
85151 plugins : PluginCodegenResult [ ] ,
86152 format : ConfigFileFormat ,
87- ) : string {
88- return format === 'ts'
89- ? generateTsConfig ( plugins )
90- : generateJsConfig ( plugins ) ;
91- }
92-
93- function generateTsConfig ( plugins : PluginCodegenResult [ ] ) : string {
94- const builder = new CodeBuilder ( ) ;
95-
96- builder . addLines ( collectTsImports ( plugins ) . map ( formatImport ) ) ;
97- builder . addEmptyLine ( ) ;
98- builder . addLine ( 'export default {' ) ;
99- addPlugins ( builder , plugins ) ;
100- builder . addLine ( '} satisfies CoreConfig;' ) ;
101-
102- return builder . toString ( ) ;
103- }
104-
105- function generateJsConfig ( plugins : PluginCodegenResult [ ] ) : string {
106- const builder = new CodeBuilder ( ) ;
107-
108- const pluginImports = collectJsImports ( plugins ) ;
109- if ( pluginImports . length > 0 ) {
110- builder . addLines ( pluginImports . map ( formatImport ) ) ;
111- builder . addEmptyLine ( ) ;
153+ ) : void {
154+ if ( format === 'ts' ) {
155+ builder . addLines ( [
156+ '/**' ,
157+ ' * Creates a Code PushUp config for a project.' ,
158+ ' * @param project Project name' ,
159+ ' */' ,
160+ 'export async function createConfig(project: string): Promise<CoreConfig> {' ,
161+ ] ) ;
162+ } else {
163+ builder . addLines ( [
164+ '/**' ,
165+ ' * Creates a Code PushUp config for a project.' ,
166+ ' * @param {string} project Project name' ,
167+ " * @returns {Promise<import('@code-pushup/models').CoreConfig>}" ,
168+ ' */' ,
169+ 'export async function createConfig(project) {' ,
170+ ] ) ;
112171 }
113-
114- builder . addLine ( "/** @type {import('@code-pushup/models').CoreConfig} */" ) ;
115- builder . addLine ( 'export default {' ) ;
116- addPlugins ( builder , plugins ) ;
117- builder . addLine ( '};' ) ;
118-
119- return builder . toString ( ) ;
172+ builder . addLine ( 'return {' , 1 ) ;
173+ addPlugins ( builder , plugins , 2 ) ;
174+ builder . addLine ( '};' , 1 ) ;
175+ builder . addLine ( '}' ) ;
120176}
0 commit comments