archivo del paquete→ .shtukbase/packages/contours/checks/contourlib.ts
checks/contourlib.ts
69 líneas, 3.2 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // Shared part of the contours checks: contour pages, their phases and the allowed moves.
- //
- // A contour is a page `type: Contour` (`<layer>/contours/<name>.md`) with `phases:`, `entry:` and `collection:`;
- // its phases are pages `type: Phase` in the folder of the same name, and a phase name is the file name without
- // `.md`. A phase lists its exits in `next:` (`- {to: <phase path>, ...}` or `- <phase path>`). A record states its
- // phase in the collection field `{kind: phase, contour: <contour path>}`.
- // The schema library is the collections package's (contours -> frame -> collections), side by side in
- // .shtukbase/packages/ and in apps/standard/packages/ of the standard.
- import { existsSync, readdirSync, statSync } from 'node:fs';
- import { join } from 'node:path';
- import { type Ctx, empty, values } from '../../collections/checks/lib/schemalib.ts';
- import { type Head, parseYaml, pystr, readText, splitFrontMatter, truthy } from '../../collections/checks/lib/speclib.ts';
- export { main } from '../../collections/checks/lib/runner.ts';
- /** Front matter of a project page as an object ({} when there is none or it does not parse). */
- export function headOf(root: string, rel: string): Head {
- const full = join(root, rel.replace(/^\/+/, ''));
- if (!existsSync(full) || !statSync(full).isFile()) return {};
- return headText(readText(full));
- }
- export function headText(text: string): Head {
- const [fm] = splitFrontMatter(text);
- try {
- const h = parseYaml(fm);
- return h && typeof h === 'object' && !Array.isArray(h) ? h : {};
- } catch {
- return {}; // a broken header is another check's finding
- }
- }
- /** '/work/contours/tasks/done.md', 'work/contours/tasks/done/' -> '/work/contours/tasks/done'. */
- export function norm(p: unknown): string {
- let s = pystr(truthy(p) ? p : '').trim().replace(/^["']+|["']+$/g, '');
- if (s.endsWith('.md')) s = s.slice(0, -3);
- const t = s.replace(/^\/+|\/+$/g, '');
- return t ? '/' + t : '';
- }
- /** Paths (without .md) of all pages `type: Contour` in the project. */
- export function contourPages(c: Ctx): string[] {
- const out: string[] = [];
- for (const f of [...c.files()].sort()) {
- if (!f.includes('/contours/') && !f.startsWith('/base/contours')) continue;
- if (pystr(truthy(headOf(c.root, f).type) ? headOf(c.root, f).type : '').trim() === 'Contour') out.push(f.slice(0, -3));
- }
- return out;
- }
- /** {phase name: header} of the phase pages in the contour's folder, in name order; null when the folder is missing. */
- export function phases(root: string, contour: string): Map<string, Head> | null {
- const d = join(root, contour.replace(/^\/+/, ''));
- if (!contour || !existsSync(d) || !statSync(d).isDirectory()) return null;
- const out = new Map<string, Head>();
- for (const f of readdirSync(d).sort())
- if (f.endsWith('.md')) out.set(f.slice(0, -3), headOf(root, contour + '/' + f));
- return out;
- }
- /** Targets of a phase's `next:` as normalized paths. */
- export function exits(phaseHead: Head): string[] {
- const out: string[] = [];
- for (const x of values(truthy(phaseHead.next) ? phaseHead.next : [])) {
- const to = x && typeof x === 'object' && !Array.isArray(x) ? x.to : x;
- if (!empty(to)) out.push(norm(to));
- }
- return out;
- }