@@ -4,11 +4,12 @@ import type {
44 Audit ,
55 AuditOutput ,
66 AuditOutputs ,
7+ PersistConfig ,
78 PluginArtifactOptions ,
89 RunnerConfig ,
910 RunnerFilesPaths ,
11+ RunnerFunction ,
1012} from '@code-pushup/models' ;
11- import { pluginArtifactOptionsSchema } from '@code-pushup/models' ;
1213import {
1314 asyncSequential ,
1415 createRunnerFiles ,
@@ -55,91 +56,62 @@ export async function createRunnerConfig(
5556 scriptPath : string ,
5657 audits : Audit [ ] ,
5758 targets : ESLintTarget [ ] ,
58- artifactOptions ?: PluginArtifactOptions ,
5959) : Promise < RunnerConfig > {
60- const parsedOptions = artifactOptions
61- ? pluginArtifactOptionsSchema . parse ( artifactOptions )
62- : undefined ;
63-
6460 const config : ESLintPluginRunnerConfig = {
6561 targets,
66- slugs : audits . map ( a => a . slug ) ,
62+ slugs : audits . map ( audit => audit . slug ) ,
6763 } ;
68-
69- const { runnerConfigPath, runnerOutputPath } = parsedOptions
70- ? await createCustomRunnerPaths ( parsedOptions , config )
71- : await createRunnerFiles ( 'eslint' , JSON . stringify ( config ) ) ;
72-
73- const args = [
74- filePathToCliArg ( scriptPath ) ,
75- ...objectToCliArgs ( { runnerConfigPath, runnerOutputPath } ) ,
76- ...resolveCommandArgs ( parsedOptions ?. generateArtifactsCommand ) ,
77- ] ;
64+ const { runnerConfigPath, runnerOutputPath } = await createRunnerFiles (
65+ 'eslint' ,
66+ JSON . stringify ( config ) ,
67+ ) ;
7868
7969 return {
8070 command : 'node' ,
81- args,
71+ args : [
72+ filePathToCliArg ( scriptPath ) ,
73+ ...objectToCliArgs ( { runnerConfigPath, runnerOutputPath } ) ,
74+ ] ,
8275 configFile : runnerConfigPath ,
8376 outputFile : runnerOutputPath ,
8477 } ;
8578}
8679
87- export async function generateAuditOutputs ( options : {
80+ export function createRunnerFunction ( options : {
8881 audits : Audit [ ] ;
8982 targets : ESLintTarget [ ] ;
9083 artifacts ?: PluginArtifactOptions ;
91- } ) : Promise < AuditOutputs > {
84+ } ) : RunnerFunction {
9285 const { audits, targets, artifacts } = options ;
9386 const config : ESLintPluginRunnerConfig = {
9487 targets,
9588 slugs : audits . map ( audit => audit . slug ) ,
9689 } ;
9790
98- ui ( ) . logger . log ( `ESLint plugin executing ${ targets . length } lint targets` ) ;
99-
100- const linterOutputs = artifacts
101- ? await loadArtifacts ( artifacts )
102- : await asyncSequential ( targets , lint ) ;
103- const lintResults = mergeLinterOutputs ( linterOutputs ) ;
104- const failedAudits = lintResultsToAudits ( lintResults ) ;
105-
106- return config . slugs . map (
107- ( slug ) : AuditOutput =>
108- failedAudits . find ( audit => audit . slug === slug ) ?? {
109- slug,
110- score : 1 ,
111- value : 0 ,
112- displayValue : 'passed' ,
113- details : { issues : [ ] } ,
114- } ,
115- ) ;
116- }
117-
118- async function createCustomRunnerPaths (
119- options : PluginArtifactOptions ,
120- config : ESLintPluginRunnerConfig ,
121- ) : Promise < RunnerFilesPaths > {
122- const artifactPaths = Array . isArray ( options . artifactsPaths )
123- ? options . artifactsPaths
124- : [ options . artifactsPaths ] ;
125-
126- const runnerOutputPath = artifactPaths [ 0 ] ?? '' ;
127- const runnerConfigPath = path . join (
128- path . dirname ( runnerOutputPath ) ,
129- 'plugin-config.json' ,
130- ) ;
131-
132- await ensureDirectoryExists ( path . dirname ( runnerConfigPath ) ) ;
133- await writeFile ( runnerConfigPath , JSON . stringify ( config ) ) ;
134-
135- return { runnerConfigPath, runnerOutputPath } ;
136- }
137-
138- function resolveCommandArgs (
139- command ?: string | { command : string ; args ?: string [ ] } ,
140- ) : string [ ] {
141- if ( ! command ) return [ ] ;
142- return typeof command === 'string'
143- ? [ command ]
144- : [ command . command , ...( command . args ?? [ ] ) ] ;
91+ return async ( { outputDir } : PersistConfig ) : Promise < AuditOutputs > => {
92+ ui ( ) . logger . log ( `ESLint plugin executing ${ targets . length } lint targets` ) ;
93+
94+ const linterOutputs = artifacts
95+ ? await loadArtifacts ( artifacts )
96+ : await asyncSequential (
97+ targets . map ( target => ( {
98+ ...target ,
99+ outputDir,
100+ } ) ) ,
101+ lint ,
102+ ) ;
103+ const lintResults = mergeLinterOutputs ( linterOutputs ) ;
104+ const failedAudits = lintResultsToAudits ( lintResults ) ;
105+
106+ return config . slugs . map (
107+ ( slug ) : AuditOutput =>
108+ failedAudits . find ( audit => audit . slug === slug ) ?? {
109+ slug,
110+ score : 1 ,
111+ value : 0 ,
112+ displayValue : 'passed' ,
113+ details : { issues : [ ] } ,
114+ } ,
115+ ) ;
116+ } ;
145117}
0 commit comments