All files / apps/developer-portal/layers/workflows/shared directory.ts

95.83% Statements 46/48
81.81% Branches 18/22
100% Functions 10/10
95.45% Lines 42/44

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                                        42x 300x     42x       85x   19x   19x 1x 1x     18x   18x 18x     42x 101x   101x         101x   101x 101x 1x   101x     42x           89x   89x 1x 1x     88x 89x   88x   89x   89x 2x     88x   88x 3x 3x     85x 85x       42x         109x 109x 109x 89x     109x    
import { projectProblems, readProject } from "./project.ts";
import { isRecord } from "./reader.ts";
import { projectView } from "./projectView.ts";
import { readSnapshot } from "./snapshot.ts";
import { readTemplate } from "./template.ts";
import type { ProjectView } from "./types/ProjectView.ts";
import type { Snapshot } from "./types/Snapshot.ts";
import type { Template } from "./types/Template.ts";
import type { WorkflowProblem } from "./types/WorkflowProblem.ts";
 
export type ParsedFile = { file: string } & (
    { ok: true; value: unknown } | { ok: false; error: string }
);
 
export interface Workflows {
    templates: Template[];
    projects: ProjectView[];
    problems: WorkflowProblem[];
}
 
const report = (problems: WorkflowProblem[], file: string, messages: string[]): void => {
    problems.push(...messages.map((message) => ({ file, message })));
};
 
const snapshotOf = (
    problems: WorkflowProblem[],
    parsed: ParsedFile | undefined,
): Snapshot | null => {
    if (!parsed) return null;
 
    const where = `projects/${parsed.file}`;
 
    if (!parsed.ok) {
        report(problems, where, [parsed.error]);
        return null;
    }
 
    const { snapshot, problems: found } = readSnapshot(parsed.value);
 
    report(problems, where, found);
    return snapshot;
};
 
const templateOf = (problems: WorkflowProblem[], parsed: ParsedFile): Template | null => {
    const where = `templates/${parsed.file}`;
 
    Iif (!parsed.ok) {
        report(problems, where, [parsed.error]);
        return null;
    }
 
    const { template, problems: found } = readTemplate(parsed.value);
 
    report(problems, where, found);
    if (`${template.id}.yaml` !== parsed.file) {
        report(problems, where, [`\`id: ${template.id}\` doesn't match the filename`]);
    }
    return template.id ? template : null;
};
 
const projectOf = (
    problems: WorkflowProblem[],
    templates: Template[],
    parsed: ParsedFile,
    snapshotFiles: Record<string, ParsedFile>,
): ProjectView | null => {
    const where = `projects/${parsed.file}`;
 
    if (!parsed.ok) {
        report(problems, where, [parsed.error]);
        return null;
    }
 
    const named = isRecord(parsed.value) ? parsed.value.template : null;
    const { project, problems: found } = readProject(
        parsed.value,
        templates.find((candidate) => candidate.id === named)?.types,
    );
    report(problems, where, found);
 
    if (`${project.id}.yaml` !== parsed.file) {
        report(problems, where, [`\`id: ${project.id}\` doesn't match the filename`]);
    }
 
    const template = templates.find((candidate) => candidate.id === project.template);
 
    if (!template) {
        report(problems, where, [`\`template: ${project.template}\` names no template`]);
        return null;
    }
 
    report(problems, where, projectProblems(project, template));
    return projectView(project, template, snapshotOf(problems, snapshotFiles[project.id]));
};
 
/** A broken file becomes a problem rather than a throw, so one bad edit never blanks the page. */
export const assembleWorkflows = (
    templateFiles: ParsedFile[],
    projectFiles: ParsedFile[],
    snapshotFiles: Record<string, ParsedFile>,
): Workflows => {
    const problems: WorkflowProblem[] = [];
    const templates = templateFiles.flatMap((parsed) => templateOf(problems, parsed) ?? []);
    const projects = projectFiles.flatMap(
        (parsed) => projectOf(problems, templates, parsed, snapshotFiles) ?? [],
    );
 
    return { templates, projects, problems };
};