#!/usr/bin/env node // siax-doctor (BACKLOG P1-19, control AGENT-04): "Repot är // self-diagnostiserande: `pnpm siax:doctor` kör hela standarden lokalt och // skriver ut avvikelser med kontroll-ID." // // This is a repo-local, zero-dependency subset -- NOT the published // @siax/doctor package from sax3l/siax-standard. That package was // deliberately not taken on as a dependency here: at the time this was // written it wasn't verified reachable/stable from this repo's frozen // lockfile install, and a doctor script that can silently fail to install // is worse than no doctor script. This checks the same class of // machine-checkable facts (required files, manifest shape) using only // Node's built-in `fs`/`path` -- no network, no registry. // // Exit code 0 = every check passed. Exit code 1 = at least one check failed // (see the printed control-ID to find the BACKLOG/standard item). // // NOTE for reviewers: this script was authored alongside two sibling PRs // (fix/honest-health-check adds app/api/ready/route.ts; // ci/security-headers-and-scanning adds CODEOWNERS) that this branch does // not itself contain. Run this on a checkout with all three merged for a // clean 0-failure result -- a couple of FAILs on this branch alone, for // exactly those two files, is expected and not a bug in either PR. import { existsSync, readFileSync } from "node:fs"; import { join, dirname } from "node:path"; import { fileURLToPath } from "node:url"; const ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); /** @type {{id: string, label: string, check: () => boolean | string}[]} */ const checks = []; function file(path) { return join(ROOT, path); } function requireFile(id, label, path) { checks.push({ id, label, check: () => (existsSync(file(path)) ? true : `missing: ${path}`), }); } function requireJsonField(id, label, path, fieldPath) { checks.push({ id, label, check: () => { if (!existsSync(file(path))) return `missing file: ${path}`; let data; try { data = JSON.parse(readFileSync(file(path), "utf8")); } catch (e) { return `invalid JSON in ${path}: ${e.message}`; } let cur = data; for (const key of fieldPath.split(".")) { if (cur == null || !(key in cur)) return `missing field "${fieldPath}" in ${path}`; cur = cur[key]; } return true; }, }); } function requireTextMatch(id, label, path, pattern, patternLabel) { checks.push({ id, label, check: () => { if (!existsSync(file(path))) return `missing file: ${path}`; const text = readFileSync(file(path), "utf8"); return pattern.test(text) ? true : `${path} does not contain ${patternLabel}`; }, }); } // -- REPO-* / DOC-* : required root files -------------------------------- requireFile("DOC-01", "README.md exists", "README.md"); requireFile("REPO-08", "LICENSE exists", "LICENSE"); requireFile("REPO-13", ".editorconfig exists", ".editorconfig"); checks.push({ id: "REPO-15/P0-19", label: "no committed .env (real secrets)", check: () => (existsSync(file(".env")) ? "a real .env file is committed -- should never happen" : true), }); requireFile("P1-15", "CODEOWNERS exists", "CODEOWNERS"); requireFile("P1-3", "CHANGELOG.md exists", "CHANGELOG.md"); requireFile("SEC-*", "SECURITY.md exists", "SECURITY.md"); requireFile("AGENT-01", "AGENTS.md exists", "AGENTS.md"); requireFile("DOC-01", "CONTRIBUTING.md exists", "CONTRIBUTING.md"); // -- DOC-02/DOC-04 : ARCHITECTURE.md at the exact standard path ----------- requireFile("DOC-02", "docs/architecture/ARCHITECTURE.md exists at the standard path", "docs/architecture/ARCHITECTURE.md"); requireTextMatch( "DOC-02/v2.0", "ARCHITECTURE.md has the v2.0 '## 5. Ytor och paritet' section", "docs/architecture/ARCHITECTURE.md", /^## 5\. Ytor och paritet/m, "'## 5. Ytor och paritet'", ); requireTextMatch( "DOC-04", "ARCHITECTURE.md has a 'Senast verifierad mot kod' date field", "docs/architecture/ARCHITECTURE.md", /Senast verifierad mot kod:\*\*\s*\d{4}-\d{2}-\d{2}/, "a dated 'Senast verifierad mot kod' field", ); // -- P1-1 / REPO-03/04/06 : manifests -------------------------------------- requireFile("P1-1/REPO-04", "siax.repo.json exists", "siax.repo.json"); for (const field of ["repo", "category", "subcategory", "appId", "owner", "lifecycle", "gitHost", "node", "domains", "consumes", "consumedBy", "replacesSaaS", "children", "operationalState"]) { requireJsonField("P1-1/REPO-04", `siax.repo.json has required field "${field}"`, "siax.repo.json", field); } requireFile("P1-1/REPO-06", "siax.config.json exists", "siax.config.json"); for (const field of ["framework", "build", "start", "port", "healthcheck", "environments"]) { requireJsonField("P1-1/REPO-06", `siax.config.json has required field "${field}"`, "siax.config.json", field); } // -- AGENT-02/03 : prompts ------------------------------------------------- requireFile("AGENT-02", "prompts/000-INDEX.md exists", "prompts/000-INDEX.md"); // -- DX-03 : devcontainer -------------------------------------------------- requireFile("DX-03", ".devcontainer/devcontainer.json exists", ".devcontainer/devcontainer.json"); // -- P1-2 : ADRs ------------------------------------------------------------- requireFile("P1-2", "docs/architecture/decisions/ has at least one ADR", "docs/architecture/decisions/README.md"); // -- CI-14 : contract --------------------------------------------------------- requireFile("A4/DOC", "contracts/openapi.yaml exists", "contracts/openapi.yaml"); // -- CD-04/OBS-04 : health/ready --------------------------------------------- requireFile("P0-3/CD-04", "app/api/health/route.ts exists", "app/api/health/route.ts"); requireFile("P0-3/OBS-04", "app/api/ready/route.ts exists (readiness, not just liveness)", "app/api/ready/route.ts"); // -------------------------------------------------------------------------- let failures = 0; console.log("siax-doctor -- 0pt (repo-local subset, see scripts/siax-doctor.mjs header)\n"); for (const { id, label, check } of checks) { const result = check(); if (result === true) { console.log(` [PASS] ${id.padEnd(14)} ${label}`); } else { failures += 1; console.log(` [FAIL] ${id.padEnd(14)} ${label} -- ${result}`); } } console.log(`\n${checks.length - failures}/${checks.length} checks passed.`); if (failures > 0) { console.log(`${failures} check(s) failed -- see control-IDs above and cross-reference docs/compliance/BACKLOG.md.`); process.exit(1); }