包文件→ .shtukbase/packages/content/lib/content.ts
lib/content.ts
40 行, 2.3 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // Shared bits of the content checks: the records of the four content collections and a tiny finding runner.
- // Works from the project root (the working directory of an action) on the installed copy.
- import { existsSync, readdirSync } from 'node:fs';
- import { join } from 'node:path';
- import { type Rec, loadCollection, normPath, pystr, readPage } from '../../collections/checks/lib/pages.ts';
- export type { Rec };
- export const ROOT = process.cwd();
- export const FOLDERS = { topics: 'content/topics', platforms: 'content/platforms', ideas: 'content/ideas', drafts: 'content/drafts' };
- export const records = (folder: string): Rec[] => loadCollection(ROOT, folder.split('/').pop()!, folder);
- export const text = (v: unknown): string => (v === null || v === undefined ? '' : pystr(v).trim());
- /** The phase name of a status (`/content/contours/drafts/review` → `review`). */
- export const phase = (v: unknown): string => text(v).replace(/^\/+|\/+$/g, '').split('/').pop()!.replace(/\.md$/, '').toLowerCase();
- /** A relation value → project path `/a/b.md`, or null. */
- export const ref = (v: unknown, own?: string): string | null => normPath(v, null, own ?? null);
- export const exists = (path: string | null): boolean => !!path && existsSync(join(ROOT, path.replace(/^\/+/, '')));
- export const page = (path: string) => readPage(join(ROOT, path.replace(/^\/+/, '')));
- /** Names that point at an agent rather than a person: agent page paths and titles, harness and model names. */
- export function agentNames(): Set<string> {
- const out = new Set(['agent', 'claude', 'codex', 'opencode', 'dsh', 'hermes', 'haiku', 'sonnet', 'opus', 'gpt', 'ai', 'bot']);
- const dir = join(ROOT, 'agents');
- if (existsSync(dir))
- for (const f of readdirSync(dir).filter((n) => n.endsWith('.md'))) {
- const id = f.slice(0, -3);
- out.add(id.toLowerCase());
- out.add(`/agents/${id}`);
- const t = text(readPage(join(dir, f)).fm.title).toLowerCase();
- if (t) out.add(t);
- }
- return out;
- }
- /** Prints findings (`record: what → fix [code]`), one per line; exit 1 when there are any. */
- export function report(findings: string[], scope: string): void {
- if (findings.length) console.log(findings.join('\n'));
- else console.log(`holds: ${scope}`);
- process.exitCode = findings.length ? 1 : 0;
- }