resumefy
Version:
A simple toolkit to bring your JSON Resume to life
79 lines (78 loc) • 2.25 kB
JavaScript
import { TimeoutError } from 'puppeteer';
import { writeFile } from 'fs/promises';
import path from 'path';
import { log } from '../cli/log.js';
import { menu } from './menu.js';
/**
* Represents a page in the browser
*/
export class ResumePage {
page;
browser;
constructor(page, browser) {
this.page = page;
this.browser = browser;
}
/**
* Get the content of the page
* @returns Promise resolving with the content of the page
*/
async content() {
return this.page.content();
}
/**
* Set the content of the page
* @param content Content to set
* @returns Promise resolving when content is set
*/
async setContent(content) {
await this.page.setContent(content, { waitUntil: 'networkidle0' });
}
/**
* Write HTML file to the given directory
* @param dir Directory to write file to
* @param name Name of the file
* @returns Promise resolving when file is written
*/
async html(dir, name) {
return writeFile(path.join(dir, `${name}.html`), await this.content());
}
/**
* Write PDF file to the given directory
* @param dir Directory to write file to
* @param name Name of the file
* @returns Promise resolving when file is written
*/
async pdf(dir, name) {
return this.page.pdf({ path: path.join(dir, `${name}.pdf`), format: 'a4', printBackground: true }).catch((err) => {
if (err instanceof TimeoutError) {
log.error(err.message);
}
else {
throw err;
}
});
}
/**
* Go to a URL
* @param url URL to go to
* @returns Promise resolving when page is loaded
*/
async goto(url) {
return this.page.goto(url, { waitUntil: 'networkidle0' });
}
/**
* Add a menu to the page
* @param page page to add menu to
* @param openPreview callback to open preview
*/
async addMenu(openPreview) {
try {
await this.page.exposeFunction('openPreview', openPreview);
}
catch {
// Function already exposed
}
await this.page.evaluate(menu(openPreview));
}
}