@@ -3,9 +3,11 @@ import path from 'node:path';
33import type {
44 Audit ,
55 AuditOutput ,
6+ PluginArtifactOptions ,
67 RunnerConfig ,
78 RunnerFilesPaths ,
89} from '@code-pushup/models' ;
10+ import { pluginArtifactOptionsSchema } from '@code-pushup/models' ;
911import {
1012 asyncSequential ,
1113 createRunnerFiles ,
@@ -51,23 +53,60 @@ export async function createRunnerConfig(
5153 scriptPath : string ,
5254 audits : Audit [ ] ,
5355 targets : ESLintTarget [ ] ,
56+ artifactOptions ?: PluginArtifactOptions ,
5457) : Promise < RunnerConfig > {
58+ const parsedOptions = artifactOptions
59+ ? pluginArtifactOptionsSchema . parse ( artifactOptions )
60+ : undefined ;
61+
5562 const config : ESLintPluginRunnerConfig = {
5663 targets,
57- slugs : audits . map ( audit => audit . slug ) ,
64+ slugs : audits . map ( a => a . slug ) ,
5865 } ;
59- const { runnerConfigPath, runnerOutputPath } = await createRunnerFiles (
60- 'eslint' ,
61- JSON . stringify ( config ) ,
62- ) ;
66+
67+ const { runnerConfigPath, runnerOutputPath } = parsedOptions
68+ ? await createCustomRunnerPaths ( parsedOptions , config )
69+ : await createRunnerFiles ( 'eslint' , JSON . stringify ( config ) ) ;
70+
71+ const args = [
72+ filePathToCliArg ( scriptPath ) ,
73+ ...objectToCliArgs ( { runnerConfigPath, runnerOutputPath } ) ,
74+ ...resolveCommandArgs ( parsedOptions ?. generateArtifactsCommand ) ,
75+ ] ;
6376
6477 return {
6578 command : 'node' ,
66- args : [
67- filePathToCliArg ( scriptPath ) ,
68- ...objectToCliArgs ( { runnerConfigPath, runnerOutputPath } ) ,
69- ] ,
79+ args,
7080 configFile : runnerConfigPath ,
7181 outputFile : runnerOutputPath ,
7282 } ;
7383}
84+
85+ async function createCustomRunnerPaths (
86+ options : PluginArtifactOptions ,
87+ config : ESLintPluginRunnerConfig ,
88+ ) : Promise < RunnerFilesPaths > {
89+ const artifactPaths = Array . isArray ( options . artifactsPaths )
90+ ? options . artifactsPaths
91+ : [ options . artifactsPaths ] ;
92+
93+ const runnerOutputPath = artifactPaths [ 0 ] ?? '' ;
94+ const runnerConfigPath = path . join (
95+ path . dirname ( runnerOutputPath ) ,
96+ 'plugin-config.json' ,
97+ ) ;
98+
99+ await ensureDirectoryExists ( path . dirname ( runnerConfigPath ) ) ;
100+ await writeFile ( runnerConfigPath , JSON . stringify ( config ) ) ;
101+
102+ return { runnerConfigPath, runnerOutputPath } ;
103+ }
104+
105+ function resolveCommandArgs (
106+ command ?: string | { command : string ; args ?: string [ ] } ,
107+ ) : string [ ] {
108+ if ( ! command ) return [ ] ;
109+ return typeof command === 'string'
110+ ? [ command ]
111+ : [ command . command , ...( command . args ?? [ ] ) ] ;
112+ }
0 commit comments