@oxlint/migrate
Version:
Generates a `.oxlintrc.json` from a existing eslint flat config
77 lines (76 loc) • 2.83 kB
JavaScript
import { program } from "commander";
import { existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
import path from "node:path";
import { getAutodetectedEslintConfigName, loadESLintConfig } from "./config-loader.mjs";
import main from "../src/index.mjs";
import packageJson from "../package.json.mjs";
import { walkAndReplaceProjectFiles } from "../src/walker/index.mjs";
import { getAllProjectFiles } from "./project-loader.mjs";
import { writeFile } from "node:fs/promises";
const cwd = process.cwd();
const getFileContent = (absoluteFilePath) => {
try {
return readFileSync(absoluteFilePath, "utf-8");
} catch {
return void 0;
}
};
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
).option(
"--replace-eslint-comments",
"Search in the project files for eslint comments and replaces them with oxlint. Some eslint comments are not supported and will be reported."
).option(
"--type-aware",
"Includes supported type-aware rules. Needs the same flag in `oxlint` to enable it."
).action(async (filePath) => {
const cliOptions = program.opts();
const oxlintFilePath = path.join(cwd, cliOptions.outputFile);
const options = {
reporter: console.warn,
merge: !!cliOptions.merge,
withNursery: !!cliOptions.withNursery,
typeAware: !!cliOptions.typeAware
};
if (cliOptions.replaceEslintComments) {
await walkAndReplaceProjectFiles(
await getAllProjectFiles(),
(filePath2) => getFileContent(filePath2),
(filePath2, content) => writeFile(filePath2, content, "utf-8"),
options
);
return;
}
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`);
}
const eslintConfigs = await loadESLintConfig(filePath);
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();