Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cjtantay committed Nov 6, 2024
1 parent 187ac0e commit d71b895
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { createInjectBlock } from './blocks/inject.js';
import { createScriptBlock } from './blocks/script.js';
import { createStaticBlock } from './blocks/static.js';
import path from 'node:path';
import glob from 'fast-glob';

// filters
import { dateFilter } from './filters/date.js';
Expand All @@ -40,6 +39,7 @@ import { AssetsEngine } from './engines/assets.js';
import { NunjucksEngine } from './engines/nunjucks.js';
import { RollupEngine } from './engines/rollup.js';
import { SassEngine } from './engines/sass.js';
import { readdir } from 'node:fs';

const CROSSWALK_ALLOWED_ASSET_TYPES = [
'img',
Expand Down Expand Up @@ -369,6 +369,19 @@ export class Baker extends EventEmitter {
}, preppedData);
}

embedFiles(path) {
return new Promise((resolve, reject) => {
readdir(path, (err, files) => {
//handling error
if (err) {
reject('Unable to scan directory: ' + err);
}

resolve(files);
});
});
}

/**
* Generates fallback images for web-component embeds.
* @param {string} baseUrl The local server's base URL
Expand All @@ -377,12 +390,12 @@ export class Baker extends EventEmitter {

async buildEmbedFallbacks(baseUrl) {
const distDir = this.output.split('/').slice(0, -1).join('/');
const embedFilePattern = path.join(distDir, 'embeds', '**/*.html');
const embedFilePattern = path.join(distDir, '_dist', 'embeds');
/**
* An array of file paths representing embed files.
* @type {string[]}
*/
const embedFiles = glob.sync(embedFilePattern);
const embedFiles = await this.embedFiles(embedFilePattern);

if (!embedFiles.length) return;

Expand All @@ -391,9 +404,8 @@ export class Baker extends EventEmitter {
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});

for (const embedFilePath of embedFiles) {
for (const embedName of embedFiles) {
try {
const embedName = path.basename(embedFilePath, '.html');
const embedPath = `embeds/${embedName}/index.html`;
const screenshotLocalUrl = `${baseUrl}/${embedPath}`;
console.log(`Taking screenshot of: ${screenshotLocalUrl}`);
Expand Down Expand Up @@ -434,7 +446,7 @@ export class Baker extends EventEmitter {
await page.screenshot({ path: screenshotStoragePath, fullPage: true });
await page.close();
} catch (err) {
console.error(`Failed to process ${embedFilePath}: ${err.message}`);
console.error(`Failed to process ${embedName}: ${err.message}`);
}
}

Expand Down

0 comments on commit d71b895

Please sign in to comment.