import type { ChapterShape, SurfaceResult } from "#types/chapter.types"; import { NO_README_INPUTS, NO_TWINS, RENDER_UNSTABLE } from "#configuration/strings/derivation.strings"; import { attributionOf, pagesFor, rendererFor, shapeInputOf, surfaceFolder } from "#core/loaders/chapter.loader"; import { REPOSITORY_SHAPE } from "#configuration/constants/chapter.constants"; import { persistChapters } from "#core/persistence/chapter.persistence"; import { unstableChapters } from "#core/validators/chapter.validator"; const LIST_JOIN = ", "; export const renderSurface = async function renderSurface( shape: ChapterShape, out: string | null = null, ): Promise { const input = shapeInputOf(shape); if (input === null) { return { ok: false, reason: NO_TWINS + pagesFor(shape).join(LIST_JOIN) }; } if (shape === REPOSITORY_SHAPE && input.readme === null) { return { ok: false, reason: NO_README_INPUTS }; } const render = rendererFor(shape); const attribution = attributionOf(); const unstable = unstableChapters(render, input, attribution); if (unstable.length > 0) { return { ok: false, reason: RENDER_UNSTABLE + unstable.join(LIST_JOIN) }; } const target = out ?? surfaceFolder(shape); const chapters = render(input, attribution); const removed = await persistChapters(target, chapters); return { chapters, ok: true, out: target, removed }; };