diff --git a/bin/create-config.js b/bin/create-config.js index 7808a24..b0006d6 100755 --- a/bin/create-config.js +++ b/bin/create-config.js @@ -10,10 +10,25 @@ import { findPackageJson } from "../lib/utils/npm-utils.js"; import * as log from "../lib/utils/logging.js"; import process from "node:process"; import fs from "node:fs/promises"; +// eslint-disable-next-line n/no-unsupported-features/node-builtins -- Using built-in parseArgs +import { parseArgs } from "node:util"; const pkg = JSON.parse(await fs.readFile(new URL("../package.json", import.meta.url), "utf8")); -log.log(`${pkg.name}: v${pkg.version}\n`); +const { values: argv } = parseArgs({ + options: { + config: { + type: "string" + }, + eslintrc: { + type: "boolean" + } + }, + args: process.argv, + strict: false +}); + +log.log(`${pkg.name}: v${pkg.version}`); process.on("uncaughtException", error => { if (error instanceof Error && error.code === "ERR_USE_AFTER_CLOSE") { @@ -32,10 +47,7 @@ if (packageJsonPath === null) { throw new Error("A package.json file is necessary to initialize ESLint. Run `npm init` to create a package.json file and try again."); } -const argv = process.argv; -const sharedConfigIndex = process.argv.indexOf("--config"); - -if (sharedConfigIndex === -1) { +if (!argv.config) { const generator = new ConfigGenerator({ cwd, packageJsonPath }); await generator.prompt(); @@ -44,8 +56,8 @@ if (sharedConfigIndex === -1) { } else { // passed "--config" - const packageName = argv[sharedConfigIndex + 1]; - const type = argv.includes("--eslintrc") ? "eslintrc" : "flat"; + const packageName = argv.config; + const type = argv.eslintrc ? "eslintrc" : "flat"; const answers = { config: { packageName, type } }; const generator = new ConfigGenerator({ cwd, packageJsonPath, answers });