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 | import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
import { parse } from "yaml";
import { at } from "../../shared/adapters/git.ts";
import { ExpectedError } from "../../shared/errors.ts";
import { readMap, type WorkflowMap } from "../domain/filing.ts";
import type { Snapshot } from "../domain/snapshot.ts";
const PROJECTS_DIR = "data/workflows/projects";
export const projectIds = (): string[] =>
existsSync(at(PROJECTS_DIR))
? readdirSync(at(PROJECTS_DIR))
.filter((file) => file.endsWith(".yaml"))
.map((file) => file.replace(/\.yaml$/, ""))
.sort((a, b) => a.localeCompare(b))
: [];
/** The parsed project file, null when it doesn't exist; a YAML error throws with its line. */
export const readProject = (id: string): unknown => {
const path = at(PROJECTS_DIR, `${id}.yaml`);
return existsSync(path) ? (parse(readFileSync(path, "utf8")) as unknown) : null;
};
export const snapshotPath = (id: string): string => `${PROJECTS_DIR}/${id}.snapshot.json`;
export const readSnapshot = (id: string): Snapshot | null => {
try {
return JSON.parse(readFileSync(at(snapshotPath(id)), "utf8")) as Snapshot;
} catch {
return null;
}
};
export const writeSnapshot = (id: string, snapshot: Snapshot): void =>
writeFileSync(at(snapshotPath(id)), `${JSON.stringify(snapshot, null, 4)}\n`, "utf8");
const TEMPLATES_DIR = "data/workflows/templates";
/** The parsed template file, null when it doesn't exist. */
export const readTemplate = (id: string): unknown => {
const path = at(TEMPLATES_DIR, `${id}.yaml`);
return existsSync(path) ? (parse(readFileSync(path, "utf8")) as unknown) : null;
};
/** The project's map, from its file and the template it names. */
export const projectMap = (id: string): WorkflowMap => {
const project = readProject(id);
const templateId = (project as { template?: unknown } | null)?.template;
const template = typeof templateId === "string" ? readTemplate(templateId) : null;
if (!template) throw new ExpectedError(`${id}.yaml names no template that exists.`);
return readMap(template, project);
};
/** The one project id a command was given, checked against the project files. */
export const oneProject = (positionals: string[]): string => {
const known = projectIds();
const [id] = positionals;
if (!id || positionals.length > 1) {
throw new ExpectedError(`Name one project: ${known.join(", ") || "(none yet)"}.`);
}
if (!known.includes(id)) {
throw new ExpectedError(`No project file for ${id}. Known: ${known.join(", ")}.`);
}
return id;
};
|