audit.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { Command } from "commander";
  2. import { PipelineRunner } from "@actalk/inkos-core";
  3. import { loadConfig, buildPipelineConfig, findProjectRoot, resolveBookId, log, logError } from "../utils.js";
  4. export const auditCommand = new Command("audit")
  5. .description("Audit a chapter for continuity issues")
  6. .argument("[book-id]", "Book ID (auto-detected if only one book)")
  7. .argument("[chapter]", "Chapter number (defaults to latest)")
  8. .option("--json", "Output JSON")
  9. .action(async (bookIdArg: string | undefined, chapterStr: string | undefined, opts) => {
  10. try {
  11. const config = await loadConfig();
  12. const root = findProjectRoot();
  13. // If first arg looks like a number, treat it as chapter (auto-detect book)
  14. let bookId: string;
  15. let chapterNumber: number | undefined;
  16. if (bookIdArg && /^\d+$/.test(bookIdArg)) {
  17. bookId = await resolveBookId(undefined, root);
  18. chapterNumber = parseInt(bookIdArg, 10);
  19. } else {
  20. bookId = await resolveBookId(bookIdArg, root);
  21. chapterNumber = chapterStr ? parseInt(chapterStr, 10) : undefined;
  22. }
  23. const pipeline = new PipelineRunner(buildPipelineConfig(config, root));
  24. if (!opts.json) log(`Auditing "${bookId}"${chapterNumber ? ` chapter ${chapterNumber}` : " (latest)"}...`);
  25. const result = await pipeline.auditDraft(bookId, chapterNumber);
  26. if (opts.json) {
  27. log(JSON.stringify(result, null, 2));
  28. } else {
  29. log(` Chapter ${result.chapterNumber}: ${result.passed ? "PASSED" : "FAILED"}`);
  30. log(` Summary: ${result.summary}`);
  31. if (result.issues.length > 0) {
  32. log(" Issues:");
  33. for (const issue of result.issues) {
  34. log(` [${issue.severity}] ${issue.category}: ${issue.description}`);
  35. }
  36. }
  37. }
  38. } catch (e) {
  39. if (opts.json) {
  40. log(JSON.stringify({ error: String(e) }));
  41. } else {
  42. logError(`Audit failed: ${e}`);
  43. }
  44. process.exit(1);
  45. }
  46. });