From 5a936f5d50c1919571401a3c273dc0d17ecc6133 Mon Sep 17 00:00:00 2001 From: Tommaso De Rossi Date: Thu, 10 Aug 2023 07:07:03 +0200 Subject: [PATCH] Add concurrency to OG image generation (#954) --- documentation/integrations/og/index.ts | 40 +++++++++++++++++--------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/documentation/integrations/og/index.ts b/documentation/integrations/og/index.ts index 0eff76bfc..4e4e59735 100644 --- a/documentation/integrations/og/index.ts +++ b/documentation/integrations/og/index.ts @@ -1,5 +1,6 @@ import fs from "fs/promises"; import path from "path"; +import os from "os"; import { createCanvas, GlobalFonts, @@ -60,22 +61,35 @@ export const generateOgImages = async () => { url }); } - for (const page of pages) { - const imagePathname = path.join( - process.cwd(), - "dist", - "og", - page.url + ".jpg" - ); - const image = await createImage(page.title, page.description); - await fs.mkdir(path.dirname(imagePathname), { - recursive: true - }); - console.log(`Generated image: ${page.url}`); - await fs.writeFile(imagePathname, image); + const concurrency = os.cpus().length + console.log(`Generating ${pages.length} images with ${concurrency} concurrency`); + const groups = groupByN(pages, concurrency); + for (const group of groups) { + await Promise.all(group.map(async page => { + const imagePathname = path.join( + process.cwd(), + "dist", + "og", + page.url + ".jpg" + ); + const image = await createImage(page.title, page.description); + await fs.mkdir(path.dirname(imagePathname), { + recursive: true + }); + console.log(`Generated image: ${page.url}`); + await fs.writeFile(imagePathname, image); + })); } }; +function groupByN(arr: T[], n: number): T[][] { + const result: T[][] = []; + for (let i = 0; i < arr.length; i += n) { + result.push(arr.slice(i, i + n)); + } + return result; +} + const readHtmlDirectory = async (pathname: string): Promise => { const ignoreHtmlPathnames = ["404.html", "index.html"].map((filename) => path.join(distDirPathname, filename)