eslint-typegen
Version:
Generate types from ESLint rule schemas automatically, with auto-completion and type-checking for rule options.
46 lines (43 loc) • 1.66 kB
JavaScript
import { flatConfigsToPlugins, pluginsToRulesDTS } from "./core.mjs";
import { existsSync } from "node:fs";
import fs from "node:fs/promises";
import { hash } from "ohash";
//#region package.json
var version = "2.3.1";
//#endregion
//#region src/index.ts
/**
* Wrap with resolved flat configs to generates types for rules.
*/
async function typegen(configs, options = {}) {
const { includeCoreRules = true, dtsPath = "eslint-typegen.d.ts" } = options;
const resolved = await configs;
let configsInput = resolved;
if (includeCoreRules) {
const { builtinRules } = await import("eslint/use-at-your-own-risk");
configsInput = [{ plugins: { "": { rules: Object.fromEntries(builtinRules.entries()) } } }, ...configsInput];
}
const plugins = await flatConfigsToPlugins(configsInput, options);
const configNames = configsInput.flatMap((c) => c.name).filter(Boolean);
const hash$1 = hash([
version,
...Object.entries(plugins).map(([n, p]) => [p.meta?.name, p.meta?.version].filter(Boolean).join("@") || p.name || n).sort(),
...configNames
].join(" "));
if ((existsSync(dtsPath) ? (await fs.readFile(dtsPath, "utf-8")).match(/\/\* eslint-typegen-hash: (\S*) \*\//)?.[1]?.trim() : void 0) !== hash$1) {
const dts = [
"/* This file is generated by eslint-typegen, for augmenting rules types in ESLint */",
"/* You might want to include this file in tsconfig.json but excluded from git */",
`/* eslint-typegen-hash: ${hash$1} */`,
"",
await pluginsToRulesDTS(plugins, {
...options,
configNames
})
].join("\n");
fs.writeFile(dtsPath, dts, "utf-8");
}
return resolved;
}
//#endregion
export { typegen as default };