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

96.96% Statements 32/33
95% Branches 19/20
100% Functions 11/11
100% Lines 27/27

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                                              42x     42x 46x               42x 17x 17x       42x       16x 6x   6x 4x 4x 5x 1x   2x     42x     42x   42x 168x     42x 18x 108x   24x 18x           18x                       42x 5x                
import type { PageProblem } from "./types/PageProblem.ts";
import type { TicketSize } from "./types/TicketSize.ts";
 
/** A page the project adds once, as an extra naming one of its template's page types. */
export interface NewPage {
    id: string;
    type: string;
    title: string;
}
 
export interface NewProject {
    id: string;
    title: string;
    template: string;
    area: string;
    milestone: string;
    summary: string;
    switches: Record<string, boolean>;
    ticketSize: TicketSize;
    extras: NewPage[];
}
 
/** Also the file name, so nothing that could climb out of `projects/` gets through. */
export const PROJECT_ID = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
 
/** A title as a project id: accents dropped, anything else between words a dash. */
export const projectSlug = (title: string): string =>
    title
        .normalize("NFD")
        .replace(/\p{M}/gu, "")
        .toLowerCase()
        .replace(/[^a-z0-9]+/g, "-")
        .replace(/^-|-$/g, "");
 
/** A page's block id from its title, ending in `-page` like the hand-written extras do. */
export const pageId = (title: string): string => {
    const slug = projectSlug(title);
    return !slug || /(?:^|-)page$/.test(slug) ? slug : `${slug}-page`;
};
 
/** Why each page can't be written yet, or null; `taken` are the template's own block ids. */
export const pageProblems = (
    pages: { title: string }[],
    taken: Set<string>,
): (PageProblem | null)[] =>
    pages.map(({ title }, index) => {
        const id = pageId(title);
 
        if (!title.trim()) return { code: "untitled", id };
        Iif (!id) return { code: "noId", id };
        if (taken.has(id)) return { code: "taken", id };
        if (pages.slice(0, index).some((other) => pageId(other.title) === id)) {
            return { code: "duplicate", id };
        }
        return null;
    });
 
const PLAIN = /^[A-Z][\w.,()/-]*(?:[ :][\w.,()/-]+)*$/i;
 
// YAML 1.1 readers still turn these into booleans or null when they're left unquoted.
const KEYWORDS = /^(?:true|false|yes|no|on|off|y|n|null)$/i;
 
const scalar = (text: string): string =>
    PLAIN.test(text) && !KEYWORDS.test(text) ? text : JSON.stringify(text);
 
/** A new project's file, written without the `yaml` package so the browser can build it too. */
export const projectYaml = (project: NewProject): string => {
    const lines = (["id", "title", "template", "area", "milestone", "summary"] as const).map(
        (key) => `${key}: ${scalar(project[key])}`,
    );
    const switches = Object.entries(project.switches).map(([key, on]) => `  ${scalar(key)}: ${on}`);
    const extras = project.extras.flatMap(({ id, type, title }) => [
        `  - id: ${scalar(id)}`,
        `    type: ${scalar(type)}`,
        `    title: ${scalar(title)}`,
    ]);
 
    return [
        ...lines,
        "",
        switches.length > 0 ? ["switches:", ...switches].join("\n") : "switches: {}",
        "",
        `ticketSize: ${project.ticketSize}`,
        ...(extras.length > 0 ? ["", "extras:", ...extras] : []),
        "",
    ].join("\n");
};
 
/** The opening of `projects/<id>.diary.md`, before its first line. */
export const diaryHeader = (title: string): string =>
    [
        `# 📓 ${title} diary`,
        "",
        "What in the workflow worked and what went wrong. The format and when to write are in",
        "[the workflows README](../../../docs/workflows/README.md#diary).",
        "",
        "",
    ].join("\n");