diff --git a/lib/server/blog.plugin.js b/lib/server/blog.plugin.js index 15b2db7..9d723b7 100644 --- a/lib/server/blog.plugin.js +++ b/lib/server/blog.plugin.js @@ -5,6 +5,8 @@ const tslib_1 = require("tslib"); const fs = tslib_1.__importStar(require("fs")); const path = tslib_1.__importStar(require("path")); const document_1 = require("./document"); +const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger")); +const progress = tslib_1.__importStar(require("./progress")); class BlogPlugin { context; options; @@ -83,11 +85,17 @@ class BlogPlugin { } }; generate = async () => { + logger_1.default.info(`Generating og images for ${this.pages.length} blog pages`); + const bar = progress.defaultBar(); + bar.start(this.pages.length, 0, { prefix: 'rendering images', suffix: '-' }); for (const page of this.pages) { const document = new document_1.Document(this.getHtmlPath(page.permalink)); + bar.update({ suffix: page.permalink }); await document.load(); - if (!document.loaded) + if (!document.loaded) { + bar.increment(); continue; + } const image = await this.imageRenderer({ ...page, document, @@ -95,12 +103,16 @@ class BlogPlugin { }, this.context); if (!image) { await document.write(); + bar.increment(); continue; } const generated = await this.imageGenerator.generate(...image); await document.setImage(generated.url); await document.write(); + bar.increment(); } + bar.stop(); + logger_1.default.success('Generated og images for blog pages'); }; getHtmlPath = (permalink, baseUrl) => path.join(this.context.outDir, permalink, 'index.html'); } diff --git a/lib/server/docs.plugin.js b/lib/server/docs.plugin.js index 917321a..bf10999 100644 --- a/lib/server/docs.plugin.js +++ b/lib/server/docs.plugin.js @@ -4,6 +4,8 @@ exports.DocsPlugin = void 0; const tslib_1 = require("tslib"); const path = tslib_1.__importStar(require("path")); const document_1 = require("./document"); +const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger")); +const progress = tslib_1.__importStar(require("./progress")); class DocsPlugin { context; options; @@ -44,8 +46,12 @@ class DocsPlugin { }))); }; generate = async () => { + logger_1.default.info(`Generating og images for ${this.docs.length} docs pages`); + const bar = progress.defaultBar(); + bar.start(this.docs.length, 0, { prefix: 'rendering images', suffix: '-' }); for (const doc of this.docs) { const document = new document_1.Document(this.getHtmlPath(doc)); + bar.update({ suffix: doc.metadata.permalink }); await document.load(); const image = await this.imageRenderer({ ...doc, @@ -54,12 +60,16 @@ class DocsPlugin { }, this.context); if (!image) { await document.write(); + bar.increment(); continue; } const generated = await this.imageGenerator.generate(...image); await document.setImage(generated.url); await document.write(); + bar.increment(); } + bar.stop(); + logger_1.default.success('Generated og images for docs pages'); }; getHtmlPath = (doc) => // slug is used here instead of permalink because permalink already contains version and locale paths of websiteOutDir diff --git a/lib/server/pages.plugin.js b/lib/server/pages.plugin.js index ff7ebff..ac429a8 100644 --- a/lib/server/pages.plugin.js +++ b/lib/server/pages.plugin.js @@ -4,6 +4,8 @@ exports.PagesPlugin = void 0; const tslib_1 = require("tslib"); const path = tslib_1.__importStar(require("path")); const document_1 = require("./document"); +const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger")); +const progress = tslib_1.__importStar(require("./progress")); class PagesPlugin { context; options; @@ -50,11 +52,17 @@ class PagesPlugin { } }; generate = async () => { + logger_1.default.info(`Generating og images for ${this.pages.length} pages`); + const bar = progress.defaultBar(); + bar.start(this.pages.length, 0, { prefix: 'rendering images', suffix: '-' }); for (const page of this.pages) { const document = new document_1.Document(this.getHtmlPath(page.metadata.permalink)); + bar.update({ suffix: page.metadata.permalink }); await document.load(); - if (!document.loaded) + if (!document.loaded) { + bar.increment(); continue; + } const image = await this.imageRenderer({ ...page, document, @@ -62,12 +70,16 @@ class PagesPlugin { }, this.context); if (!image) { await document.write(); + bar.increment(); continue; } const generated = await this.imageGenerator.generate(...image); await document.setImage(generated.url); await document.write(); + bar.increment(); } + bar.stop(); + logger_1.default.success('Generated og images for pages'); }; getHtmlPath = (permalink) => path.join(this.context.outDir, permalink, 'index.html'); } diff --git a/lib/server/progress.d.ts b/lib/server/progress.d.ts new file mode 100644 index 0000000..67bcc3b --- /dev/null +++ b/lib/server/progress.d.ts @@ -0,0 +1,2 @@ +import cliProgress from 'cli-progress'; +export declare function defaultBar(): cliProgress.SingleBar; diff --git a/lib/server/progress.js b/lib/server/progress.js new file mode 100644 index 0000000..df5ac12 --- /dev/null +++ b/lib/server/progress.js @@ -0,0 +1,17 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.defaultBar = void 0; +const tslib_1 = require("tslib"); +const cli_progress_1 = tslib_1.__importDefault(require("cli-progress")); +const chalk_1 = tslib_1.__importDefault(require("chalk")); +const figures_1 = tslib_1.__importDefault(require("figures")); +function defaultBar() { + return new cli_progress_1.default.SingleBar({ + format: chalk_1.default.green(figures_1.default.bullet) + chalk_1.default.green(' og images ') + '{bar}' + chalk_1.default.white(' {prefix} ({percentage}%) ') + chalk_1.default.grey('{value}/{total}\n') + chalk_1.default.grey('{suffix}'), + barCompleteChar: chalk_1.default.green('█'), + barIncompleteChar: chalk_1.default.white('█'), + hideCursor: true + }); +} +exports.defaultBar = defaultBar; +; diff --git a/package.json b/package.json index 51abcf0..d61aab5 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,9 @@ "@docusaurus/utils-common": "^2.4.1", "@docusaurus/utils-validation": "^2.4.1", "@resvg/resvg-js": "^2.6.0", + "chalk": "^4.1.2", + "cli-progress": "^3.12.0", + "figures": "^3.2.0", "lodash": "^4.17.21", "node-html-parser": "^6.1.5", "object-hash": "^3.0.0", @@ -31,9 +34,11 @@ "node": ">=16.14" }, "devDependencies": { + "@docusaurus/logger": "^3.1.1", "@docusaurus/plugin-content-blog": "^3.1.1", "@docusaurus/plugin-content-docs": "^3.1.1", "@docusaurus/plugin-content-pages": "^3.1.1", + "@types/cli-progress": "^3.11.5", "@types/lodash": "^4.14.186", "typescript": "^5.4.3" } diff --git a/src/server/blog.plugin.ts b/src/server/blog.plugin.ts index a8e3aff..9e59c5e 100644 --- a/src/server/blog.plugin.ts +++ b/src/server/blog.plugin.ts @@ -10,6 +10,8 @@ import { ImageGenerator } from './imageGenerator' import { BlogPageData } from './types/blog.types' import { ImageRenderer } from './types/image.types' import { PluginOptions } from './types/plugin.types' +import logger from '@docusaurus/logger' +import * as progress from './progress'; export class BlogPlugin { static plugin = 'docusaurus-plugin-content-blog' @@ -105,11 +107,18 @@ export class BlogPlugin { } generate = async () => { + logger.info(`Generating og images for ${this.pages.length} blog pages`) + const bar = progress.defaultBar() + bar.start(this.pages.length, 0, {prefix: 'rendering images', suffix: '-'}) for (const page of this.pages) { const document = new Document(this.getHtmlPath(page.permalink)) + bar.update({ suffix: page.permalink }) await document.load() - if (!document.loaded) continue + if (!document.loaded) { + bar.increment() + continue + } const image = await this.imageRenderer( { @@ -122,6 +131,7 @@ export class BlogPlugin { if (!image) { await document.write() + bar.increment() continue } @@ -130,7 +140,11 @@ export class BlogPlugin { await document.setImage(generated.url) await document.write() + bar.increment() } + bar.stop() + + logger.success('Generated og images for blog pages') } getHtmlPath = (permalink: string, baseUrl?: string) => diff --git a/src/server/docs.plugin.ts b/src/server/docs.plugin.ts index 9ce6f49..ffa90b0 100644 --- a/src/server/docs.plugin.ts +++ b/src/server/docs.plugin.ts @@ -10,6 +10,8 @@ import { ImageGenerator } from './imageGenerator' import { DocsPageData } from './types/docs.types' import { ImageRenderer } from './types/image.types' import { PluginOptions } from './types/plugin.types' +import logger from '@docusaurus/logger' +import * as progress from './progress'; export class DocsPlugin { static plugin = 'docusaurus-plugin-content-docs' @@ -62,8 +64,13 @@ export class DocsPlugin { } generate = async () => { + logger.info(`Generating og images for ${this.docs.length} docs pages`) + const bar = progress.defaultBar() + bar.start(this.docs.length, 0, {prefix: 'rendering images', suffix: '-'}) for (const doc of this.docs) { const document = new Document(this.getHtmlPath(doc)!) + bar.update({ suffix: doc.metadata.permalink }) + await document.load() const image = await this.imageRenderer( @@ -77,6 +84,7 @@ export class DocsPlugin { if (!image) { await document.write() + bar.increment() continue } @@ -84,7 +92,10 @@ export class DocsPlugin { await document.setImage(generated.url) await document.write() + bar.increment() } + bar.stop() + logger.success('Generated og images for docs pages') } getHtmlPath = (doc: Partial) => diff --git a/src/server/pages.plugin.ts b/src/server/pages.plugin.ts index 1e5a012..57a35ec 100644 --- a/src/server/pages.plugin.ts +++ b/src/server/pages.plugin.ts @@ -9,6 +9,8 @@ import { Document } from './document' import { ImageGenerator } from './imageGenerator' import { ImageRenderer } from './types/image.types' import { PluginOptions } from './types/plugin.types' +import logger from '@docusaurus/logger' +import * as progress from './progress'; export class PagesPlugin { static plugin = 'docusaurus-plugin-content-pages' @@ -67,10 +69,18 @@ export class PagesPlugin { } generate = async () => { + logger.info(`Generating og images for ${this.pages.length} pages`) + const bar = progress.defaultBar() + bar.start(this.pages.length, 0, {prefix: 'rendering images', suffix: '-'}) for (const page of this.pages) { const document = new Document(this.getHtmlPath(page.metadata.permalink)) + bar.update({ suffix: page.metadata.permalink }) + await document.load() - if (!document.loaded) continue + if (!document.loaded) { + bar.increment() + continue + } const image = await this.imageRenderer( { @@ -83,6 +93,7 @@ export class PagesPlugin { if (!image) { await document.write() + bar.increment() continue } @@ -91,7 +102,10 @@ export class PagesPlugin { await document.setImage(generated.url) await document.write() + bar.increment() } + bar.stop() + logger.success('Generated og images for pages') } getHtmlPath = (permalink: string) => diff --git a/src/server/progress.ts b/src/server/progress.ts new file mode 100644 index 0000000..679bc2d --- /dev/null +++ b/src/server/progress.ts @@ -0,0 +1,12 @@ +import cliProgress from 'cli-progress'; +import chalk from 'chalk'; +import figures from 'figures'; + +export function defaultBar(): cliProgress.SingleBar { + return new cliProgress.SingleBar({ + format: chalk.green(figures.bullet) + chalk.green(' og images ') + '{bar}' + chalk.white(' {prefix} ({percentage}%) ') + chalk.grey('{value}/{total}\n') + chalk.grey('{suffix}'), + barCompleteChar: chalk.green('█'), + barIncompleteChar: chalk.white('█'), + hideCursor: true + }); +}; \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 4be121c..37f8002 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1282,7 +1282,7 @@ chalk "^4.1.2" tslib "^2.4.0" -"@docusaurus/logger@3.1.1": +"@docusaurus/logger@3.1.1", "@docusaurus/logger@^3.1.1": version "3.1.1" resolved "https://registry.yarnpkg.com/@docusaurus/logger/-/logger-3.1.1.tgz#423e8270c00a57b1b3a0cc8a3ee0a4c522a68387" integrity sha512-BjkNDpQzewcTnST8trx4idSoAla6zZ3w22NqM/UMcFtvYJgmoE4layuTzlfql3VFPNuivvj7BOExa/+21y4X2Q== @@ -2019,6 +2019,13 @@ dependencies: "@types/node" "*" +"@types/cli-progress@^3.11.5": + version "3.11.5" + resolved "https://registry.yarnpkg.com/@types/cli-progress/-/cli-progress-3.11.5.tgz#9518c745e78557efda057e3f96a5990c717268c3" + integrity sha512-D4PbNRbviKyppS5ivBGyFO29POlySLmA2HyUFE4p5QGazAMM3CwkKWcvTl8gvElSuxRh6FPKL8XmidX873ou4g== + dependencies: + "@types/node" "*" + "@types/connect-history-api-fallback@^1.3.5": version "1.5.4" resolved "https://registry.yarnpkg.com/@types/connect-history-api-fallback/-/connect-history-api-fallback-1.5.4.tgz#7de71645a103056b48ac3ce07b3520b819c1d5b3" @@ -3104,6 +3111,13 @@ cli-boxes@^3.0.0: resolved "https://registry.yarnpkg.com/cli-boxes/-/cli-boxes-3.0.0.tgz#71a10c716feeba005e4504f36329ef0b17cf3145" integrity sha512-/lzGpEWL/8PfI0BmBOPRwp0c/wFNX1RdUML3jK/RcSBA9T8mZDdQpqYBKtCFTOfQbwPqWEOpjqW+Fnayc0969g== +cli-progress@^3.12.0: + version "3.12.0" + resolved "https://registry.yarnpkg.com/cli-progress/-/cli-progress-3.12.0.tgz#807ee14b66bcc086258e444ad0f19e7d42577942" + integrity sha512-tRkV3HJ1ASwm19THiiLIXLO7Im7wlTuKnvkYaTkyoAPefqjNg7W7DHKUlGRxy9vxDvbyCYQkQozvptuMkGCg8A== + dependencies: + string-width "^4.2.3" + cli-table3@^0.6.2, cli-table3@^0.6.3: version "0.6.4" resolved "https://registry.yarnpkg.com/cli-table3/-/cli-table3-0.6.4.tgz#d1c536b8a3f2e7bec58f67ac9e5769b1b30088b0" @@ -4229,6 +4243,13 @@ fflate@^0.7.3: resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.7.4.tgz#61587e5d958fdabb5a9368a302c25363f4f69f50" integrity sha512-5u2V/CDW15QM1XbbgS+0DfPxVB+jUKhWEKuuFuHncbk3tEEqzmoXL+2KyOFuKGqOnmdIy0/davWF1CkuwtibCw== +figures@^3.2.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/figures/-/figures-3.2.0.tgz#625c18bd293c604dc4a8ddb2febf0c88341746af" + integrity sha512-yaduQFRKLXYOGgEn6AZau90j3ggSOyiqXU0F9JZfeXYhNa+Jk4X+s45A2zg5jns87GAFa34BBm2kXw4XpNcbdg== + dependencies: + escape-string-regexp "^1.0.5" + file-loader@^6.2.0: version "6.2.0" resolved "https://registry.yarnpkg.com/file-loader/-/file-loader-6.2.0.tgz#baef7cf8e1840df325e4390b4484879480eebe4d" @@ -8341,7 +8362,7 @@ std-env@^3.0.1: resolved "https://registry.yarnpkg.com/std-env/-/std-env-3.7.0.tgz#c9f7386ced6ecf13360b6c6c55b8aaa4ef7481d2" integrity sha512-JPbdCEQLj1w5GilpiHAx3qJvFndqybBysA3qUOnznweH4QbNYUsW/ea8QzSrnh0vNsezMMw5bcVool8lM0gwzg== -string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2: +string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==