From 3e5545ddbd47e47027de63df9b8bed59db8a4dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=88=9A=28noham=29=C2=B2?= <100566912+NohamR@users.noreply.github.com> Date: Fri, 19 Jun 2026 15:29:47 +0200 Subject: [PATCH] Add commander lib to handle CLI --- package-lock.json | 10 ++++++ package.json | 1 + src/cli.js | 81 ++++++++++++++++++++++------------------------- src/index.js | 6 +--- 4 files changed, 49 insertions(+), 49 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3970685..8a54ed8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "html2pdf-reader", "version": "1.0.0", "dependencies": { + "commander": "^15.0.0", "puppeteer": "^24.0.0" } }, @@ -310,6 +311,15 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "license": "MIT" }, + "node_modules/commander": { + "version": "15.0.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-15.0.0.tgz", + "integrity": "sha512-z67u4ZhzCL/Tydu1lJARtEZYWbWaN7oYLHbsuzocr6y4N6WZAagG3RQ4FW61V1/0+jImpj293XfrcYnd1qxtPg==", + "license": "MIT", + "engines": { + "node": ">=22.12.0" + } + }, "node_modules/cosmiconfig": { "version": "9.0.2", "resolved": "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-9.0.2.tgz", diff --git a/package.json b/package.json index d9db8ad..9868d27 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "test": "node src/test/test.js" }, "dependencies": { + "commander": "^15.0.0", "puppeteer": "^24.0.0" } } diff --git a/src/cli.js b/src/cli.js index 95ae6dd..af61ce7 100644 --- a/src/cli.js +++ b/src/cli.js @@ -1,4 +1,4 @@ -import process from "node:process"; +import { Command } from "commander"; export const VALID_THEMES = [ "light", @@ -11,49 +11,42 @@ export const VALID_THEMES = [ "nord-dark", ]; -export function parseArgs() { - const args = process.argv.slice(2); - const url = args[0]; - const opts = { theme: null, css: null, prefs: null, output: null }; - for (let i = 1; i < args.length; i++) { - if (args[i] === "--theme" && args[i + 1]) opts.theme = args[++i]; - else if (args[i] === "--css" && args[i + 1]) opts.css = args[++i]; - else if (args[i] === "--prefs" && args[i + 1]) opts.prefs = args[++i]; - else if (args[i] === "--output" && args[i + 1]) opts.output = args[++i]; - } - if ( - !opts.theme && - args[1] && - !args[1].startsWith("-") && - VALID_THEMES.includes(args[1]) - ) { - opts.theme = args[1]; - } - return { url, opts }; -} +const program = new Command(); -export function printUsage() { - console.error("Usage: node src/index.js [theme] [options]"); - console.error(""); - console.error("Options:"); - console.error( - " --theme Reader View theme (" + VALID_THEMES.join(", ") + ")", +program + .name("html2pdf-reader") + .description("Convert web articles to PDF using Reader View and Puppeteer") + .argument("", "URL of the article to convert") + .option( + "-t, --theme ", + `Reader View theme (${VALID_THEMES.join(", ")})`, + ) + .option("-c, --css ", "path to custom CSS file") + .option("-p, --prefs ", "path to extension preferences JSON") + .option( + "-o, --output ", + "output file path (default: ./output/.pdf)", + ) + .addHelpText( + "after", + ` +Examples: + node src/index.js <URL> + node src/index.js <URL> --theme dark --css config/rules.css + npm start -- <URL> --theme dark --css config/rules.css +`, ); - console.error(" --css <path> Path to custom CSS file"); - console.error(" --prefs <path> Path to extension preferences JSON"); - console.error( - " --output <path> Output file path (default: ./output/<title>.pdf)", - ); - console.error(""); - console.error("Examples:"); - console.error(" node src/index.js <URL>"); - console.error(" node src/index.js <URL> dark --css config/rules.css"); - console.error(""); - console.error("IMPORTANT: npm start consumes --flags as npm config."); - console.error("Use node directly or add -- before flags:"); - console.error( - " node src/index.js <URL> --theme dark --css config/rules.css", - ); - console.error(" npm start -- <URL> --theme dark --css config/rules.css"); - console.error(""); + +export function parseArgs() { + program.parse(process.argv); + const opts = program.opts(); + return { + url: program.args[0], + opts: { + theme: opts.theme || null, + css: opts.css || null, + prefs: opts.prefs || null, + output: opts.output || null, + }, + }; } diff --git a/src/index.js b/src/index.js index 304416f..487d1ed 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ import process from "node:process"; import path from "node:path"; -import { parseArgs, printUsage, VALID_THEMES } from "./cli.js"; +import { parseArgs, VALID_THEMES } from "./cli.js"; import { loadPreferences, readCustomCss } from "./config.js"; import { checkExtension, @@ -27,10 +27,6 @@ const EXTENSION_BASE = "chrome-extension://"; async function main() { const { url, opts } = parseArgs(); - if (!url) { - printUsage(); - process.exit(1); - } checkExtension();