mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
refactor(cli): render command help with REA's own help owner (#11683)
* refactor(cli): render command help with REA's own help owner Add src/cli/help.ts and move the three incur Help.formatCommand callers (rea mcp add, rea mcp doctor, rea mcp --http) onto it. Option spelling and the unwrapped field type move to src/cli/optionFields.ts, which completion now reads instead of its own copies. A counted incur-import guard records the names still imported from incur so the list can only shrink. Refs #11654 * refactor(cli): share option spelling across CLI owners and count every incur import The argv parser, strict positionals, contract command projection and the command registry now read the flag spelling, declared field type and count marker from src/cli/optionFields.ts instead of their own copies. The incur-import guard also counts namespace, default, star, side-effect and dynamic imports, so `import * as Incur from "incur"` cannot bring a removed owner back. * refactor(cli): format and filter CLI output without incur (#11684) * refactor(cli): format and filter CLI output without incur formatCliOutput encodes TOON (@toon-format/toon), JSON, YAML and JSON Lines itself instead of calling incur's Formatter; YAML still loads on first use. src/cli/outputFilter.ts owns the --filter-output grammar and returns a Result: an unclosed slice is a parse failure, so output validation reads the parser instead of a second bracket scanner, and the parsed paths travel with the output arguments to the result writer and JSON Lines. @toon-format/toon moves from devDependencies to dependencies because REA now imports it at runtime. Refs #11654 * test(cli): pin output encodings and filter grammar with literal cases Literal expected outputs for empty values, TOON scalars, objects and rows, JSON-text re-indentation, nested BigInt values, JSON Lines rows and YAML, and literal parsed paths and unclosed-slice refusals for --filter-output, so a change to either owner fails without comparing against itself.
This commit is contained in:
1 parent
4c8e616029
commit
ec1cf3a9bd
32 files changed
+791
-194
No files matched your search
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Markdown output layout is adapted from Incur's Formatter.
|
||||
* The TOON, JSON, YAML and JSON Lines encodings and the Markdown layout are
|
||||
* adapted from Incur's Formatter.
|
||||
* Copyright (c) 2025-present weth, LLC
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@@ -20,13 +21,18 @@
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
import { Formatter } from "incur";
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
import { encode as encodeToon } from "@toon-format/toon";
|
||||
|
||||
import type { CliOutputFormat } from "../cliOutput.js";
|
||||
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { isRecord } from "../kernel/isRecord.js";
|
||||
|
||||
type Yaml = typeof import("yaml");
|
||||
|
||||
export interface CliDelimitedOutputOptions {
|
||||
readonly fields: readonly string[];
|
||||
readonly metadata: Readonly<Record<string, unknown>>;
|
||||
@@ -95,10 +101,10 @@ export interface PreparedDelimitedOutput {
|
||||
readonly format: "csv" | "tsv";
|
||||
}
|
||||
|
||||
/** Keep REA's Markdown layout compatible with Incur while owning table safety. */
|
||||
/** Render one CLI result document in the selected output format. */
|
||||
export const formatCliOutput = (
|
||||
value: unknown,
|
||||
format: Formatter.Format | "csv" | "tsv",
|
||||
format: CliOutputFormat,
|
||||
delimited?: CliDelimitedOutputOptions,
|
||||
): string => {
|
||||
if (format === "csv" || format === "tsv") {
|
||||
@@ -112,11 +118,58 @@ export const formatCliOutput = (
|
||||
);
|
||||
return prepared.ok
|
||||
? [...formatDelimitedOutputParts(prepared.value)].join("")
|
||||
: Formatter.format(projectAnalysisError(prepared.error), "json");
|
||||
: formatDocument(projectAnalysisError(prepared.error), "json");
|
||||
}
|
||||
return format === "md"
|
||||
? formatMarkdown(value)
|
||||
: Formatter.format(value, format);
|
||||
: formatDocument(value, format);
|
||||
};
|
||||
|
||||
/** JSON with BigInt values written as decimal strings. */
|
||||
const stringifyJson = (value: unknown, indent?: number): string =>
|
||||
JSON.stringify(
|
||||
value,
|
||||
(_key, child: unknown) =>
|
||||
typeof child === "bigint" ? child.toString() : child,
|
||||
indent,
|
||||
);
|
||||
|
||||
/** YAML loads on first use, so runs that never select it skip its startup. */
|
||||
let yaml: Yaml | undefined;
|
||||
const loadYaml = (): Yaml =>
|
||||
(yaml ??= createRequire(import.meta.url)("yaml") as Yaml);
|
||||
|
||||
/** Text that already holds a JSON object or array is re-indented, not quoted. */
|
||||
const formatJson = (value: unknown): string => {
|
||||
if (typeof value === "string") {
|
||||
const trimmed = value.trim();
|
||||
if (trimmed.startsWith("{") || trimmed.startsWith("["))
|
||||
try {
|
||||
return JSON.stringify(JSON.parse(value), null, 2);
|
||||
} catch {
|
||||
// Not JSON after all; encode it as a JSON string below.
|
||||
}
|
||||
}
|
||||
return stringifyJson(value, 2);
|
||||
};
|
||||
|
||||
const formatDocument = (
|
||||
value: unknown,
|
||||
format: "toon" | "json" | "yaml" | "jsonl",
|
||||
): string => {
|
||||
if (value === null || value === undefined) return "";
|
||||
switch (format) {
|
||||
case "json":
|
||||
return formatJson(value);
|
||||
case "yaml":
|
||||
return loadYaml().stringify(value);
|
||||
case "jsonl":
|
||||
return Array.isArray(value)
|
||||
? value.map((item) => stringifyJson(item)).join("\n")
|
||||
: stringifyJson(value);
|
||||
case "toon":
|
||||
return isScalar(value) ? String(value) : encodeToon(value);
|
||||
}
|
||||
};
|
||||
|
||||
/** Validate a complete row page before the caller writes any output. */
|
||||
|
||||
Reference in new issue
Block a user