UNPKG

@oxlint/migrate

Version:

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

47 lines (46 loc) 1.32 kB
import { existsSync } from "node:fs"; import path from "node:path"; import { pathToFileURL } from "node:url"; const FLAT_CONFIG_FILENAMES = [ "eslint.config.js", "eslint.config.mjs", "eslint.config.cjs", "eslint.config.ts", "eslint.config.mts", "eslint.config.cts" ]; const getAutodetectedEslintConfigName = (cwd) => { for (const filename of FLAT_CONFIG_FILENAMES) { const filePath = path.join(cwd, filename); if (existsSync(filePath)) { return filePath; } } }; const loadESLintConfig = async (filePath) => { if (filePath.endsWith("json")) { throw new Error( `json format is not supported. @oxlint/migrate only supports the eslint flat configuration` ); } let url = pathToFileURL(filePath).toString(); if (!existsSync(filePath)) { throw new Error(`eslint config file not found: ${filePath}`); } if ("Bun" in globalThis || "Deno" in globalThis) { return import(url); } if (filePath.endsWith(".ts") || filePath.endsWith(".mts") || filePath.endsWith(".cts")) { const { createJiti } = await import("jiti"); const jitiInstance = createJiti(filePath, { interopDefault: false, moduleCache: false }); return jitiInstance.import(url); } return import(url); }; export { getAutodetectedEslintConfigName, loadESLintConfig };