import { AVOIDANCE_REFERENCE, SURFACE_POLICY, UTF8 } from "#configuration/constants/source.constants"; import type { Inventory, SourceText } from "#types/inventory.types"; import { absolutePath, relativePath } from "@ssot/paths"; import { docsConfig, loadGovlabConfig } from "@govlab/quality/config"; import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; import { readJsonOrNull } from "#core/persistence/report.persistence"; const readSource = function readSource(path: string): SourceText { return { path, text: readFileSync(join(process.cwd(), path), UTF8) }; }; const boundaryDocsPresent = async function boundaryDocsPresent(): Promise { const config = await loadGovlabConfig(process.cwd()); return docsConfig(config).boundaryDocs.filter((name) => existsSync(join(process.cwd(), name))); }; export const inventorySourcePaths = async function inventorySourcePaths(): Promise { return [ ...(await boundaryDocsPresent()), relativePath("collaboration.behaviour", SURFACE_POLICY), relativePath("docArch.references", AVOIDANCE_REFERENCE), ]; }; export const readInventorySources = async function readInventorySources(): Promise { return (await inventorySourcePaths()).map(readSource); }; export const inventoryTarget = function inventoryTarget(): string { return absolutePath("govlabHost.reports.content.inventory"); }; const isInventory = function isInventory(value: unknown): value is Inventory { return typeof value === "object" && value !== null && Array.isArray(Reflect.get(value, "records")); }; export const readInventory = function readInventory(): Inventory | null { const parsed = readJsonOrNull(inventoryTarget()); return isInventory(parsed) ? parsed : null; };