From 959b01f2d89751f435af7dca3c0da77a3b955456 Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Mon, 23 Jun 2025 18:44:36 +0200 Subject: [PATCH 1/2] breaking: use Node `parseArgs` for parsing CLI arguments --- package.json | 2 +- src/program.js | 38 +++++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index e136442..3219ba6 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "performance" ], "engines": { - "node": ">=18" + "node": ">=20" }, "scripts": { "pretest": "npm run build", diff --git a/src/program.js b/src/program.js index fa44f86..d64ba31 100644 --- a/src/program.js +++ b/src/program.js @@ -1,26 +1,34 @@ import { analyze } from '@projectwallace/css-analyzer' +import { parseArgs } from 'node:util' import { help } from './help.js' import { Analytics } from './components.js' export async function Program({ args, read_file, terminal_colors, stdin }) { - const format_as_json = '--json' - const help_args = ['-h', '--help'] + const options = { + json: { + type: 'boolean', + short: 'j' + }, + help: { + type: 'boolean', + short: 'h' + } + } + + const { values, positionals } = parseArgs({ + args, + options, + allowPositionals: true, + strict: false + }) - // Show help if the user explicitly asked for it - if (args.some(arg => help_args.includes(arg)) - // Show help if there's no input and no arguments provided - || args.length === 0 && stdin === '') { + // Show help if the user explicitly asked for it or if no arguments were provided + if (values.help || args.length === 0 && stdin === '') { return help(terminal_colors) } - // path is the first param that doesn't start with -- and isn't one - // of the existing flags - const path_param = args.find(arg => { - if (arg == format_as_json) return false - if (help_args.includes(arg)) return false - if (arg.startsWith('--')) return false - return true - }) + // Use the first positional argument as the file path + const path_param = positionals[0] const css = path_param ? await read_file(path_param) : stdin if (!css) { @@ -31,7 +39,7 @@ export async function Program({ args, read_file, terminal_colors, stdin }) { delete stats.__meta__ // Format as JSON if user asked for it - if (args.some(arg => arg === format_as_json)) { + if (values.json) { return JSON.stringify(stats) } From f0c11170865efff119e8bbd70195cf75e8541b08 Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Mon, 23 Jun 2025 18:51:06 +0200 Subject: [PATCH 2/2] lol, is was not breaking --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3219ba6..e136442 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "performance" ], "engines": { - "node": ">=20" + "node": ">=18" }, "scripts": { "pretest": "npm run build",