UNPKG

@oxlint/migrate

Version:

Generates a `.oxlintrc.json` from a existing eslint flat config

60 lines (59 loc) 2.2 kB
#!/usr/bin/env node import { program } from "commander"; import { getAutodetectedEslintConfigName } from "./autoDetectConfigFile.mjs"; import { existsSync, readFileSync, renameSync, writeFileSync } from "fs"; import main from "../src/index.mjs"; import path from "path"; import packageJson from "../package.json.mjs"; import { pathToFileURL } from "node:url"; const cwd = process.cwd(); program.name("oxlint-migrate").version(packageJson.version).argument("[eslint-config]", "The path to the eslint v9 config file").option( "--output-file <file>", "The oxlint configuration file where to eslint v9 rules will be written to", ".oxlintrc.json" ).option( "--merge", "Merge eslint configuration with an existing .oxlintrc.json configuration", false ).option( "--with-nursery", "Include oxlint rules which are currently under development", false ).action(async (filePath) => { const cliOptions = program.opts(); const oxlintFilePath = path.join(cwd, cliOptions.outputFile); if (filePath === void 0) { filePath = getAutodetectedEslintConfigName(cwd); } else { filePath = path.join(cwd, filePath); } if (filePath === void 0) { program.error(`could not autodetect eslint config file`); } if (filePath.endsWith("json")) { program.error( `json format is not supported. @oxlint/migrate only supports the eslint flat configuration` ); } if (!existsSync(filePath)) { program.error(`eslint config file not found: ${filePath}`); } const eslintConfigs = await import(pathToFileURL(filePath).toString()); const options = { reporter: console.warn, merge: !!cliOptions.merge, withNursery: !!cliOptions.withNursery }; let config; if (options.merge && existsSync(oxlintFilePath)) { config = JSON.parse( readFileSync(oxlintFilePath, { encoding: "utf8", flag: "r" }) ); } const oxlintConfig = "default" in eslintConfigs ? await main(eslintConfigs.default, config, options) : await main(eslintConfigs, config, options); if (existsSync(oxlintFilePath)) { renameSync(oxlintFilePath, `${oxlintFilePath}.bak`); } writeFileSync(oxlintFilePath, JSON.stringify(oxlintConfig, null, 2)); }); program.parse();