包文件→ .shtukbase/packages/norms-core/checks/pagelib.ts
checks/pagelib.ts
97 行, 4.3 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // Shared part of the checks of norms-core, decisions and owner: records of a project folder, read with the collections
- // package's parser. The `decisions` and `owner` packages require norms-core and import this file by the sibling path
- // `../../norms-core/checks/pagelib.ts` (in a project: .shtukbase/packages/norms-core/checks/pagelib.ts).
- //
- // The working directory is the project root. A record is a page `<folder>/**/<name>.md`; its address is the path from the
- // project root without `.md` (`norms/rules/norms-core/NS-R01`). A finding is one line on stdout, in Russian, naming the
- // record; `report` exits 1 when there are findings and 0 when there are none.
- // The page parser is the collections package's (this package requires it through contours and frame), side by side
- // in .shtukbase/packages/ of a project and in apps/standard/packages/ of the standard.
- import { existsSync, statSync } from 'node:fs';
- import { posix } from 'node:path';
- import { type Head, pystr, parseYaml, readText, splitFrontMatter, truthy, walk } from '../../collections/checks/lib/pages.ts';
- export type { Head };
- /** A record: its address, front matter and body text. */
- export type Page = [address: string, head: Head, body: string];
- /** What a check module exports: its rule (id, title) and the function that returns the findings, one line each. */
- export type Check = { RULE: { id: string; title: string }; check: () => string[] };
- export const ROOT = process.cwd();
- /** The records of the pages under `folder` (recursive, hidden folders skipped); [] when the folder is missing. */
- export function records(folder: string): Page[] {
- const base = posix.join(ROOT, folder);
- const out: Page[] = [];
- if (!existsSync(base) || !statSync(base).isDirectory()) return out;
- for (const [rel, files] of walk(base, (name) => !name.startsWith('.'))) {
- for (const f of files) {
- if (!f.endsWith('.md')) continue;
- const [head, body] = read(posix.join(base, rel, f));
- out.push([posix.normalize(posix.join(folder, rel, f)).slice(0, -3), head, body]);
- }
- }
- return out;
- }
- /** The header and the body text of a page; a header that does not parse is `{}`. */
- export function read(full: string): [Head, string] {
- const [fm, body] = splitFrontMatter(readText(full));
- let head: unknown;
- try {
- head = parseYaml(fm);
- } catch {
- head = {}; // a broken header is the collections checks' finding
- }
- return [head && typeof head === 'object' && !Array.isArray(head) ? (head as Head) : {}, body || ''];
- }
- export function empty(v: unknown): boolean {
- if (Array.isArray(v)) return v.every(empty);
- return v === null || v === undefined || v === false || pystr(v).trim() === '';
- }
- /** A field as a list of non-empty strings. */
- export function values(v: unknown): string[] {
- const items = Array.isArray(v) ? v : [v];
- return items.filter((x) => !empty(x)).map((x) => pystr(x).trim());
- }
- /** '/norms/rules/x/NS-R01.md', 'norms/rules/x/NS-R01' -> 'norms/rules/x/NS-R01'. */
- export function address(v: unknown): string {
- let s = (truthy(v) ? pystr(v) : '').trim().replace(/^["']+|["']+$/g, '');
- if (s.endsWith('.md')) s = s.slice(0, -3);
- return s.replace(/^\/+|\/+$/g, '');
- }
- /** Phase name of a record: `active` for both `active` and `/norms/contours/rules/active.md`. */
- export function status(head: Head): string {
- return address(head.status).split('/').pop()!;
- }
- /** A YYYY-MM-DD field as a day (the same text, so days compare as strings), or null. */
- export function day(v: unknown): string | null {
- const s = pystr(v).trim().slice(0, 10);
- const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s);
- if (!m) return null;
- const d = new Date(Date.UTC(+m[1], +m[2] - 1, +m[3]));
- return d.getUTCFullYear() === +m[1] && d.getUTCMonth() === +m[2] - 1 && d.getUTCDate() === +m[3] ? s : null;
- }
- /** Today in the local time zone, as YYYY-MM-DD. */
- export function today(): string {
- const d = new Date();
- const p = (n: number) => String(n).padStart(2, '0');
- return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`;
- }
- /** Prints the findings, one per line, and sets the exit code: 1 when there are findings, 0 when none. */
- export function report(findings: string[]): void {
- if (findings.length) console.log(findings.join('\n'));
- process.exitCode = findings.length ? 1 : 0;
- }
- /** Runs a check module as an action. */
- export function main(mod: Check): void {
- report(mod.check());
- }