mirror of
https://github.com/NohamR/pdfy.git
synced 2026-10-10 18:29:55 +00:00
Add commander lib to handle CLI
This commit is contained in:
10
package-lock.json
generated
10
package-lock.json
generated
@@ -8,6 +8,7 @@
|
|||||||
"name": "html2pdf-reader",
|
"name": "html2pdf-reader",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"commander": "^15.0.0",
|
||||||
"puppeteer": "^24.0.0"
|
"puppeteer": "^24.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -310,6 +311,15 @@
|
|||||||
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
|
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
|
||||||
"license": "MIT"
|
"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": {
|
"node_modules/cosmiconfig": {
|
||||||
"version": "9.0.2",
|
"version": "9.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-9.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-9.0.2.tgz",
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
"test": "node src/test/test.js"
|
"test": "node src/test/test.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"commander": "^15.0.0",
|
||||||
"puppeteer": "^24.0.0"
|
"puppeteer": "^24.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
81
src/cli.js
81
src/cli.js
@@ -1,4 +1,4 @@
|
|||||||
import process from "node:process";
|
import { Command } from "commander";
|
||||||
|
|
||||||
export const VALID_THEMES = [
|
export const VALID_THEMES = [
|
||||||
"light",
|
"light",
|
||||||
@@ -11,49 +11,42 @@ export const VALID_THEMES = [
|
|||||||
"nord-dark",
|
"nord-dark",
|
||||||
];
|
];
|
||||||
|
|
||||||
export function parseArgs() {
|
const program = new Command();
|
||||||
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 };
|
|
||||||
}
|
|
||||||
|
|
||||||
export function printUsage() {
|
program
|
||||||
console.error("Usage: node src/index.js <URL> [theme] [options]");
|
.name("html2pdf-reader")
|
||||||
console.error("");
|
.description("Convert web articles to PDF using Reader View and Puppeteer")
|
||||||
console.error("Options:");
|
.argument("<url>", "URL of the article to convert")
|
||||||
console.error(
|
.option(
|
||||||
" --theme <theme> Reader View theme (" + VALID_THEMES.join(", ") + ")",
|
"-t, --theme <theme>",
|
||||||
|
`Reader View theme (${VALID_THEMES.join(", ")})`,
|
||||||
|
)
|
||||||
|
.option("-c, --css <path>", "path to custom CSS file")
|
||||||
|
.option("-p, --prefs <path>", "path to extension preferences JSON")
|
||||||
|
.option(
|
||||||
|
"-o, --output <path>",
|
||||||
|
"output file path (default: ./output/<title>.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");
|
export function parseArgs() {
|
||||||
console.error(
|
program.parse(process.argv);
|
||||||
" --output <path> Output file path (default: ./output/<title>.pdf)",
|
const opts = program.opts();
|
||||||
);
|
return {
|
||||||
console.error("");
|
url: program.args[0],
|
||||||
console.error("Examples:");
|
opts: {
|
||||||
console.error(" node src/index.js <URL>");
|
theme: opts.theme || null,
|
||||||
console.error(" node src/index.js <URL> dark --css config/rules.css");
|
css: opts.css || null,
|
||||||
console.error("");
|
prefs: opts.prefs || null,
|
||||||
console.error("IMPORTANT: npm start consumes --flags as npm config.");
|
output: opts.output || null,
|
||||||
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("");
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import process from "node:process";
|
import process from "node:process";
|
||||||
import path from "node:path";
|
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 { loadPreferences, readCustomCss } from "./config.js";
|
||||||
import {
|
import {
|
||||||
checkExtension,
|
checkExtension,
|
||||||
@@ -27,10 +27,6 @@ const EXTENSION_BASE = "chrome-extension://";
|
|||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const { url, opts } = parseArgs();
|
const { url, opts } = parseArgs();
|
||||||
if (!url) {
|
|
||||||
printUsage();
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
checkExtension();
|
checkExtension();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user