@@ -7,6 +7,9 @@ import type {
77 PluginSetupBinding ,
88} from '@code-pushup/models' ;
99import {
10+ answerArray ,
11+ answerBoolean ,
12+ answerString ,
1013 directoryExists ,
1114 hasDependency ,
1215 readJsonFile ,
@@ -54,6 +57,12 @@ const ESLINT_CATEGORIES: CategoryConfig[] = [
5457 } ,
5558] ;
5659
60+ type EslintOptions = {
61+ eslintrc : string ;
62+ patterns : string [ ] ;
63+ categories : boolean ;
64+ } ;
65+
5766export const eslintSetupBinding = {
5867 slug : ESLINT_PLUGIN_SLUG ,
5968 title : ESLINT_PLUGIN_TITLE ,
@@ -76,36 +85,47 @@ export const eslintSetupBinding = {
7685 } ,
7786 {
7887 key : 'eslint.categories' ,
79- message : 'Add recommended categories (bug prevention, code style) ?' ,
88+ message : 'Add ESLint categories?' ,
8089 type : 'confirm' ,
8190 default : true ,
8291 } ,
8392 ] ,
8493 generateConfig : ( answers : Record < string , PluginAnswer > ) => {
85- const withCategories = answers [ 'eslint.categories' ] !== false ;
86- const args = [
87- resolveEslintrc ( answers [ 'eslint.eslintrc' ] ) ,
88- resolvePatterns ( answers [ 'eslint.patterns' ] ) ,
89- ] . filter ( Boolean ) ;
90-
94+ const options = parseAnswers ( answers ) ;
9195 return {
9296 imports : [
9397 { moduleSpecifier : PACKAGE_NAME , defaultImport : 'eslintPlugin' } ,
9498 ] ,
95- pluginInit :
96- args . length > 0
97- ? `await eslintPlugin({ ${ args . join ( ', ' ) } })`
98- : 'await eslintPlugin()' ,
99- ...( withCategories ? { categories : ESLINT_CATEGORIES } : { } ) ,
99+ pluginInit : formatPluginInit ( options ) ,
100+ ...( options . categories ? { categories : ESLINT_CATEGORIES } : { } ) ,
100101 } ;
101102 } ,
102103} satisfies PluginSetupBinding ;
103104
104- async function detectEslintConfig (
105- targetDir : string ,
106- ) : Promise < string | undefined > {
107- const files = await readdir ( targetDir , { encoding : 'utf8' } ) ;
108- return files . find ( file => ESLINT_CONFIG_PATTERN . test ( file ) ) ;
105+ function parseAnswers ( answers : Record < string , PluginAnswer > ) : EslintOptions {
106+ return {
107+ eslintrc : answerString ( answers , 'eslint.eslintrc' ) ,
108+ patterns : answerArray ( answers , 'eslint.patterns' ) ,
109+ categories : answerBoolean ( answers , 'eslint.categories' ) ,
110+ } ;
111+ }
112+
113+ function formatPluginInit ( { eslintrc, patterns } : EslintOptions ) : string {
114+ const useCustomEslintrc =
115+ eslintrc !== '' && ! ESLINT_CONFIG_PATTERN . test ( eslintrc ) ;
116+ const customPatterns = patterns
117+ . filter ( s => s !== '' && s !== DEFAULT_PATTERN )
118+ . map ( singleQuote ) ;
119+
120+ const body = [
121+ useCustomEslintrc ? `eslintrc: ${ singleQuote ( eslintrc ) } ` : '' ,
122+ customPatterns . length === 1 ? `patterns: ${ customPatterns [ 0 ] } ` : '' ,
123+ customPatterns . length > 1 ? `patterns: [${ customPatterns . join ( ', ' ) } ]` : '' ,
124+ ]
125+ . filter ( Boolean )
126+ . join ( ', ' ) ;
127+
128+ return body ? `await eslintPlugin({ ${ body } })` : 'await eslintPlugin()' ;
109129}
110130
111131async function isRecommended ( targetDir : string ) : Promise < boolean > {
@@ -123,34 +143,9 @@ async function isRecommended(targetDir: string): Promise<boolean> {
123143 }
124144}
125145
126- /** Omits `eslintrc` for standard config filenames (ESLint discovers them automatically). */
127- function resolveEslintrc ( value : PluginAnswer | undefined ) : string {
128- if ( typeof value !== 'string' || ! value ) {
129- return '' ;
130- }
131- if ( ESLINT_CONFIG_PATTERN . test ( value ) ) {
132- return '' ;
133- }
134- return `eslintrc: ${ singleQuote ( value ) } ` ;
135- }
136-
137- /** Formats patterns as a string or array literal, omitting the plugin default. */
138- function resolvePatterns ( value : PluginAnswer | undefined ) : string {
139- if ( typeof value === 'string' ) {
140- return resolvePatterns ( value . split ( ',' ) ) ;
141- }
142- if ( ! Array . isArray ( value ) ) {
143- return '' ;
144- }
145- const patterns = value
146- . map ( s => s . trim ( ) )
147- . filter ( s => s !== '' && s !== DEFAULT_PATTERN )
148- . map ( singleQuote ) ;
149- if ( patterns . length === 0 ) {
150- return '' ;
151- }
152- if ( patterns . length === 1 ) {
153- return `patterns: ${ patterns . join ( '' ) } ` ;
154- }
155- return `patterns: [${ patterns . join ( ', ' ) } ]` ;
146+ async function detectEslintConfig (
147+ targetDir : string ,
148+ ) : Promise < string | undefined > {
149+ const files = await readdir ( targetDir , { encoding : 'utf8' } ) ;
150+ return files . find ( file => ESLINT_CONFIG_PATTERN . test ( file ) ) ;
156151}
0 commit comments