(
files: string[],
options: TransformerOptions = {}
)
| 1043 | } |
| 1044 | |
| 1045 | export default function transformer( |
| 1046 | files: string[], |
| 1047 | options: TransformerOptions = {} |
| 1048 | ): void { |
| 1049 | // The codemod CLI passes arguments as an array for consistency with file-based transforms, |
| 1050 | // but project-level transforms like this one only process a single directory. |
| 1051 | // Usage: npx @next/codemod next-lint-to-eslint-cli <project-directory> |
| 1052 | const dir = files[0] |
| 1053 | if (!dir) { |
| 1054 | console.error('Error: Please specify a directory path') |
| 1055 | return |
| 1056 | } |
| 1057 | |
| 1058 | // Allow skipping installation via option |
| 1059 | const skipInstall = options.skipInstall === true |
| 1060 | |
| 1061 | const projectRoot = path.resolve(dir) |
| 1062 | const packageJsonPath = path.join(projectRoot, 'package.json') |
| 1063 | |
| 1064 | if (!existsSync(packageJsonPath)) { |
| 1065 | console.error('Error: package.json not found in the specified directory') |
| 1066 | return |
| 1067 | } |
| 1068 | |
| 1069 | const isTypeScript = detectTypeScript(projectRoot) |
| 1070 | |
| 1071 | console.log('Migrating from next lint to the ESLint CLI...') |
| 1072 | |
| 1073 | // Check for existing ESLint config |
| 1074 | const existingConfig = findExistingEslintConfig(projectRoot) |
| 1075 | |
| 1076 | // If no existing ESLint config found, create a new one. |
| 1077 | if (existingConfig.exists === false) { |
| 1078 | // Create new ESLint flat config |
| 1079 | const eslintConfigPath = path.join(projectRoot, 'eslint.config.mjs') |
| 1080 | const template = isTypeScript |
| 1081 | ? ESLINT_CONFIG_TEMPLATE_TYPESCRIPT |
| 1082 | : ESLINT_CONFIG_TEMPLATE_JAVASCRIPT |
| 1083 | |
| 1084 | try { |
| 1085 | writeFileSync(eslintConfigPath, template) |
| 1086 | console.log(` Created ${path.basename(eslintConfigPath)}`) |
| 1087 | } catch (error) { |
| 1088 | console.error(' Error creating ESLint config:', error) |
| 1089 | } |
| 1090 | } else { |
| 1091 | let eslintConfigFilename = path.basename(existingConfig.path) |
| 1092 | let eslintConfigPath = existingConfig.path |
| 1093 | |
| 1094 | // If legacy config found, run ESLint migration tool first. It will |
| 1095 | // use FlatCompat, so will continue to migrate using Flat config format. |
| 1096 | if (existingConfig.isLegacy && existingConfig.path) { |
| 1097 | console.log(` Found legacy ESLint config: ${eslintConfigFilename}`) |
| 1098 | |
| 1099 | // Run npx @eslint/migrate-config |
| 1100 | const command = `npx @eslint/migrate-config ${existingConfig.path}` |
| 1101 | console.log(` Running "${command}" to convert legacy config...`) |
| 1102 | try { |
nothing calls this directly
no test coverage detected