|
| 1 | +import { readdir } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import type { CategoryConfig, PluginSetupBinding } from '@code-pushup/models'; |
| 4 | +import { directoryExists, readJsonFile, singleQuote } from '@code-pushup/utils'; |
| 5 | +import { |
| 6 | + DEFAULT_PATTERN, |
| 7 | + ESLINT_PLUGIN_SLUG, |
| 8 | + ESLINT_PLUGIN_TITLE, |
| 9 | +} from './constants.js'; |
| 10 | + |
| 11 | +const PACKAGE_NAME = '@code-pushup/eslint-plugin'; |
| 12 | +const ESLINT_CONFIG_PATTERN = /^(\.eslintrc(\.\w+)?|eslint\.config\.\w+)$/; |
| 13 | + |
| 14 | +const ESLINT_CATEGORIES: CategoryConfig[] = [ |
| 15 | + { |
| 16 | + slug: 'bug-prevention', |
| 17 | + title: 'Bug prevention', |
| 18 | + description: 'Lint rules that find **potential bugs** in your code.', |
| 19 | + refs: [ |
| 20 | + { |
| 21 | + type: 'group', |
| 22 | + plugin: ESLINT_PLUGIN_SLUG, |
| 23 | + slug: 'problems', |
| 24 | + weight: 1, |
| 25 | + }, |
| 26 | + ], |
| 27 | + }, |
| 28 | + { |
| 29 | + slug: 'code-style', |
| 30 | + title: 'Code style', |
| 31 | + description: |
| 32 | + 'Lint rules that promote **good practices** and consistency in your code.', |
| 33 | + refs: [ |
| 34 | + { |
| 35 | + type: 'group', |
| 36 | + plugin: ESLINT_PLUGIN_SLUG, |
| 37 | + slug: 'suggestions', |
| 38 | + weight: 1, |
| 39 | + }, |
| 40 | + ], |
| 41 | + }, |
| 42 | +]; |
| 43 | + |
| 44 | +export const eslintSetupBinding = { |
| 45 | + slug: ESLINT_PLUGIN_SLUG, |
| 46 | + title: ESLINT_PLUGIN_TITLE, |
| 47 | + packageName: PACKAGE_NAME, |
| 48 | + isRecommended, |
| 49 | + prompts: async (targetDir: string) => [ |
| 50 | + { |
| 51 | + key: 'eslint.eslintrc', |
| 52 | + message: 'Path to ESLint config', |
| 53 | + type: 'input', |
| 54 | + default: (await detectEslintConfig(targetDir)) ?? '', |
| 55 | + }, |
| 56 | + { |
| 57 | + key: 'eslint.patterns', |
| 58 | + message: 'File patterns to lint', |
| 59 | + type: 'input', |
| 60 | + default: (await directoryExists(path.join(targetDir, 'src'))) |
| 61 | + ? 'src' |
| 62 | + : DEFAULT_PATTERN, |
| 63 | + }, |
| 64 | + { |
| 65 | + key: 'eslint.categories', |
| 66 | + 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', |
| 73 | + }, |
| 74 | + ], |
| 75 | + generateConfig: (answers: Record<string, string | string[]>) => { |
| 76 | + const withCategories = answers['eslint.categories'] !== 'no'; |
| 77 | + const args = [ |
| 78 | + resolveEslintrc(answers['eslint.eslintrc']), |
| 79 | + resolvePatterns(answers['eslint.patterns']), |
| 80 | + ].filter(Boolean); |
| 81 | + |
| 82 | + return { |
| 83 | + imports: [ |
| 84 | + { moduleSpecifier: PACKAGE_NAME, defaultImport: 'eslintPlugin' }, |
| 85 | + ], |
| 86 | + pluginInit: |
| 87 | + args.length > 0 |
| 88 | + ? `await eslintPlugin({ ${args.join(', ')} })` |
| 89 | + : 'await eslintPlugin()', |
| 90 | + ...(withCategories ? { categories: ESLINT_CATEGORIES } : {}), |
| 91 | + }; |
| 92 | + }, |
| 93 | +} satisfies PluginSetupBinding; |
| 94 | + |
| 95 | +async function detectEslintConfig( |
| 96 | + targetDir: string, |
| 97 | +): Promise<string | undefined> { |
| 98 | + const files = await readdir(targetDir, { encoding: 'utf8' }); |
| 99 | + return files.find(file => ESLINT_CONFIG_PATTERN.test(file)); |
| 100 | +} |
| 101 | + |
| 102 | +async function isRecommended(targetDir: string): Promise<boolean> { |
| 103 | + if (await detectEslintConfig(targetDir)) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + try { |
| 107 | + const packageJson = await readJsonFile<{ |
| 108 | + dependencies?: Record<string, string>; |
| 109 | + devDependencies?: Record<string, string>; |
| 110 | + }>(path.join(targetDir, 'package.json')); |
| 111 | + return ( |
| 112 | + 'eslint' in (packageJson.dependencies ?? {}) || |
| 113 | + 'eslint' in (packageJson.devDependencies ?? {}) |
| 114 | + ); |
| 115 | + } catch { |
| 116 | + return false; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** Omits `eslintrc` for standard config filenames (ESLint discovers them automatically). */ |
| 121 | +function resolveEslintrc(value: string | string[] | undefined): string { |
| 122 | + if (typeof value !== 'string' || !value) { |
| 123 | + return ''; |
| 124 | + } |
| 125 | + if (ESLINT_CONFIG_PATTERN.test(value)) { |
| 126 | + return ''; |
| 127 | + } |
| 128 | + return `eslintrc: ${singleQuote(value)}`; |
| 129 | +} |
| 130 | + |
| 131 | +/** 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 |
| 135 | + .map(s => s.trim()) |
| 136 | + .filter(s => s !== '' && s !== DEFAULT_PATTERN) |
| 137 | + .map(singleQuote); |
| 138 | + if (patterns.length === 0) { |
| 139 | + return ''; |
| 140 | + } |
| 141 | + if (patterns.length === 1) { |
| 142 | + return `patterns: ${patterns.join('')}`; |
| 143 | + } |
| 144 | + return `patterns: [${patterns.join(', ')}]`; |
| 145 | +} |
0 commit comments