@paroicms/site-generator-plugin
Version:
ParoiCMS Site Generator Plugin
81 lines (80 loc) • 3.82 kB
JavaScript
import { getPartTypeByName, getRegularDocumentTypeByName, getRoutingDocumentTypeByName, } from "@paroicms/internal-anywhere-lib";
import { createSimpleTranslator, } from "@paroicms/public-server-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, { regSite, localizedValues }) {
const { service } = ctx;
const { fqdn } = regSite;
const report = createGeneratedContentReport();
const { siteSchema, homeRoutingCluster } = await service.connector.loadSiteSchemaAndIds(fqdn);
const schemaI18n = createSimpleTranslator({
labels: siteSchema.l10n,
logger: ctx.logger,
});
await updateSiteFields(ctx, report, { fqdn, siteSchema, siteTitle: localizedValues.siteTitle });
const tasks = createTaskCollector(ctx);
fillRoutingDocumentAndAddChildren(ctx, tasks, report, {
fqdn,
siteSchema,
schemaI18n,
}, {
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, {
siteSchema,
idPicker: results.idPicker,
fqdn,
});
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, siteOptions, nodeOptions) {
const { clusterNode, nodeType } = nodeOptions;
const { siteSchema } = siteOptions;
tasks.add(() => updateRoutingDocument(ctx, report, siteOptions, nodeOptions));
for (const listType of nodeType.lists ?? []) {
for (const typeName of listType.parts) {
const partType = getPartTypeByName(siteSchema, typeName);
tasks.add(() => addParts(ctx, report, siteOptions, {
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)
throw new Error(`Missing childIds for ${typeName}`);
fillRoutingDocumentAndAddChildren(ctx, tasks, report, siteOptions, {
clusterNode: childIds,
nodeType: childType,
});
}
for (const typeName of nodeType.regularChildren ?? []) {
const childType = getRegularDocumentTypeByName(siteSchema, typeName);
tasks.add(() => addRegularDocuments(ctx, report, siteOptions, {
parentNodeId: clusterNode.nodeId,
nodeType: childType,
documentType: childType,
}));
}
}