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
112
decktape.js
112
decktape.js
@ -50,23 +50,23 @@ parser.script('decktape').options({
|
||||
default : 0,
|
||||
help : 'Duration in milliseconds between the page has loaded and starting to export slides',
|
||||
},
|
||||
urlLoadTimeout: {
|
||||
full: 'url-load-timeout',
|
||||
metavar: '<ms>',
|
||||
default: 60000,
|
||||
help: 'Timeout in milliseconds to use when waiting for the initial URL to load',
|
||||
urlLoadTimeout : {
|
||||
full : 'url-load-timeout',
|
||||
metavar : '<ms>',
|
||||
default : 60000,
|
||||
help : 'Timeout in milliseconds to use when waiting for the initial URL to load',
|
||||
},
|
||||
pageLoadTimeout: {
|
||||
full: 'page-load-timeout',
|
||||
metavar: '<ms>',
|
||||
default: 20000,
|
||||
help: 'Timeout in milliseconds to use when waiting for the slide deck page to load',
|
||||
pageLoadTimeout : {
|
||||
full : 'page-load-timeout',
|
||||
metavar : '<ms>',
|
||||
default : 20000,
|
||||
help : 'Timeout in milliseconds to use when waiting for the slide deck page to load',
|
||||
},
|
||||
bufferTimeout: {
|
||||
full: 'buffer-timeout',
|
||||
metavar: '<ms>',
|
||||
default: 30000,
|
||||
help: 'Timeout in milliseconds to use when waiting for a slide to finish buffering (set to 0 to disable)',
|
||||
bufferTimeout : {
|
||||
full : 'buffer-timeout',
|
||||
metavar : '<ms>',
|
||||
default : 30000,
|
||||
help : 'Timeout in milliseconds to use when waiting for a slide to finish buffering (set to 0 to disable)',
|
||||
},
|
||||
screenshots : {
|
||||
default : false,
|
||||
@ -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,32 +286,23 @@ 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) {
|
||||
async function loadAvailablePlugins(pluginsPath) {
|
||||
const plugins = await fs.promises.readdir(pluginsPath);
|
||||
const entries = await Promise.all(plugins.map(async pluginPath => {
|
||||
const [, plugin] = pluginPath.match(/^(.*)\.js$/);
|
||||
if (plugin && (await fs.promises.stat(path.join(pluginsPath, pluginPath))).isFile()) {
|
||||
return [plugin, await import (`./plugins/${pluginPath}`)];
|
||||
return [plugin, await import(`./plugins/${pluginPath}`)];
|
||||
}
|
||||
}));
|
||||
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);
|
||||
@ -324,30 +315,30 @@ process.on('unhandledRejection', error => {
|
||||
}
|
||||
console.log(chalk.cyan(`${chalk.bold('%s')} plugin activated`), plugin.getName());
|
||||
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);
|
||||
if (await plugin.isActive()) return plugin;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
await page.setViewport(options.size);
|
||||
}
|
||||
}
|
||||
|
||||
async function configurePlugin(plugin) {
|
||||
async function configurePlugin(plugin) {
|
||||
if (typeof plugin.configure === 'function') {
|
||||
await plugin.configure();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@ -383,9 +374,9 @@ process.on('unhandledRejection', error => {
|
||||
pdf.context.assign(ref, pdf.context.flateStream(font.write({ type: 'ttf', hinting: true })));
|
||||
});
|
||||
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({
|
||||
@ -415,9 +406,9 @@ process.on('unhandledRejection', error => {
|
||||
await pause(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function printSlide(pdf, slide, context) {
|
||||
async function printSlide(pdf, slide, context) {
|
||||
const duplicatedEntries = [];
|
||||
const [page] = await pdf.copyPages(slide, [0]);
|
||||
pdf.addPage(page);
|
||||
@ -516,23 +507,33 @@ process.on('unhandledRejection', error => {
|
||||
.filter(([key, _]) => keys.includes(key))
|
||||
.reduce((r, [k, v], i) => r + (i > 0 ? ',' : '') + k + '=' + v, '');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function hasNextSlide(plugin, context) {
|
||||
async function hasNextSlide(plugin, context) {
|
||||
if (typeof plugin.hasNextSlide === 'function') {
|
||||
return await plugin.hasNextSlide();
|
||||
} else {
|
||||
return context.currentSlide < context.totalSlides;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function nextSlide(plugin, context) {
|
||||
async function nextSlide(plugin, context) {
|
||||
context.currentSlide++;
|
||||
return plugin.nextSlide();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: add progress bar, duration, ETA and file size
|
||||
async function progressBar(plugin, context, { skip } = { skip : false }) {
|
||||
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 = [];
|
||||
const index = await plugin.currentSlideIndex();
|
||||
cols.push(`${skip ? 'Skipping' : 'Printing'} slide `);
|
||||
@ -546,5 +547,4 @@ process.on('unhandledRejection', error => {
|
||||
cols.push(' '.repeat(Math.max(context.progressBarOverflow - Math.max(index.length + 1 - 8, 0), 0)));
|
||||
context.progressBarOverflow = Math.max(index.length + 1 - 8, 0);
|
||||
return cols.join('');
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user