feat: генерация человекочитаемой документации из спецификации (BHL-017)
- bhl spec render: index + страницы требований и антипаттернов, детерминированный вывод - docs/spec/ закоммичен; SSOT остаётся spec/; метка 'не редактировать вручную' - тесты: идемпотентность, детект устаревания, синхронность с Git - AGENTS.md и README: правила сгенерированных страниц
This commit is contained in:
+39
-5
@@ -1,5 +1,7 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { emit, ExitCode, failEnvelope, okEnvelope } from './envelope.ts';
|
||||
import { renderSpec } from './render.ts';
|
||||
import { resolveProfiles } from './resolve.ts';
|
||||
import { specList, specShow, validateManifest } from './spec.ts';
|
||||
import { validateReport } from './report.ts';
|
||||
@@ -9,6 +11,7 @@ const USAGE_MESSAGE =
|
||||
'Usage: bhl <version|validate|spec|profile|report|manifest|audit> [args]; ' +
|
||||
'spec list [--spec-root <path>] [--domain <id>] [--capability <id>]; ' +
|
||||
'spec show <req-id> [--spec-root <path>]; ' +
|
||||
'spec render --spec-root <path> --out <dir>; ' +
|
||||
'profile resolve <spec-root> [--profile <id>] [--preset <id>]; ' +
|
||||
'report validate <report.json> [--spec-root <path>]; ' +
|
||||
'manifest validate <manifest.yaml> [--spec-root <path>]; ' +
|
||||
@@ -177,6 +180,7 @@ interface SpecFlags {
|
||||
root: string;
|
||||
domain?: string;
|
||||
capability?: string;
|
||||
out?: string;
|
||||
positional: string[];
|
||||
}
|
||||
|
||||
@@ -194,8 +198,10 @@ function takeNamedFlag(
|
||||
target.root = value;
|
||||
} else if (arg === '--domain') {
|
||||
target.domain = value;
|
||||
} else {
|
||||
} else if (arg === '--capability') {
|
||||
target.capability = value;
|
||||
} else {
|
||||
target.out = value;
|
||||
}
|
||||
return { next: cursor + 2 };
|
||||
}
|
||||
@@ -205,7 +211,7 @@ function parseSpecArgs(args: readonly string[]): SpecFlags | { error: string } {
|
||||
let cursor = 0;
|
||||
while (cursor < args.length) {
|
||||
const arg = args[cursor] ?? '';
|
||||
if (arg === '--spec-root' || arg === '--domain' || arg === '--capability') {
|
||||
if (arg === '--spec-root' || arg === '--domain' || arg === '--capability' || arg === '--out') {
|
||||
const taken = takeNamedFlag(args, cursor, target, arg);
|
||||
if ('error' in taken) {
|
||||
return taken;
|
||||
@@ -268,10 +274,35 @@ function emitSpecShow(parsed: SpecFlags): number {
|
||||
return ExitCode.Ok;
|
||||
}
|
||||
|
||||
function emitSpecRender(parsed: SpecFlags): number {
|
||||
const out = parsed.out;
|
||||
if (out === undefined) {
|
||||
emit(failEnvelope('spec', 'USAGE', 'spec render requires --out <dir>'));
|
||||
return ExitCode.Usage;
|
||||
}
|
||||
const result = renderSpec(parsed.root);
|
||||
if (!result.ok) {
|
||||
return emitValidationFailure(
|
||||
'spec',
|
||||
{ root: parsed.root, errors: result.diagnostics },
|
||||
result.diagnostics.length,
|
||||
);
|
||||
}
|
||||
for (const file of result.files) {
|
||||
const target = path.join(out, file.path);
|
||||
mkdirSync(path.dirname(target), { recursive: true });
|
||||
writeFileSync(target, file.content, 'utf8');
|
||||
}
|
||||
emit(
|
||||
okEnvelope('spec', { root: parsed.root, out, files: result.files.map((file) => file.path) }),
|
||||
);
|
||||
return ExitCode.Ok;
|
||||
}
|
||||
|
||||
function runSpec(command: string, rest: readonly string[]): number {
|
||||
const [sub, ...args] = rest;
|
||||
if (sub !== 'list' && sub !== 'show') {
|
||||
emit(failEnvelope(command, 'USAGE', 'spec requires the subcommand list or show'));
|
||||
if (sub !== 'list' && sub !== 'show' && sub !== 'render') {
|
||||
emit(failEnvelope(command, 'USAGE', 'spec requires the subcommand list, show or render'));
|
||||
return ExitCode.Usage;
|
||||
}
|
||||
const parsed = parseSpecArgs(args);
|
||||
@@ -282,6 +313,9 @@ function runSpec(command: string, rest: readonly string[]): number {
|
||||
if (sub === 'list') {
|
||||
return emitSpecList(parsed);
|
||||
}
|
||||
if (sub === 'render') {
|
||||
return emitSpecRender(parsed);
|
||||
}
|
||||
return emitSpecShow(parsed);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user