core/loaders/section.loader.ts

core/loaders/section.loader.ts is a file in Bane's Lab Content. 38 lines of code and 8 definitions.

import { COORDINATION_PAGE, README_FILE } from "#configuration/constants/writing.constants";
import { markdownSectionsOf, moduleSectionOf, sectionsOf } from "#core/converters/section.converter";
import { packageFiles, packagePath } from "#core/loaders/coordination.loader";
import type { PageSection } from "#types/writing.types";
import { STRINGS_SUFFIX } from "#configuration/constants/tone.constants";
import { UTF8 } from "#configuration/constants/source.constants";
import { join } from "node:path";
import { literalsOf } from "#core/loaders/strings.loader";
import { readFileSync } from "node:fs";
import { readPageContent } from "#core/loaders/payload.loader";

const PAGE_IDS_MODULE = "@banes-lab/web/core/ids/page.ids.ts";

export const sitePages = async function sitePages(): Promise<readonly string[]> {
    const ids: unknown = await import(PAGE_IDS_MODULE);
    return typeof ids === "object" && ids !== null
        ? Object.values(ids).filter((id: unknown): id is string => typeof id === "string")
        : [];
};

export const readSections = function readSections(pages: readonly string[]): {
    readonly missing: readonly string[];
    readonly sections: readonly PageSection[];
} {
    const found = pages.map((page) => ({ content: readPageContent(page), page }));
    return {
        missing: found.filter((entry) => entry.content === null).map((entry) => entry.page),
        sections: found.flatMap((entry) => (entry.content === null ? [] : sectionsOf(entry.page, entry.content))),
    };
};

export const packageSections = async function packageSections(root: string): Promise<readonly PageSection[]> {
    const modules = await Promise.all(
        packageFiles(root)
            .filter((file) => file.endsWith(STRINGS_SUFFIX))
            .map(async (file) =>
                moduleSectionOf(COORDINATION_PAGE, packagePath(root, file), await literalsOf(readFileSync(file, UTF8))),
            ),
    );
    const readme = readFileSync(join(root, README_FILE), UTF8);
    return [...markdownSectionsOf(COORDINATION_PAGE, README_FILE, readme), ...modules];
};