UNPKG

eslint-plugin-complete

Version:

An ESLint plugin that contains useful rules.

68 lines (67 loc) 2.5 kB
import fs from "node:fs"; import path from "node:path"; import { isObject } from "./completeCommon.js"; import { configs } from "./configs.js"; import { rules } from "./rules.js"; const { name, version } = getPackageJSONNameAndVersion(); const plugin = { meta: { name, version, }, configs, rules, }; addPluginToConfigs(configs, name); // ESLint plugins must provide a default export by design. // eslint-disable-next-line export default plugin; /** * We parse the package JSON manually since importing JSON files directly in Node is experimental. */ function getPackageJSONNameAndVersion() { const packageRoot = path.join(import.meta.dirname, ".."); const packageJSONPath = path.join(packageRoot, "package.json"); let packageJSON; try { const packageJSONString = fs.readFileSync(packageJSONPath, "utf8"); packageJSON = JSON.parse(packageJSONString); } catch (error) { throw new Error(`Failed to read the "${packageJSONPath}" file: ${error}`); } if (!isObject(packageJSON)) { throw new Error(`Failed to parse the "${packageJSONPath}" file since it was not an object.`); } // eslint-disable-next-line @typescript-eslint/no-shadow const { name } = packageJSON; if (typeof name !== "string") { throw new TypeError('Failed to parse the "name" property of the "package.json" file.'); } // eslint-disable-next-line @typescript-eslint/no-shadow const { version } = packageJSON; if (typeof version !== "string") { throw new TypeError('Failed to parse the "version" property of the "package.json" file.'); } return { name, version }; } /** @see https://eslint.org/docs/latest/extend/plugins#configs-in-plugins */ function addPluginToConfigs(configsToMutate, packageName) { if (typeof packageName !== "string") { throw new TypeError('Failed to parse the plugin name from the "package.json" file.'); } const packageNameWords = packageName.split("-"); const pluginName = packageNameWords.at(-1); if (pluginName === undefined || pluginName === "") { throw new Error("Failed to parse the plugin name from the package name."); } for (const configArray of Object.values(configsToMutate)) { for (const config of configArray) { if (config.plugins !== undefined) { Object.assign(config.plugins, { [pluginName]: plugin, }); } } } }