ShtukbaseStandard
package file→ .shtukbase/packages/contours/checks/contourlib.ts

checks/contourlib.ts

69 lines, 3.2 KB.

  1. // Shared part of the contours checks: contour pages, their phases and the allowed moves.
  2. //
  3. // A contour is a page `type: Contour` (`<layer>/contours/<name>.md`) with `phases:`, `entry:` and `collection:`;
  4. // its phases are pages `type: Phase` in the folder of the same name, and a phase name is the file name without
  5. // `.md`. A phase lists its exits in `next:` (`- {to: <phase path>, ...}` or `- <phase path>`). A record states its
  6. // phase in the collection field `{kind: phase, contour: <contour path>}`.
  7. // The schema library is the collections package's (contours -> frame -> collections), side by side in
  8. // .shtukbase/packages/ and in apps/standard/packages/ of the standard.
  9. import { existsSync, readdirSync, statSync } from 'node:fs';
  10. import { join } from 'node:path';
  11. import { type Ctx, empty, values } from '../../collections/checks/lib/schemalib.ts';
  12. import { type Head, parseYaml, pystr, readText, splitFrontMatter, truthy } from '../../collections/checks/lib/speclib.ts';
  13. export { main } from '../../collections/checks/lib/runner.ts';
  14. /** Front matter of a project page as an object ({} when there is none or it does not parse). */
  15. export function headOf(root: string, rel: string): Head {
  16. const full = join(root, rel.replace(/^\/+/, ''));
  17. if (!existsSync(full) || !statSync(full).isFile()) return {};
  18. return headText(readText(full));
  19. }
  20. export function headText(text: string): Head {
  21. const [fm] = splitFrontMatter(text);
  22. try {
  23. const h = parseYaml(fm);
  24. return h && typeof h === 'object' && !Array.isArray(h) ? h : {};
  25. } catch {
  26. return {}; // a broken header is another check's finding
  27. }
  28. }
  29. /** '/work/contours/tasks/done.md', 'work/contours/tasks/done/' -> '/work/contours/tasks/done'. */
  30. export function norm(p: unknown): string {
  31. let s = pystr(truthy(p) ? p : '').trim().replace(/^["']+|["']+$/g, '');
  32. if (s.endsWith('.md')) s = s.slice(0, -3);
  33. const t = s.replace(/^\/+|\/+$/g, '');
  34. return t ? '/' + t : '';
  35. }
  36. /** Paths (without .md) of all pages `type: Contour` in the project. */
  37. export function contourPages(c: Ctx): string[] {
  38. const out: string[] = [];
  39. for (const f of [...c.files()].sort()) {
  40. if (!f.includes('/contours/') && !f.startsWith('/base/contours')) continue;
  41. if (pystr(truthy(headOf(c.root, f).type) ? headOf(c.root, f).type : '').trim() === 'Contour') out.push(f.slice(0, -3));
  42. }
  43. return out;
  44. }
  45. /** {phase name: header} of the phase pages in the contour's folder, in name order; null when the folder is missing. */
  46. export function phases(root: string, contour: string): Map<string, Head> | null {
  47. const d = join(root, contour.replace(/^\/+/, ''));
  48. if (!contour || !existsSync(d) || !statSync(d).isDirectory()) return null;
  49. const out = new Map<string, Head>();
  50. for (const f of readdirSync(d).sort())
  51. if (f.endsWith('.md')) out.set(f.slice(0, -3), headOf(root, contour + '/' + f));
  52. return out;
  53. }
  54. /** Targets of a phase's `next:` as normalized paths. */
  55. export function exits(phaseHead: Head): string[] {
  56. const out: string[] = [];
  57. for (const x of values(truthy(phaseHead.next) ? phaseHead.next : [])) {
  58. const to = x && typeof x === 'object' && !Array.isArray(x) ? x.to : x;
  59. if (!empty(to)) out.push(norm(to));
  60. }
  61. return out;
  62. }