chore: pass options argument around

This commit is contained in:
Antonin Stefanutti 2022-12-09 18:26:37 +01:00
parent 7ba704f82b
commit 8d102c9115
No known key found for this signature in database
GPG Key ID: 867EBC6DCE8E6AA2

View File

@ -271,10 +271,10 @@ process.on('unhandledRejection', error => {
// TODO: improve message when reading file locally
.then(response => console.log('Loading page finished with status: %s', response.status()))
.then(delay(options.loadPause))
.then(_ => createPlugin(page))
.then(_ => createPlugin(page, plugins, options))
.then(plugin => configurePlugin(plugin)
.then(_ => configurePage(plugin, page))
.then(_ => exportSlides(plugin, page, pdf))
.then(_ => configurePage(page, plugin, options))
.then(_ => exportSlides(page, plugin, pdf, options))
.then(async context => {
await writePdf(options.filename, pdf);
console.log(chalk.green(`\nPrinted ${chalk.bold('%s')} slides`), context.exportedSlides);
@ -286,16 +286,7 @@ process.on('unhandledRejection', error => {
browser.close();
process.exit(1);
});
async function writePdf(filename, pdf) {
const pdfDir = path.dirname(filename);
try {
fs.accessSync(pdfDir, fs.constants.F_OK);
} catch {
fs.mkdirSync(pdfDir, { recursive: true });
}
fs.writeFileSync(filename, await pdf.save({ addDefaultPage: false }));
}
})();
async function loadAvailablePlugins(pluginsPath) {
const plugins = await fs.promises.readdir(pluginsPath);
@ -308,10 +299,10 @@ process.on('unhandledRejection', error => {
return Object.fromEntries(entries.filter(Boolean));
}
async function createPlugin(page) {
async function createPlugin(page, plugins, options) {
let plugin;
if (!options.command || options.command === 'automatic') {
plugin = await createActivePlugin(page);
plugin = await createActivePlugin(page, plugins, options);
if (!plugin) {
console.log('No supported DeckTape plugin detected, falling back to generic plugin');
plugin = plugins['generic'].create(page, options);
@ -326,7 +317,7 @@ process.on('unhandledRejection', error => {
return plugin;
}
async function createActivePlugin(page) {
async function createActivePlugin(page, plugins, options) {
for (let id in plugins) {
if (id === 'generic') continue;
const plugin = plugins[id].create(page, options);
@ -334,7 +325,7 @@ process.on('unhandledRejection', error => {
}
}
async function configurePage(plugin, page) {
async function configurePage(page, plugin, options) {
if (!options.size) {
options.size = typeof plugin.size === 'function' ? await plugin.size() : { width: 1280, height: 720 };
}
@ -347,7 +338,7 @@ process.on('unhandledRejection', error => {
}
}
async function exportSlides(plugin, page, pdf) {
async function exportSlides(page, plugin, pdf, options) {
const context = {
progressBarOverflow : 0,
currentSlide : 1,
@ -364,7 +355,7 @@ process.on('unhandledRejection', error => {
process.stdout.write('\r' + await progressBar(plugin, context, { skip: true }));
} else {
await pause(options.pause);
await exportSlide(plugin, page, pdf, context);
await exportSlide(page, plugin, pdf, context, options);
}
const maxSlide = options.slides ? Math.max(...Object.keys(options.slides)) : Infinity;
let hasNext = await hasNextSlide(plugin, context);
@ -374,7 +365,7 @@ process.on('unhandledRejection', error => {
if (options.slides && !options.slides[context.currentSlide]) {
process.stdout.write('\r' + await progressBar(plugin, context, { skip: true }));
} else {
await exportSlide(plugin, page, pdf, context);
await exportSlide(page, plugin, pdf, context, options);
}
hasNext = await hasNextSlide(plugin, context);
}
@ -385,7 +376,7 @@ process.on('unhandledRejection', error => {
return context;
}
async function exportSlide(plugin, page, pdf, context) {
async function exportSlide(page, plugin, pdf, context, options) {
process.stdout.write('\r' + await progressBar(plugin, context));
const buffer = await page.pdf({
@ -531,6 +522,16 @@ process.on('unhandledRejection', error => {
return plugin.nextSlide();
}
async function writePdf(filename, pdf) {
const pdfDir = path.dirname(filename);
try {
fs.accessSync(pdfDir, fs.constants.F_OK);
} catch {
fs.mkdirSync(pdfDir, { recursive: true });
}
fs.writeFileSync(filename, await pdf.save({ addDefaultPage: false }));
}
// TODO: add progress bar, duration, ETA and file size
async function progressBar(plugin, context, { skip } = { skip: false }) {
const cols = [];
@ -547,4 +548,3 @@ process.on('unhandledRejection', error => {
context.progressBarOverflow = Math.max(index.length + 1 - 8, 0);
return cols.join('');
}
})();