|
1 | 1 | import fs from 'node:fs' |
| 2 | +import path from 'node:path' |
2 | 3 |
|
3 | 4 | import execa from 'execa' |
4 | 5 | import { terminalLink } from 'termi-link' |
5 | 6 |
|
6 | 7 | import { recordTelemetryAttributes } from '@cedarjs/cli-helpers' |
7 | 8 |
|
8 | | -import { getPaths } from '../lib/index.js' |
| 9 | +import { getPaths, getConfig } from '../lib/index.js' |
| 10 | + |
| 11 | +/** |
| 12 | + * Checks for legacy ESLint configuration files in the project root |
| 13 | + * @returns {string[]} Array of legacy config file names found |
| 14 | + */ |
| 15 | +function detectLegacyEslintConfig() { |
| 16 | + const projectRoot = getPaths().base |
| 17 | + const legacyConfigFiles = [ |
| 18 | + '.eslintrc.js', |
| 19 | + '.eslintrc.cjs', |
| 20 | + '.eslintrc.json', |
| 21 | + '.eslintrc.yaml', |
| 22 | + '.eslintrc.yml', |
| 23 | + ] |
| 24 | + |
| 25 | + const foundLegacyFiles = [] |
| 26 | + |
| 27 | + // Check for .eslintrc.* files |
| 28 | + for (const configFile of legacyConfigFiles) { |
| 29 | + if (fs.existsSync(path.join(projectRoot, configFile))) { |
| 30 | + foundLegacyFiles.push(configFile) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // Check for eslint or eslintConfig fields in package.json |
| 35 | + const packageJsonPath = path.join(projectRoot, 'package.json') |
| 36 | + if (fs.existsSync(packageJsonPath)) { |
| 37 | + try { |
| 38 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')) |
| 39 | + if (packageJson.eslintConfig) { |
| 40 | + foundLegacyFiles.push('package.json (eslintConfig field)') |
| 41 | + } |
| 42 | + if (packageJson.eslint) { |
| 43 | + foundLegacyFiles.push('package.json (eslint field)') |
| 44 | + } |
| 45 | + } catch (error) { |
| 46 | + // Ignore JSON parse errors |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return foundLegacyFiles |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Shows a deprecation warning for legacy ESLint configuration |
| 55 | + * @param {string[]} legacyFiles Array of legacy config file names |
| 56 | + */ |
| 57 | +function showLegacyEslintDeprecationWarning(legacyFiles) { |
| 58 | + console.warn('') |
| 59 | + console.warn('⚠️ DEPRECATION WARNING: Legacy ESLint Configuration Detected') |
| 60 | + console.warn('') |
| 61 | + console.warn(' The following legacy ESLint configuration files were found:') |
| 62 | + legacyFiles.forEach((file) => { |
| 63 | + console.warn(` - ${file}`) |
| 64 | + }) |
| 65 | + console.warn('') |
| 66 | + console.warn( |
| 67 | + ' Cedar has migrated to ESLint flat config format. Legacy configurations', |
| 68 | + ) |
| 69 | + console.warn( |
| 70 | + ' still work but are deprecated and will be removed in a future version.', |
| 71 | + ) |
| 72 | + console.warn('') |
| 73 | + console.warn(' To migrate to the new format:') |
| 74 | + console.warn(' 1. Remove the legacy config file(s) listed above') |
| 75 | + console.warn(' 2. Create an eslint.config.mjs') |
| 76 | + console.warn(' 3. Use the flat config format with @cedarjs/eslint-config') |
| 77 | + console.warn('') |
| 78 | + console.warn(' See more here: https://github.com/cedarjs/cedar/pull/629') |
| 79 | + console.warn('') |
| 80 | +} |
9 | 81 |
|
10 | 82 | export const command = 'lint [path..]' |
11 | 83 | export const description = 'Lint your files' |
@@ -37,6 +109,13 @@ export const builder = (yargs) => { |
37 | 109 | export const handler = async ({ path, fix, format }) => { |
38 | 110 | recordTelemetryAttributes({ command: 'lint', fix, format }) |
39 | 111 |
|
| 112 | + // Check for legacy ESLint configuration and show deprecation warning |
| 113 | + const config = getConfig() |
| 114 | + const legacyConfigFiles = detectLegacyEslintConfig() |
| 115 | + if (legacyConfigFiles.length > 0 && config.eslintLegacyConfigWarning) { |
| 116 | + showLegacyEslintDeprecationWarning(legacyConfigFiles) |
| 117 | + } |
| 118 | + |
40 | 119 | try { |
41 | 120 | const pathString = path?.join(' ') |
42 | 121 | const sbPath = getPaths().web.storybook |
|
0 commit comments