refactor: merge view into index and rename test tiers (task 0003)

Fold the view command module (viewFile, the error type, the read
adapter) into index.ts so the command functions sit beside the guarded
CLI, and generalise formatViewError into formatError matching over a
CliError union that new fallible commands extend.

Align the test files with ADR 0009's tiers by name:
- index_test.ts now holds the in-process integration tests of the
  command functions (formerly view_test.ts)
- end_to_end_test.ts holds the black-box subprocess e2e (formerly
  index_test.ts)
This commit is contained in:
2026-07-28 23:15:41 -04:00
parent 02020309eb
commit a425a6b312
6 changed files with 172 additions and 170 deletions

View File

@@ -1,6 +1,35 @@
#!/usr/bin/env bun
import { readFileSync } from "node:fs";
import { parse } from "@kitchen-md/core";
import { Command } from "commander";
import { formatViewError, viewFile } from "./view.ts";
import { err, ok, type Result } from "neverthrow";
import { render } from "./render.ts";
export type ViewError = { tag: "read-failed"; path: string; cause: string };
// The union of every error a command can surface to the boundary; new fallible commands add their variants here.
export type CliError = ViewError;
export function viewFile(path: string): Result<string, ViewError> {
return readFile(path).map((content) => render(parse(content)));
}
export function formatError(error: CliError): string {
switch (error.tag) {
case "read-failed":
return `cannot read '${error.path}': ${error.cause}`;
}
}
// The one place a throwing API is turned into a Result; nothing above this leaks exceptions.
function readFile(path: string): Result<string, ViewError> {
try {
return ok(readFileSync(path, "utf8"));
} catch (error) {
const cause = error instanceof Error ? error.message : String(error);
return err({ tag: "read-failed", path, cause });
}
}
const program = new Command();
@@ -14,7 +43,7 @@ program
viewFile(file).match(
(output) => process.stdout.write(output),
(error) => {
process.stderr.write(`kitchen: ${formatViewError(error)}\n`);
process.stderr.write(`kitchen: ${formatError(error)}\n`);
process.exit(1);
},
);