mirror of
https://github.com/astefanutti/decktape.git
synced 2024-09-11 22:27:11 +03:00
chore: pass options argument around
This commit is contained in:
parent
7ba704f82b
commit
8d102c9115
44
decktape.js
44
decktape.js
@ -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('');
|
||||
}
|
||||
})();
|
||||
|
Loading…
Reference in New Issue
Block a user