11import { readdir } from 'node:fs/promises' ;
2+ import { createRequire } from 'node:module' ;
23import path from 'node:path' ;
3- import type { CategoryConfig , PluginSetupBinding } from '@code-pushup/models' ;
4- import { directoryExists , readJsonFile , singleQuote } from '@code-pushup/utils' ;
4+ import type {
5+ CategoryConfig ,
6+ PluginAnswer ,
7+ PluginSetupBinding ,
8+ } from '@code-pushup/models' ;
9+ import {
10+ directoryExists ,
11+ hasDependency ,
12+ readJsonFile ,
13+ singleQuote ,
14+ } from '@code-pushup/utils' ;
515import {
616 DEFAULT_PATTERN ,
717 ESLINT_PLUGIN_SLUG ,
818 ESLINT_PLUGIN_TITLE ,
919} from './constants.js' ;
1020
11- const PACKAGE_NAME = '@code-pushup/eslint-plugin' ;
21+ const { name : PACKAGE_NAME } = createRequire ( import . meta. url ) (
22+ '../../package.json' ,
23+ ) as typeof import ( '../../package.json' ) ;
24+
1225const ESLINT_CONFIG_PATTERN = / ^ ( \. e s l i n t r c ( \. \w + ) ? | e s l i n t \. c o n f i g \. \w + ) $ / ;
1326
1427const ESLINT_CATEGORIES : CategoryConfig [ ] = [
@@ -64,16 +77,12 @@ export const eslintSetupBinding = {
6477 {
6578 key : 'eslint.categories' ,
6679 message : 'Add recommended categories (bug prevention, code style)?' ,
67- type : 'select' ,
68- choices : [
69- { name : 'Yes' , value : 'yes' } ,
70- { name : 'No' , value : 'no' } ,
71- ] ,
72- default : 'yes' ,
80+ type : 'confirm' ,
81+ default : true ,
7382 } ,
7483 ] ,
75- generateConfig : ( answers : Record < string , string | string [ ] > ) => {
76- const withCategories = answers [ 'eslint.categories' ] !== 'no' ;
84+ generateConfig : ( answers : Record < string , PluginAnswer > ) => {
85+ const withCategories = answers [ 'eslint.categories' ] !== false ;
7786 const args = [
7887 resolveEslintrc ( answers [ 'eslint.eslintrc' ] ) ,
7988 resolvePatterns ( answers [ 'eslint.patterns' ] ) ,
@@ -108,17 +117,14 @@ async function isRecommended(targetDir: string): Promise<boolean> {
108117 dependencies ?: Record < string , string > ;
109118 devDependencies ?: Record < string , string > ;
110119 } > ( path . join ( targetDir , 'package.json' ) ) ;
111- return (
112- 'eslint' in ( packageJson . dependencies ?? { } ) ||
113- 'eslint' in ( packageJson . devDependencies ?? { } )
114- ) ;
120+ return hasDependency ( packageJson , 'eslint' ) ;
115121 } catch {
116122 return false ;
117123 }
118124}
119125
120126/** Omits `eslintrc` for standard config filenames (ESLint discovers them automatically). */
121- function resolveEslintrc ( value : string | string [ ] | undefined ) : string {
127+ function resolveEslintrc ( value : PluginAnswer | undefined ) : string {
122128 if ( typeof value !== 'string' || ! value ) {
123129 return '' ;
124130 }
@@ -129,9 +135,14 @@ function resolveEslintrc(value: string | string[] | undefined): string {
129135}
130136
131137/** Formats patterns as a string or array literal, omitting the plugin default. */
132- function resolvePatterns ( value : string | string [ ] | undefined ) : string {
133- const items = typeof value === 'string' ? value . split ( ',' ) : ( value ?? [ ] ) ;
134- const patterns = items
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
135146 . map ( s => s . trim ( ) )
136147 . filter ( s => s !== '' && s !== DEFAULT_PATTERN )
137148 . map ( singleQuote ) ;
0 commit comments