Skip to content

Commit

Permalink
refactor: path.resolve for all paths and process logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Krakazybik committed Dec 23, 2021
1 parent dc8ac3e commit e7cf6b9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 40 deletions.
11 changes: 6 additions & 5 deletions website/plugins/docusaurus-plugin-open-graph-image/config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const { resolve } = require("path");
const { readFile } = require("fs/promises");
const { object, string, number, array, is } = require("superstruct");
const { objectFromBuffer } = require("./utils");
const { objectFromBuffer, Logger } = require("./utils");

async function getConfig(path, encode = "utf-8") {
async function getConfig(configPath, encode = "utf-8") {
try {
const config = objectFromBuffer(await readFile(`${path}\\config.json`, encode));
const config = objectFromBuffer(await readFile(resolve(configPath, "config.json"), encode));

if (!validateConfig(config)) {
console.error("Config validation error");
Logger.err("Config validation error");
return;
}

return config;
} catch (error) {
console.error(error);
Logger.err(error);
}
}

Expand Down
3 changes: 2 additions & 1 deletion website/plugins/docusaurus-plugin-open-graph-image/font.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { resolve } = require("path");
const textToSVG = require("text-to-svg");

function createFontsMapFromTemplates(templates) {
Expand All @@ -6,7 +7,7 @@ function createFontsMapFromTemplates(templates) {
if (!fonts.has(template.params.font)) {
fonts.set(
template.params.font,
textToSVG.loadSync(`${template.path}\\${template.name}\\${template.params.font}`),
textToSVG.loadSync(resolve(template.path, template.name, template.params.font)),
);
}
});
Expand Down
3 changes: 2 additions & 1 deletion website/plugins/docusaurus-plugin-open-graph-image/image.js
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) {
Expand All @@ -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));
}

function createImagesMapFromTemplates(templates) {
Expand Down
58 changes: 36 additions & 22 deletions website/plugins/docusaurus-plugin-open-graph-image/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
const fs = require("fs");
const path = require("path");
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 }) {
return {
name: "docusaurus-plugin-open-graph-image",
async postBuild({ plugins, outDir, i18n }) {
Logger.info(`OG: work in progress.`);

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);
});
Expand All @@ -44,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?
Expand Down Expand Up @@ -94,8 +103,13 @@ async function generateImageFromDoc(initData, doc, locale, outputDir) {
quality: config.quality,
chromaSubsampling: "4:4:4",
})
.toFile(path.resolve(`${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`)}`);
}
}
3 changes: 2 additions & 1 deletion website/plugins/docusaurus-plugin-open-graph-image/layout.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { createSVGText } = require("./font");
const { Logger } = require("./utils");

function createLayoutLayers(doc, layout, previewFont, textWidthLimit) {
/* Check for all layers names exist in doc fields */
if (layout.some((layer) => !doc[layer.name])) {
console.error(`Wrong template config.`);
Logger.err(`Wrong template config.`);
return;
}

Expand Down
28 changes: 18 additions & 10 deletions website/plugins/docusaurus-plugin-open-graph-image/template.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { resolve } = require("path");
const { readdir, readFile } = require("fs/promises");
const { object, string, number, array, is } = require("superstruct");
const { objectFromBuffer } = require("./utils");
const { objectFromBuffer, Logger } = require("./utils");

const dirIgnore = ["config.json"];

Expand All @@ -9,22 +10,29 @@ async function getTemplates(templatesDir, encode = "utf8") {
const allDirFiles = await readdir(templatesDir);
const templatesDirNames = allDirFiles.filter((fileName) => !dirIgnore.includes(fileName));

const templates = templatesDirNames.map(async (templateName) => ({
name: templateName,
path: templatesDir,
params: objectFromBuffer(
await readFile(`${templatesDir}\\${templateName}\\template.json`, encode),
),
}));
const templates = await Promise.all(
templatesDirNames.map(async (templateName) => {
const templateBuffer = await readFile(
resolve(templatesDir, templateName, "template.json"),
encode,
);

return {
name: templateName,
path: templatesDir,
params: objectFromBuffer(templateBuffer),
};
}),
);

if (!templates.some(validateTemplate)) {
console.error("Templates validation error.");
Logger.err("Templates validation error.");
return;
}

return templates;
} catch (error) {
console.error(error);
Logger.err(error);
}
}

Expand Down

0 comments on commit e7cf6b9

Please sign in to comment.