2222
2323import fsp from "node:fs/promises" ;
2424import path from "node:path" ;
25- import { migrateConfig } from "./migrate-config.js" ;
25+ import { migrateConfig , migrateJSConfig } from "./migrate-config.js" ;
2626import { Legacy } from "@eslint/eslintrc" ;
2727
2828//-----------------------------------------------------------------------------
@@ -64,7 +64,6 @@ if (!configFilePath) {
6464 process . exit ( 1 ) ;
6565}
6666
67- const config = loadConfigFile ( path . resolve ( configFilePath ) ) ;
6867const ignorePatterns = await loadIgnoreFile (
6968 path . resolve ( configFilePath , "../" , ".eslintignore" ) ,
7069) ;
@@ -74,37 +73,38 @@ const configFileBasename = path.basename(configFilePath, configFileExtname);
7473const resultFileBasename = configFileBasename . startsWith ( ".eslintrc" )
7574 ? "eslint.config"
7675 : configFileBasename ;
77- const resultFilePath = `${ path . dirname ( configFilePath ) } /${ resultFileBasename } .${ resultExtname } ` ;
7876
7977console . log ( "\nMigrating" , configFilePath ) ;
8078
81- if ( configFileExtname . endsWith ( "js" ) ) {
82- console . error (
83- "\nWARNING: This tool does not yet work great for .eslintrc.(js|cjs|mjs) files." ,
84- ) ;
85- console . error (
86- "It will convert the evaluated output of our config file, not the source code." ,
87- ) ;
88- console . error (
89- "Please review the output carefully to ensure it is correct.\n" ,
90- ) ;
91- }
92-
9379if ( ignorePatterns ) {
9480 console . log ( "Also importing your .eslintignore file" ) ;
81+ }
9582
96- if ( ! config . ignorePatterns ) {
97- config . ignorePatterns = [ ] ;
98- }
83+ const isJS = configFileExtname . endsWith ( "js" ) ;
84+ const resultFilePath = `${ path . dirname ( configFilePath ) } /${ resultFileBasename } ${ isJS ? configFileExtname : `.${ resultExtname } ` } ` ;
9985
100- // put the .eslintignore patterns last so they can override config ignores
101- config . ignorePatterns = [ ...config . ignorePatterns , ...ignorePatterns ] ;
86+ let result ;
87+
88+ if ( isJS ) {
89+ console . error (
90+ "\nIMPORTANT: Migration of JavaScript configuration files results in removal of comments." ,
91+ ) ;
92+ console . error ( "Please review the output carefully.\n" ) ;
93+
94+ const code = await fsp . readFile ( configFilePath , "utf8" ) ;
95+ result = migrateJSConfig ( code , {
96+ ignorePatterns,
97+ gitignore,
98+ } ) ;
99+ } else {
100+ const config = loadConfigFile ( path . resolve ( configFilePath ) ) ;
101+ result = migrateConfig ( config , {
102+ sourceType : commonjs ? "commonjs" : "module" ,
103+ ignorePatterns,
104+ gitignore,
105+ } ) ;
102106}
103107
104- const result = migrateConfig ( config , {
105- sourceType : commonjs ? "commonjs" : "module" ,
106- gitignore,
107- } ) ;
108108await fsp . writeFile ( resultFilePath , result . code ) ;
109109
110110console . log ( "\nWrote new config to" , resultFilePath ) ;
0 commit comments