UNPKG

@paroicms/site-generator-plugin

Version:

ParoiCMS Site Generator Plugin

96 lines (95 loc) 4.19 kB
import { messageOf } from "@paroi/data-formatters-lib"; import { pathExists } from "@paroicms/internal-server-lib"; import { ApiError, escapeHtml, makeStylesheetLinkAsyncTag, } from "@paroicms/public-server-lib"; import { join } from "node:path"; import { executeCommand } from "./commands/execute-command.js"; import { SLUG, packageDir, pluginVersion } from "./context.js"; import { formatGeneratorCommand, formatGeneratorPluginConfiguration } from "./data-format.js"; import { createOrOpenSiteGeneratorConnection } from "./db/db-init.js"; import { initializeImageNames } from "./generator/lib/images-lib.js"; import { createRawContext } from "./lib/create-raw-context.js"; import { startSiteRemover } from "./lib/site-remover.js"; await initializeImageNames(); const plugin = { version: pluginVersion, slug: SLUG, async siteInit(service) { const { cn, logNextQuery } = await createOrOpenSiteGeneratorConnection({ sqliteFile: join(service.registeredSite.dataDir, "site-generator.sqlite"), canCreate: true, logger: service.logger, }); const pluginConf = formatGeneratorPluginConfiguration(service.configuration); let debugDir = pluginConf.debugDir; if (debugDir) { if (!(await pathExists(debugDir))) { service.logger.warn(`[generator] Debug directory (debugDir) doesn't exist: ${debugDir}`); debugDir = undefined; } } let rawContext; service.registerHook("initialized", (service) => { rawContext = createRawContext(service, { cn, logNextQuery, pluginConf, debugDir, }); startSiteRemover(rawContext); }); service.setPublicAssetsDirectory(join(packageDir, "gen-front", "dist")); service.registerLiquidTag("siteGeneratorApp", "injectHtml", (service) => { service.setRenderState("paSiteGeneratorApp", true); return `<div id="site-generator-app"></div>`; }); service.registerHeadTags(({ state }) => { if (!state.get("paSiteGeneratorApp")) return; return [ makeStylesheetLinkAsyncTag(`${service.pluginAssetsUrl}/gen-front.css`), `<script type="module" src="${escapeHtml(`${service.pluginAssetsUrl}/gen-front.mjs`)}" class="js-script-${SLUG}" data-google-recaptcha-site-key="${escapeHtml(service.configuration.googleRecaptchaSiteKey)}" async></script>`, ]; }); service.setPublicApiHandler(async (service, httpContext, relativePath) => { if (!rawContext) throw new Error("should be initialized"); const ctx = rawContext; const { req, res } = httpContext; if (relativePath !== "") { res.status(404).send({ status: 404 }); return; } let command; try { command = formatGeneratorCommand(req.body); } catch (error) { res.status(400).send({ status: 400, message: messageOf(error) }); return; } try { const result = await executeCommand(ctx, command); const buf = Buffer.from(JSON.stringify(result), "utf-8"); res.status(200); res.append("Content-Type", "application/json"); res.append("Content-Length", buf.byteLength.toString()); res.send(buf); } catch (error) { service.logger.error("Error executing command:", error); if (error instanceof ApiError) { const response = { success: false, userMessage: error.message, }; res.status(error.status).send(response); } else { const response = { success: false }; res.status(500).send(response); } } }); }, }; export default plugin;