Skip to content

Commit

Permalink
feat: enhance pdf and cliProgress func
Browse files Browse the repository at this point in the history
  • Loading branch information
condorheroblog committed Apr 25, 2023
1 parent 32c3f5a commit eac8c92
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
20 changes: 18 additions & 2 deletions src/commands/htmlExportPdf/htmlExportPdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from "node:path";
import { accessSync, constants } from "node:fs";

import { dim, green, red } from "colorette";
import type { PaperFormat } from "puppeteer";
import type { PDFMargin, PaperFormat } from "puppeteer";
import fg from "fast-glob";

import { createProgress, isValidUrl, replaceExt, writeFileSafe } from "../../utils";
Expand Down Expand Up @@ -136,7 +136,23 @@ export async function htmlExportPdf(args: undefined | string[], options: HtmlExp
await printer.preview(inputPath);
}
else {
file = await printer.pdf(inputPath, options)
const format = options.pageSize;

let margin: PDFMargin = {};
if (options.margin) {
margin = options.margin.split(",").reduce<PDFMargin>((obj, item) => {
const [key, value] = item.split("=");
if (key === "top" || key === "bottom" || key === "left" || key === "right")
obj[key] = value;

return obj;
}, {});
}
file = await printer.pdf(inputPath, {
...options,
margin,
format,
})
.catch((e: ErrorOptions) => {
console.error(e);
process.exit(1);
Expand Down
22 changes: 4 additions & 18 deletions src/core/printer.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import EventEmitter from "node:events";
import path from "node:path";

import type { Browser, PDFMargin, PDFOptions, Page, PaperFormat } from "puppeteer";
import type { Browser, PDFOptions, Page } from "puppeteer";
import { PDFDocument } from "pdf-lib";
import puppeteer from "puppeteer";

import { getOutline, setOutline } from "./outline";
import { setMetadata } from "./postprocesser";

export interface PrintPDFOptions extends Omit<PDFOptions, "format" | "margin"> {
pageSize?: PaperFormat
margin?: string | PDFMargin
}
export interface PrinterOptions {
debug?: boolean
headless?: boolean
Expand Down Expand Up @@ -177,7 +173,7 @@ export class Printer extends EventEmitter {
}
}

async pdf(input: string, options: PrintPDFOptions = {}) {
async pdf(input: string, options: PDFOptions = {}) {
let page = this.pages.get(input);
if (!page) {
page = await this.render(input)
Expand Down Expand Up @@ -218,21 +214,11 @@ export class Printer extends EventEmitter {
width: options.width,
height: options.height,
landscape: options.landscape,
format: options.pageSize ?? "letter",
format: options.format ?? "letter",
};

if (typeof options.margin === "string") {
pdfExportOptions.margin = options.margin.split(",").reduce<PDFMargin>((obj, item) => {
const [key, value] = item.split("=");
if (key === "top" || key === "bottom" || key === "left" || key === "right")
obj[key] = value;

return obj;
}, {});
}
else if (options.margin) {
if (options.margin)
pdfExportOptions.margin = options.margin;
}

if (options.pageRanges)
pdfExportOptions.pageRanges = options.pageRanges;
Expand Down
17 changes: 11 additions & 6 deletions src/utils/cliProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,40 @@ export function createProgress(indeterminate = false) {
let current = 0;
let spinner = 0;
let text = "Generating";
let title = "";
let timer: NodeJS.Timer;

const progress = new SingleBar({
clearOnComplete: true,
hideCursor: true,
format: ` {spin} {text} ${indeterminate ? dim(yellow("...")) : " {bar} {value}/{total}"}`,
format: ` {spin} {text} ${indeterminate ? dim(yellow("...")) : " {bar} {value}/{total}"} ${title.length ? "|| {title}" : ""} `,
linewrap: false,
barsize: 30,
}, Presets.shades_grey);

return {
bar: progress,
start(total: number) {
progress.start(total, 0, { spin: getSpinner(spinner), text });
progress.start(total, 0, { spin: getSpinner(spinner), text, title });
timer = setInterval(() => {
spinner += 1;
progress.update({ spin: getSpinner(spinner), text });
progress.update({ spin: getSpinner(spinner), text, title });
}, 200);
},
updateNumber(v: number) {
current = v;
progress.update(1, { spin: getSpinner(spinner), text });
progress.update(1, { spin: getSpinner(spinner), text, title });
},
increment(step: number) {
progress.increment(step, { spin: getSpinner(spinner), text });
progress.increment(step, { spin: getSpinner(spinner), text, title });
},
updateText(t: string) {
text = t;
progress.update(current, { spin: getSpinner(spinner), text });
progress.update(current, { spin: getSpinner(spinner), text, title });
},
updateTitle(t: string) {
title = t;
progress.update(current, { spin: getSpinner(spinner), text, title });
},
stop() {
clearInterval(timer);
Expand Down

0 comments on commit eac8c92

Please sign in to comment.