All files / apps/developer-portal/tools/lib workflows.ts

41.17% Statements 7/17
0% Branches 0/4
44.44% Functions 4/9
43.75% Lines 7/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                    2x                                 2x 1x 2x 2x 1x           2x                                        
import { existsSync } from "node:fs";
import { resolve } from "node:path";
import { readWorkflowFiles } from "#layers/workflows/server/utils/workflowFiles.ts";
import {
    assembleWorkflows,
    type Template,
    type WorkflowProblem,
} from "#layers/workflows/shared/workflows.ts";
 
/** Paths a capability's `reuse.from` points at that don't exist, relative to the workspace root. */
const missingReusePaths = (templates: Template[], workspaceRoot: string): WorkflowProblem[] =>
    templates.flatMap((template) =>
        [...template.capabilities, ...template.types].flatMap((capability) => {
            const from = capability.reuse?.from;
 
            if (!from || existsSync(resolve(workspaceRoot, from))) return [];
 
            return [
                {
                    file: `templates/${template.id}.yaml`,
                    message: `capability \`${capability.id}\` reuses \`${from}\`, which doesn't exist`,
                },
            ];
        }),
    );
 
/** Every project file with no `<id>.diary.md` beside it. */
export const missingDiaries = (projectFiles: string[], dir: string): WorkflowProblem[] =>
    projectFiles
        .map((file) => file.replace(/\.yaml$/, ""))
        .filter((id) => !existsSync(resolve(dir, "projects", `${id}.diary.md`)))
        .map((id) => ({
            file: `projects/${id}.yaml`,
            message: `${id} has no \`${id}.diary.md\` beside it`,
        }));
 
/** Every rule `docs/workflows/README.md` states, plus the ones only a filesystem can check. */
export const workflowProblems = async (
    dir: string,
    workspaceRoot: string,
): Promise<WorkflowProblem[]> => {
    const files = await readWorkflowFiles(dir);
    const { templates, problems } = assembleWorkflows(
        files.templates,
        files.projects,
        files.snapshots,
    );
 
    return [
        ...problems,
        ...missingReusePaths(templates, workspaceRoot),
        ...missingDiaries(
            files.projects.map(({ file }) => file),
            dir,
        ),
    ];
};