-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PROMO-251(OG Plugin): Minor refinements (fs, logs) #387
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
const { resolve } = require("path"); | ||
const sharp = require("sharp"); | ||
|
||
function getTemplateImageId(template) { | ||
|
@@ -11,7 +12,7 @@ function createImagePipeline(file) { | |
} | ||
|
||
function createImageFromTemplate({ path, name, params }) { | ||
return createImagePipeline(`${path}\\${name}\\${params.image}`); | ||
return createImagePipeline(resolve(path, name, params.image)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
function createImagesMapFromTemplates(templates) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,50 @@ | ||
const fs = require("fs"); | ||
const { mkdir } = require("fs/promises"); | ||
const { resolve } = require("path"); | ||
const sha1 = require("sha1"); | ||
const { getTemplates } = require("./template"); | ||
const { createLayoutLayers } = require("./layout"); | ||
const { createFontsMapFromTemplates } = require("./font"); | ||
const { createImagesMapFromTemplates, getTemplateImageId } = require("./image"); | ||
const { getConfig } = require("./config"); | ||
const { getTemplateNameByRules } = require("./rules"); | ||
const { Logger } = require("./utils"); | ||
|
||
module.exports = function ({ templatesDir }) { | ||
const initData = bootstrap(templatesDir); | ||
if (!initData) { | ||
console.error("OpenGraph plugin exit with error."); | ||
return; | ||
} | ||
|
||
const { config } = initData; | ||
|
||
module.exports = function (_, { templatesDir }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. вернул на место удалённый по ошибке первый аргумент. |
||
return { | ||
name: "docusaurus-plugin-open-graph-image", | ||
async postBuild({ plugins, outDir, i18n }) { | ||
Logger.info(`OG: work in progress.`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Пенерёс инициализацию в postBuild изза асинхронности. |
||
|
||
const initData = await bootstrap(templatesDir); | ||
if (!initData) { | ||
Logger.err("OpenGraph plugin exit with error."); | ||
return; | ||
} | ||
|
||
Logger.ok(`OG: initialization complete.`); | ||
|
||
const { config } = initData; | ||
|
||
const docsPlugin = plugins.find( | ||
(plugin) => plugin.name === "docusaurus-plugin-content-docs", | ||
); | ||
|
||
if (!docsPlugin) throw new Error("Docusaurus Doc plugin not found."); | ||
|
||
const previewOutputDir = `${outDir}\\${config.outputDir}`; | ||
fs.mkdir(previewOutputDir, { recursive: true }, (error) => { | ||
if (error) throw error; | ||
}); | ||
const previewOutputDir = resolve(outDir, config.outputDir); | ||
|
||
try { | ||
await mkdir(previewOutputDir, { recursive: true }); | ||
} catch (error) { | ||
Logger.err(error); | ||
return; | ||
} | ||
Logger.ok(`OG: assets output folder created.`); | ||
|
||
const docsContent = docsPlugin.content; | ||
const docsVersions = docsContent.loadedVersions; | ||
docsVersions.forEach((version) => { | ||
const { docs } = version; | ||
|
||
docs.forEach((document) => { | ||
generateImageFromDoc(initData, document, i18n.currentLocale, previewOutputDir); | ||
}); | ||
|
@@ -43,19 +53,19 @@ module.exports = function ({ templatesDir }) { | |
}; | ||
}; | ||
|
||
function bootstrap(templatesDir) { | ||
async function bootstrap(templatesDir) { | ||
const isProd = process.env.NODE_ENV === "production"; | ||
if (!isProd) return; | ||
|
||
if (!templatesDir) { | ||
console.error("Wrong templatesDir option."); | ||
Logger.err("Wrong templatesDir option."); | ||
return; | ||
} | ||
|
||
const templates = getTemplates(templatesDir); | ||
const templates = await getTemplates(templatesDir); | ||
if (!templates) return; | ||
|
||
const config = getConfig(templatesDir); | ||
const config = await getConfig(templatesDir); | ||
if (!config) return; | ||
|
||
// TODO: File not found exception? | ||
|
@@ -93,8 +103,13 @@ async function generateImageFromDoc(initData, doc, locale, outputDir) { | |
quality: config.quality, | ||
chromaSubsampling: "4:4:4", | ||
}) | ||
.toFile(`${outputDir}\\${hashFileName}.jpg`); | ||
.toFile(resolve(outputDir, `${hashFileName}.jpg`)); | ||
Logger.ok(`Generated: ${hashFileName}.jpg`); | ||
} catch (error) { | ||
console.error(error, id, title, hashFileName); | ||
Logger.err(`${error} | ||
DocumentID: ${id} | ||
Title: ${title} | ||
Hash: ${hashFileName} | ||
Path: ${resolve(outputDir, `${hashFileName}.jpg`)}`); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,21 @@ | ||
const pc = require("picocolors"); | ||
|
||
function objectFromBuffer(buffer) { | ||
return JSON.parse(buffer.toString()); | ||
} | ||
|
||
module.exports = { objectFromBuffer }; | ||
const Logger = { | ||
info(text) { | ||
// eslint-disable-next-line no-console | ||
console.log(pc.bgYellow(pc.black(`! ${text}`))); | ||
}, | ||
ok(text) { | ||
// eslint-disable-next-line no-console | ||
console.log(pc.bgGreen(pc.black(`\u2714 ${text}`))); | ||
}, | ||
err(text) { | ||
console.error(pc.bgRed(pc.black(`\u274C ${text}`))); | ||
}, | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Немного красивости, надеюсь никто не будет против =) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Лишь бы бандл нам не расфигачило, но там разберемся уже) |
||
module.exports = { objectFromBuffer, Logger }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise:
Красивое...