UNPKG

@paroicms/site-generator-plugin

Version:

ParoiCMS Site Generator Plugin

68 lines (67 loc) 3.29 kB
import { getPartTypeByName, getRegularDocumentTypeByName, getRoutingDocumentTypeByName, } from "@paroicms/internal-anywhere-lib"; import { updateGeneratedSiteStepSetAsCompleted, } from "../../db/db-write.queries.js"; import { createTaskCollector } from "../lib/tasks.js"; import { createGeneratedContentReport } from "./content-report.js"; import { addParts, addRegularDocuments, updateRoutingDocument } from "./fill-lnodes.js"; import { updateSiteFields } from "./fill-site-fields.js"; import { updateLNodesWithTaxonomies } from "./fill-taxonomy-fields.js"; export async function fillSiteWithFakeContent(ctx, stepHandle, { homeRoutingCluster, localizedValues, }) { const { siteSchema } = ctx; const report = createGeneratedContentReport(); await updateSiteFields(ctx, report, { siteTitle: localizedValues.siteTitle }); const tasks = createTaskCollector(ctx); fillRoutingDocumentAndAddChildren(ctx, tasks, report, { clusterNode: homeRoutingCluster, nodeType: siteSchema.nodeTypes.home, }); const { promise } = tasks.runAll({ maxParallel: 10, rateLimitPerSecond: 3 }); const { doneCount, errorMessages } = await promise; const results = report.getResults(); await updateLNodesWithTaxonomies(ctx, { idPicker: results.idPicker, }); if (errorMessages.length > 0) { ctx.logger.warn(`Failed to generate documents:\n - ${errorMessages.join("\n - ")}`); } ctx.logger.debug(`… Executed ${doneCount} generating tasks`); await updateGeneratedSiteStepSetAsCompleted(ctx, stepHandle, { status: "completed", contentEntryCount: results.entryCount, contentInputTokenCount: results.llmReports.reduce((acc, r) => acc + r.inputTokenCount, 0), contentOutputTokenCount: results.llmReports.reduce((acc, r) => acc + (r.outputTokenCount ?? 0), 0), contentErrors: errorMessages.length > 0 ? errorMessages.join("\n - ") : null, }); } function fillRoutingDocumentAndAddChildren(ctx, tasks, report, nodeOptions) { const { clusterNode, nodeType } = nodeOptions; const { siteSchema } = ctx; tasks.add(() => updateRoutingDocument(ctx, report, nodeOptions)); for (const listType of nodeType.lists ?? []) { for (const typeName of listType.parts) { const partType = getPartTypeByName(siteSchema, typeName); tasks.add(() => addParts(ctx, report, { parentNodeId: clusterNode.nodeId, nodeType: partType, documentType: nodeType, })); } } for (const typeName of nodeType.routingChildren ?? []) { const childType = getRoutingDocumentTypeByName(siteSchema, typeName); const childIds = clusterNode.children?.[typeName]; if (!childIds) continue; fillRoutingDocumentAndAddChildren(ctx, tasks, report, { clusterNode: childIds, nodeType: childType, }); } for (const typeName of nodeType.regularChildren ?? []) { const childType = getRegularDocumentTypeByName(siteSchema, typeName); tasks.add(() => addRegularDocuments(ctx, report, { parentNodeId: clusterNode.nodeId, nodeType: childType, documentType: childType, })); } }