feat: каркас bhl и валидатор спецификации (BHL-020, BHL-011)
packages/bhl: @baseline-harness/bhl 0.0.1 — детерминированный CLI без LLM/оркестрации/сервера/БД; JSON-конверт и коды выхода 0/1/2/3/4 задокументированы и закреплены тестами. validate: Ajv 2020-12 по схемам <root>/schemas, семантическая сверка ID, ссылок, RFC-2119 слов и циклов depends-on. Местный строгий gate (prettier, eslint --max-warnings 0, tsc strict) зелёный: 10 тестов.
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { emit, ExitCode, failEnvelope, okEnvelope } from './envelope.ts';
|
||||
import { validateSpec } from './validate.ts';
|
||||
|
||||
interface PackageManifest {
|
||||
name: string;
|
||||
version: string;
|
||||
}
|
||||
|
||||
const USAGE_MESSAGE =
|
||||
'Usage: bhl <version|validate|spec|profile|report|audit> [args]; ' +
|
||||
'JSON envelope on stdout; exit codes: 0 ok, 1 validation failed, 2 usage, ' +
|
||||
'3 not implemented, 4 internal.';
|
||||
|
||||
const KNOWN: ReadonlySet<string> = new Set([
|
||||
'version',
|
||||
'validate',
|
||||
'spec',
|
||||
'profile',
|
||||
'report',
|
||||
'audit',
|
||||
]);
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null;
|
||||
}
|
||||
|
||||
function manifest(): PackageManifest {
|
||||
const parsed: unknown = JSON.parse(
|
||||
readFileSync(new URL('../package.json', import.meta.url), 'utf8'),
|
||||
);
|
||||
if (
|
||||
!isRecord(parsed) ||
|
||||
typeof parsed['name'] !== 'string' ||
|
||||
typeof parsed['version'] !== 'string'
|
||||
) {
|
||||
throw new Error('package.json must carry string name and version');
|
||||
}
|
||||
return { name: parsed['name'], version: parsed['version'] };
|
||||
}
|
||||
|
||||
export function run(argv: readonly string[]): number {
|
||||
const [command, ...rest] = argv;
|
||||
if (command === undefined || command === '-h' || command === '--help') {
|
||||
emit(failEnvelope(command ?? '', 'USAGE', USAGE_MESSAGE));
|
||||
return ExitCode.Usage;
|
||||
}
|
||||
if (!KNOWN.has(command)) {
|
||||
emit(failEnvelope(command, 'USAGE', `Unknown command "${command}". ${USAGE_MESSAGE}`));
|
||||
return ExitCode.Usage;
|
||||
}
|
||||
if (command === 'version') {
|
||||
const { name, version } = manifest();
|
||||
emit(okEnvelope(command, { name, version, args: rest }));
|
||||
return ExitCode.Ok;
|
||||
}
|
||||
if (command === 'validate') {
|
||||
const [target] = rest;
|
||||
if (target === undefined) {
|
||||
emit(failEnvelope(command, 'USAGE', 'validate requires a spec root path'));
|
||||
return ExitCode.Usage;
|
||||
}
|
||||
const result = validateSpec(target);
|
||||
const data = { root: target, files: result.files, errors: result.diagnostics };
|
||||
if (result.ok) {
|
||||
emit(okEnvelope(command, data));
|
||||
return ExitCode.Ok;
|
||||
}
|
||||
emit({
|
||||
ok: false,
|
||||
command,
|
||||
data,
|
||||
error: {
|
||||
code: 'VALIDATION_FAILED',
|
||||
message: `${String(result.diagnostics.length)} diagnostic(s)`,
|
||||
},
|
||||
});
|
||||
return ExitCode.ValidationFailed;
|
||||
}
|
||||
emit(
|
||||
failEnvelope(
|
||||
command,
|
||||
'NOT_IMPLEMENTED',
|
||||
`Command "${command}" is scheduled by the laboratory backlog (Wave B/C).`,
|
||||
),
|
||||
);
|
||||
return ExitCode.NotImplemented;
|
||||
}
|
||||
|
||||
process.exitCode = run(process.argv.slice(2));
|
||||
Reference in New Issue
Block a user