From 9745b5248e6027d889d14d9e7ab1e614e188aedd Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Thu, 23 Oct 2025 04:20:41 +0700 Subject: [PATCH 1/5] add parseArgs --- bin/create-config.js | 59 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/bin/create-config.js b/bin/create-config.js index 7808a24..120b60d 100755 --- a/bin/create-config.js +++ b/bin/create-config.js @@ -10,10 +10,58 @@ 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 VERSION_TEXT = `${pkg.name}: v${pkg.version}`; +const HELP_TEXT = ` +Usage: ${pkg.name} [options] + +Options: + --config [String] Specify shareable config that is hosted on npm + --eslintrc Use an eslintrc-style (legacy) shared config + -h, --help Show help + -v, --version Show version +`.trim(); + +const { values: argv } = parseArgs({ + options: { + config: { + type: "string" + }, + eslintrc: { + type: "boolean" + }, + help: { + type: "boolean", + short: "h" + }, + version: { + type: "boolean", + short: "v" + } + }, + args: process.argv, + strict: false +}); + +/* eslint-disable n/no-process-exit, no-console -- show help & version menu */ + +if (argv.help) { + console.log(HELP_TEXT); + process.exit(0); +} + +if (argv.version) { + console.log(VERSION_TEXT); + process.exit(0); +} + +/* eslint-enable -- enable again */ + +log.log(VERSION_TEXT); process.on("uncaughtException", error => { if (error instanceof Error && error.code === "ERR_USE_AFTER_CLOSE") { @@ -32,10 +80,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 +89,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 }); From 47aecb05f12caa98d77afb36c28482962fb8592f Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Thu, 23 Oct 2025 04:24:40 +0700 Subject: [PATCH 2/5] change version text --- bin/create-config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/create-config.js b/bin/create-config.js index 120b60d..082b138 100755 --- a/bin/create-config.js +++ b/bin/create-config.js @@ -15,7 +15,7 @@ import { parseArgs } from "node:util"; const pkg = JSON.parse(await fs.readFile(new URL("../package.json", import.meta.url), "utf8")); -const VERSION_TEXT = `${pkg.name}: v${pkg.version}`; +const VERSION_TEXT = `v${pkg.version}`; const HELP_TEXT = ` Usage: ${pkg.name} [options] @@ -61,7 +61,7 @@ if (argv.version) { /* eslint-enable -- enable again */ -log.log(VERSION_TEXT); +log.log(`${pkg.name}: ${VERSION_TEXT}`); process.on("uncaughtException", error => { if (error instanceof Error && error.code === "ERR_USE_AFTER_CLOSE") { From de457684c8eef7b36c4220c383060170f30a73ab Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Sun, 26 Oct 2025 20:53:58 +0700 Subject: [PATCH 3/5] remove unrelated changes --- bin/create-config.js | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/bin/create-config.js b/bin/create-config.js index 082b138..b0006d6 100755 --- a/bin/create-config.js +++ b/bin/create-config.js @@ -15,17 +15,6 @@ import { parseArgs } from "node:util"; const pkg = JSON.parse(await fs.readFile(new URL("../package.json", import.meta.url), "utf8")); -const VERSION_TEXT = `v${pkg.version}`; -const HELP_TEXT = ` -Usage: ${pkg.name} [options] - -Options: - --config [String] Specify shareable config that is hosted on npm - --eslintrc Use an eslintrc-style (legacy) shared config - -h, --help Show help - -v, --version Show version -`.trim(); - const { values: argv } = parseArgs({ options: { config: { @@ -33,35 +22,13 @@ const { values: argv } = parseArgs({ }, eslintrc: { type: "boolean" - }, - help: { - type: "boolean", - short: "h" - }, - version: { - type: "boolean", - short: "v" } }, args: process.argv, strict: false }); -/* eslint-disable n/no-process-exit, no-console -- show help & version menu */ - -if (argv.help) { - console.log(HELP_TEXT); - process.exit(0); -} - -if (argv.version) { - console.log(VERSION_TEXT); - process.exit(0); -} - -/* eslint-enable -- enable again */ - -log.log(`${pkg.name}: ${VERSION_TEXT}`); +log.log(`${pkg.name}: v${pkg.version}`); process.on("uncaughtException", error => { if (error instanceof Error && error.code === "ERR_USE_AFTER_CLOSE") { From 6f05a97f227fb22e6e3431fc2e8c1f46565bea0e Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Fri, 7 Nov 2025 19:11:00 +0700 Subject: [PATCH 4/5] use right name --- bin/create-config.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/create-config.js b/bin/create-config.js index b0006d6..14b8f15 100755 --- a/bin/create-config.js +++ b/bin/create-config.js @@ -15,7 +15,7 @@ import { parseArgs } from "node:util"; const pkg = JSON.parse(await fs.readFile(new URL("../package.json", import.meta.url), "utf8")); -const { values: argv } = parseArgs({ +const { values: options } = parseArgs({ options: { config: { type: "string" @@ -47,7 +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."); } -if (!argv.config) { +if (!options.config) { const generator = new ConfigGenerator({ cwd, packageJsonPath }); await generator.prompt(); @@ -56,8 +56,8 @@ if (!argv.config) { } else { // passed "--config" - const packageName = argv.config; - const type = argv.eslintrc ? "eslintrc" : "flat"; + const packageName = options.config; + const type = options.eslintrc ? "eslintrc" : "flat"; const answers = { config: { packageName, type } }; const generator = new ConfigGenerator({ cwd, packageJsonPath, answers }); From 829380d8e42b8b9fde79307bcd8b4b62d4a9476e Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Fri, 7 Nov 2025 19:36:28 +0700 Subject: [PATCH 5/5] strict parse --- bin/create-config.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/bin/create-config.js b/bin/create-config.js index 14b8f15..8170ec1 100755 --- a/bin/create-config.js +++ b/bin/create-config.js @@ -15,20 +15,29 @@ import { parseArgs } from "node:util"; const pkg = JSON.parse(await fs.readFile(new URL("../package.json", import.meta.url), "utf8")); -const { values: options } = parseArgs({ - options: { - config: { - type: "string" +log.log(`${pkg.name}: v${pkg.version}`); + +let options; + +try { + const { values } = parseArgs({ + options: { + config: { + type: "string" + }, + eslintrc: { + type: "boolean" + } }, - eslintrc: { - type: "boolean" - } - }, - args: process.argv, - strict: false -}); + args: process.argv.slice(2) + }); -log.log(`${pkg.name}: v${pkg.version}`); + options = values; +} catch (error) { + log.error(error.message); + // eslint-disable-next-line n/no-process-exit -- exit gracefully on invalid arguments + process.exit(1); +} process.on("uncaughtException", error => { if (error instanceof Error && error.code === "ERR_USE_AFTER_CLOSE") {