@hop-top/kit 0.4.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +86 -0
- package/LICENSE +25 -0
- package/README.md +189 -0
- package/dist/aim.d.ts +116 -0
- package/dist/aim.js +417 -0
- package/dist/alias.d.ts +73 -0
- package/dist/alias.js +207 -0
- package/dist/api.d.ts +62 -0
- package/dist/api.js +165 -0
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +41 -0
- package/dist/cli.d.ts +309 -0
- package/dist/cli.js +885 -0
- package/dist/config.d.ts +97 -0
- package/dist/config.js +79 -0
- package/dist/errcorrect.d.ts +37 -0
- package/dist/errcorrect.js +69 -0
- package/dist/llm.d.ts +188 -0
- package/dist/llm.js +356 -0
- package/dist/output.d.ts +213 -0
- package/dist/output.js +447 -0
- package/dist/progress.d.ts +30 -0
- package/dist/progress.js +56 -0
- package/dist/provenance.d.ts +24 -0
- package/dist/provenance.js +32 -0
- package/dist/routellm.d.ts +64 -0
- package/dist/routellm.js +258 -0
- package/dist/rpc.d.ts +25 -0
- package/dist/rpc.js +101 -0
- package/dist/safety.d.ts +24 -0
- package/dist/safety.js +44 -0
- package/dist/scope.d.ts +121 -0
- package/dist/scope.js +380 -0
- package/dist/sqlstore.d.ts +84 -0
- package/dist/sqlstore.js +85 -0
- package/dist/stream.d.ts +35 -0
- package/dist/stream.js +52 -0
- package/dist/tui/index.d.ts +300 -0
- package/dist/tui/index.js +332 -0
- package/dist/upgrade.d.ts +95 -0
- package/dist/upgrade.js +115 -0
- package/dist/xdg.d.ts +169 -0
- package/dist/xdg.js +129 -0
- package/eslint.config.mjs +17 -0
- package/package.json +133 -0
- package/src/README.md +3 -0
- package/src/aim.test.ts +331 -0
- package/src/aim.ts +388 -0
- package/src/alias.test.ts +423 -0
- package/src/alias.ts +256 -0
- package/src/api.test.ts +190 -0
- package/src/api.ts +214 -0
- package/src/auth.test.ts +24 -0
- package/src/auth.ts +37 -0
- package/src/bus.test.ts +178 -0
- package/src/bus.ts +114 -0
- package/src/cli.test.ts +690 -0
- package/src/cli.ts +741 -0
- package/src/completion-values.test.ts +202 -0
- package/src/completion-values.ts +173 -0
- package/src/completion.test.ts +212 -0
- package/src/completion.ts +158 -0
- package/src/compliance.test.ts +134 -0
- package/src/compliance.ts +669 -0
- package/src/config.test.ts +110 -0
- package/src/config.ts +164 -0
- package/src/errcorrect.test.ts +134 -0
- package/src/errcorrect.ts +84 -0
- package/src/flagregister.test.ts +103 -0
- package/src/flagregister.ts +78 -0
- package/src/gen/crud_connect.ts +76 -0
- package/src/gen/crud_pb.ts +269 -0
- package/src/hint.test.ts +315 -0
- package/src/hint.ts +178 -0
- package/src/llm.test.ts +612 -0
- package/src/llm.ts +577 -0
- package/src/log.test.ts +145 -0
- package/src/log.ts +155 -0
- package/src/output/builtins.ts +34 -0
- package/src/output/dispatch.ts +195 -0
- package/src/output/flags.ts +151 -0
- package/src/output/format_help.ts +80 -0
- package/src/output/formatter.ts +167 -0
- package/src/output/formatters/csv.ts +72 -0
- package/src/output/formatters/json.ts +44 -0
- package/src/output/formatters/table.ts +44 -0
- package/src/output/formatters/text.ts +109 -0
- package/src/output/formatters/yaml.ts +47 -0
- package/src/output/projection.ts +126 -0
- package/src/output/registry.ts +88 -0
- package/src/output/template.ts +63 -0
- package/src/output.test.ts +117 -0
- package/src/output.ts +90 -0
- package/src/progress.test.ts +93 -0
- package/src/progress.ts +58 -0
- package/src/provenance.test.ts +59 -0
- package/src/provenance.ts +26 -0
- package/src/routellm.test.ts +179 -0
- package/src/routellm.ts +365 -0
- package/src/router/bert.test.ts +180 -0
- package/src/router/bert.ts +145 -0
- package/src/router/controller.test.ts +183 -0
- package/src/router/controller.ts +165 -0
- package/src/router/integration.test.ts +309 -0
- package/src/router/intent.test.ts +187 -0
- package/src/router/intent.ts +208 -0
- package/src/router/mf.test.ts +66 -0
- package/src/router/mf.ts +52 -0
- package/src/router/random.test.ts +23 -0
- package/src/router/random.ts +13 -0
- package/src/router/router.test.ts +31 -0
- package/src/router/router.ts +62 -0
- package/src/router/server.test.ts +193 -0
- package/src/router/server.ts +134 -0
- package/src/router/sw.test.ts +199 -0
- package/src/router/sw.ts +232 -0
- package/src/rpc.test.ts +47 -0
- package/src/rpc.ts +91 -0
- package/src/safety.test.ts +53 -0
- package/src/safety.ts +42 -0
- package/src/scope-defaults.json +72 -0
- package/src/scope.test.ts +177 -0
- package/src/scope.ts +382 -0
- package/src/setflag.test.ts +110 -0
- package/src/setflag.ts +102 -0
- package/src/sqlstore.test.ts +110 -0
- package/src/sqlstore.ts +144 -0
- package/src/stream.test.ts +69 -0
- package/src/stream.ts +47 -0
- package/src/textflag.test.ts +105 -0
- package/src/textflag.ts +68 -0
- package/src/toolspec.test.ts +187 -0
- package/src/toolspec.ts +279 -0
- package/src/triton/client.test.ts +205 -0
- package/src/triton/client.ts +261 -0
- package/src/tui/anim.test.ts +146 -0
- package/src/tui/anim.ts +134 -0
- package/src/tui/badge.test.ts +41 -0
- package/src/tui/badge.ts +55 -0
- package/src/tui/confirm.test.ts +68 -0
- package/src/tui/confirm.ts +49 -0
- package/src/tui/index.ts +29 -0
- package/src/tui/list.test.ts +84 -0
- package/src/tui/list.ts +72 -0
- package/src/tui/parity.ts +41 -0
- package/src/tui/pills.test.ts +40 -0
- package/src/tui/pills.ts +43 -0
- package/src/tui/progress.test.ts +164 -0
- package/src/tui/progress.ts +120 -0
- package/src/tui/spinner.test.ts +111 -0
- package/src/tui/spinner.ts +96 -0
- package/src/tui/status.test.ts +68 -0
- package/src/tui/status.ts +66 -0
- package/src/upgrade.test.ts +298 -0
- package/src/upgrade.ts +205 -0
- package/src/uri.ts +199 -0
- package/src/wizard.test.ts +441 -0
- package/src/wizard.ts +662 -0
- package/src/xdg.test.ts +202 -0
- package/src/xdg.ts +280 -0
- package/test/output/cols.test.ts +148 -0
- package/test/output/csv.test.ts +129 -0
- package/test/output/dispatch.test.ts +194 -0
- package/test/output/format_help.test.ts +131 -0
- package/test/output/formatter.test.ts +137 -0
- package/test/output/output_flag.test.ts +124 -0
- package/test/output/registry.test.ts +127 -0
- package/test/output/smoke-capture.ts +83 -0
- package/test/output/smoke.test.ts +142 -0
- package/test/output/template.test.ts +95 -0
- package/test/output/text.test.ts +75 -0
- package/test/testdata/api-fixture.json +115 -0
- package/test/testdata/query-vectors.json +187 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output/formatter
|
|
3
|
+
*
|
|
4
|
+
* Core Formatter interface + Options/OptionSpec/ColumnSpec types and
|
|
5
|
+
* parseOptions helper. Mirrors Go's go/console/output Formatter interface.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Kinds of values an OptionSpec accepts. */
|
|
9
|
+
export type OptionType = 'string' | 'int' | 'bool' | 'enum';
|
|
10
|
+
|
|
11
|
+
/** Describes one option accepted by a Formatter via --format-opt key=value. */
|
|
12
|
+
export interface OptionSpec {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly type: OptionType;
|
|
15
|
+
readonly default?: string | number | boolean;
|
|
16
|
+
readonly usage: string;
|
|
17
|
+
readonly enum?: readonly string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Validated map of option values produced by parseOptions. Keys are
|
|
22
|
+
* OptionSpec.name; values are coerced to the spec's declared type.
|
|
23
|
+
*/
|
|
24
|
+
export type Options = Readonly<Record<string, string | number | boolean>>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Column metadata used by table/csv/text formatters and --cols validation.
|
|
28
|
+
*
|
|
29
|
+
* - `header` is the user-visible label and the value matched against --cols.
|
|
30
|
+
* - `key` is the property name on the row object.
|
|
31
|
+
* - `priority` (optional) controls hide-on-overflow for table; higher wins.
|
|
32
|
+
*/
|
|
33
|
+
export interface ColumnSpec {
|
|
34
|
+
readonly header: string;
|
|
35
|
+
readonly key: string;
|
|
36
|
+
readonly priority?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A Formatter encodes structured data to a writable stream.
|
|
41
|
+
*
|
|
42
|
+
* Implementations declare their key, file extensions, and the option keys
|
|
43
|
+
* they accept. The dispatch helper validates --format-opt input against
|
|
44
|
+
* options() before invoking render(), so render() may trust opts to only
|
|
45
|
+
* contain declared keys with values coerced to declared types.
|
|
46
|
+
*/
|
|
47
|
+
export interface Formatter<T = unknown> {
|
|
48
|
+
/** Unique format identifier exposed via --format <key>. */
|
|
49
|
+
readonly key: string;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* File extensions (with leading dot, e.g. ".csv") that map to this
|
|
53
|
+
* formatter for --output extension inference. May be empty.
|
|
54
|
+
*/
|
|
55
|
+
readonly extensions: readonly string[];
|
|
56
|
+
|
|
57
|
+
/** Option specs accepted by this formatter via --format-opt key=value. */
|
|
58
|
+
readonly options: readonly OptionSpec[];
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Renders data to out.
|
|
62
|
+
*
|
|
63
|
+
* @param out Destination writable stream.
|
|
64
|
+
* @param data Single row or readonly array of rows.
|
|
65
|
+
* @param opts Validated options; only declared keys with coerced values.
|
|
66
|
+
* @param cols User-requested column projection; empty means "all columns".
|
|
67
|
+
*/
|
|
68
|
+
render(
|
|
69
|
+
out: NodeJS.WritableStream,
|
|
70
|
+
data: T | readonly T[],
|
|
71
|
+
opts: Options,
|
|
72
|
+
cols: readonly string[],
|
|
73
|
+
): Promise<void> | void;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Validates raw `key=value` pairs against specs and returns a coerced
|
|
78
|
+
* Options map. Unknown keys, type errors, and out-of-enum values throw an
|
|
79
|
+
* Error listing the offending key and the valid set.
|
|
80
|
+
*
|
|
81
|
+
* A pair without `=` (e.g. `"no-header"`) is treated as bool true; only
|
|
82
|
+
* valid when the matching spec has type `'bool'`.
|
|
83
|
+
*
|
|
84
|
+
* Defaults from specs fill in any keys not present in pairs.
|
|
85
|
+
*/
|
|
86
|
+
export function parseOptions(
|
|
87
|
+
pairs: readonly string[],
|
|
88
|
+
specs: readonly OptionSpec[],
|
|
89
|
+
): Options {
|
|
90
|
+
const specByName = new Map<string, OptionSpec>();
|
|
91
|
+
for (const s of specs) {
|
|
92
|
+
specByName.set(s.name, s);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const out: Record<string, string | number | boolean> = {};
|
|
96
|
+
for (const raw of pairs) {
|
|
97
|
+
const eq = raw.indexOf('=');
|
|
98
|
+
const hasEq = eq !== -1;
|
|
99
|
+
const key = (hasEq ? raw.slice(0, eq) : raw).trim();
|
|
100
|
+
const val = hasEq ? raw.slice(eq + 1) : '';
|
|
101
|
+
if (key === '') {
|
|
102
|
+
throw new Error(`empty option key in "${raw}"`);
|
|
103
|
+
}
|
|
104
|
+
const spec = specByName.get(key);
|
|
105
|
+
if (!spec) {
|
|
106
|
+
const valid = specs.map(s => s.name).join(', ');
|
|
107
|
+
throw new Error(`unknown option "${key}" (valid: ${valid})`);
|
|
108
|
+
}
|
|
109
|
+
if (!hasEq) {
|
|
110
|
+
if (spec.type !== 'bool') {
|
|
111
|
+
throw new Error(`option "${key}" requires a value (e.g. ${key}=...)`);
|
|
112
|
+
}
|
|
113
|
+
out[key] = true;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
out[key] = coerce(spec, val);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (const s of specs) {
|
|
120
|
+
if (Object.prototype.hasOwnProperty.call(out, s.name)) continue;
|
|
121
|
+
if (s.default !== undefined) {
|
|
122
|
+
out[s.name] = s.default;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function coerce(
|
|
129
|
+
spec: OptionSpec,
|
|
130
|
+
val: string,
|
|
131
|
+
): string | number | boolean {
|
|
132
|
+
switch (spec.type) {
|
|
133
|
+
case 'string':
|
|
134
|
+
return val;
|
|
135
|
+
case 'int': {
|
|
136
|
+
// Go's strconv.Atoi accepts only base-10, no leading/trailing space.
|
|
137
|
+
if (!/^-?\d+$/.test(val)) {
|
|
138
|
+
throw new Error(`option "${spec.name}": "${val}" is not an int`);
|
|
139
|
+
}
|
|
140
|
+
const n = Number(val);
|
|
141
|
+
if (!Number.isSafeInteger(n)) {
|
|
142
|
+
throw new Error(`option "${spec.name}": "${val}" is not an int`);
|
|
143
|
+
}
|
|
144
|
+
return n;
|
|
145
|
+
}
|
|
146
|
+
case 'bool': {
|
|
147
|
+
// Mirrors Go's strconv.ParseBool accepted set.
|
|
148
|
+
const truthy = new Set(['1', 't', 'T', 'TRUE', 'true', 'True']);
|
|
149
|
+
const falsy = new Set(['0', 'f', 'F', 'FALSE', 'false', 'False']);
|
|
150
|
+
if (truthy.has(val)) return true;
|
|
151
|
+
if (falsy.has(val)) return false;
|
|
152
|
+
throw new Error(`option "${spec.name}": "${val}" is not a bool`);
|
|
153
|
+
}
|
|
154
|
+
case 'enum': {
|
|
155
|
+
const allowed = spec.enum ?? [];
|
|
156
|
+
if (allowed.includes(val)) return val;
|
|
157
|
+
throw new Error(
|
|
158
|
+
`option "${spec.name}": "${val}" not in {${allowed.join(', ')}}`,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Pretty-print an OptionType. */
|
|
165
|
+
export function optionTypeName(t: OptionType): string {
|
|
166
|
+
return t;
|
|
167
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output/formatters/csv
|
|
3
|
+
*
|
|
4
|
+
* Built-in CSV formatter. Uses csv-stringify (sync API) for RFC 4180 quoting.
|
|
5
|
+
* Honors `delimiter`, `no-header`, `quote-all`, `crlf` options + `cols`
|
|
6
|
+
* filtering.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { stringify } from 'csv-stringify/sync';
|
|
10
|
+
import type { Formatter, Options } from '../formatter';
|
|
11
|
+
|
|
12
|
+
export const csvFormatter: Formatter = {
|
|
13
|
+
key: 'csv',
|
|
14
|
+
extensions: ['.csv'],
|
|
15
|
+
options: [
|
|
16
|
+
{ name: 'delimiter', type: 'string', default: ',', usage: 'field delimiter' },
|
|
17
|
+
{ name: 'no-header', type: 'bool', default: false, usage: 'omit header row' },
|
|
18
|
+
{
|
|
19
|
+
name: 'quote-all',
|
|
20
|
+
type: 'bool',
|
|
21
|
+
default: false,
|
|
22
|
+
usage: 'quote every field, not just those needing it',
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: 'crlf',
|
|
26
|
+
type: 'bool',
|
|
27
|
+
default: false,
|
|
28
|
+
usage: 'use CRLF line endings (default LF)',
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
render(out, data, opts: Options, cols) {
|
|
32
|
+
const rows = normalise(data);
|
|
33
|
+
if (rows.length === 0) return;
|
|
34
|
+
|
|
35
|
+
const allHeaders = Object.keys(rows[0] ?? {});
|
|
36
|
+
const headers =
|
|
37
|
+
cols.length > 0 ? cols.filter(c => allHeaders.includes(c)) : allHeaders;
|
|
38
|
+
if (headers.length === 0) return;
|
|
39
|
+
|
|
40
|
+
const delimiter = (opts['delimiter'] as string) ?? ',';
|
|
41
|
+
const noHeader = (opts['no-header'] as boolean) ?? false;
|
|
42
|
+
const quoteAll = (opts['quote-all'] as boolean) ?? false;
|
|
43
|
+
const crlf = (opts['crlf'] as boolean) ?? false;
|
|
44
|
+
|
|
45
|
+
if (delimiter.length !== 1) {
|
|
46
|
+
throw new Error(`option "delimiter": delimiter must be exactly one character`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const records = rows.map(r => headers.map(h => stringify_cell(r[h])));
|
|
50
|
+
const csvRows = noHeader ? records : [headers, ...records];
|
|
51
|
+
|
|
52
|
+
const text = stringify(csvRows, {
|
|
53
|
+
delimiter,
|
|
54
|
+
record_delimiter: crlf ? 'windows' : 'unix',
|
|
55
|
+
quoted: quoteAll,
|
|
56
|
+
// No `header: true` — we already prepend headers ourselves to keep
|
|
57
|
+
// row layout uniform with csv format and to make `no-header` toggle
|
|
58
|
+
// a single decision point.
|
|
59
|
+
});
|
|
60
|
+
out.write(text);
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
function normalise(v: unknown): Record<string, unknown>[] {
|
|
65
|
+
if (Array.isArray(v)) return v as Record<string, unknown>[];
|
|
66
|
+
return [v as Record<string, unknown>];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function stringify_cell(v: unknown): string {
|
|
70
|
+
if (v === null || v === undefined) return '';
|
|
71
|
+
return String(v);
|
|
72
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output/formatters/json
|
|
3
|
+
*
|
|
4
|
+
* Built-in JSON formatter. Honors `indent` option (default 2) + trailing
|
|
5
|
+
* newline. When `cols` is non-empty, projects rows to header-keyed objects.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Formatter, Options } from '../formatter';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Build the JSON formatter. The `columns` ref is optional; when provided
|
|
12
|
+
* (via dispatch with a ColumnSpec list), rows are projected before encode.
|
|
13
|
+
*/
|
|
14
|
+
export const jsonFormatter: Formatter = {
|
|
15
|
+
key: 'json',
|
|
16
|
+
extensions: ['.json'],
|
|
17
|
+
options: [
|
|
18
|
+
{ name: 'indent', type: 'int', default: 2, usage: 'spaces per indent level' },
|
|
19
|
+
],
|
|
20
|
+
render(out, data, opts: Options, cols) {
|
|
21
|
+
const indent = (opts['indent'] as number) ?? 2;
|
|
22
|
+
const value = projectForJson(data, cols);
|
|
23
|
+
out.write(JSON.stringify(value, null, indent) + '\n');
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function projectForJson(
|
|
28
|
+
data: unknown,
|
|
29
|
+
cols: readonly string[],
|
|
30
|
+
): unknown {
|
|
31
|
+
if (cols.length === 0) return data;
|
|
32
|
+
if (Array.isArray(data)) {
|
|
33
|
+
return data.map(row => projectRow(row, cols));
|
|
34
|
+
}
|
|
35
|
+
return projectRow(data, cols);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function projectRow(row: unknown, cols: readonly string[]): unknown {
|
|
39
|
+
if (row === null || typeof row !== 'object') return row;
|
|
40
|
+
const r = row as Record<string, unknown>;
|
|
41
|
+
const out: Record<string, unknown> = {};
|
|
42
|
+
for (const c of cols) out[c] = r[c];
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output/formatters/table
|
|
3
|
+
*
|
|
4
|
+
* Built-in table formatter. Hand-rolled aligner (matches the original
|
|
5
|
+
* output.ts behaviour). Honors `cols` to filter columns; preserves first-
|
|
6
|
+
* row key order otherwise.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { Formatter, Options } from '../formatter';
|
|
10
|
+
|
|
11
|
+
export const tableFormatter: Formatter = {
|
|
12
|
+
key: 'table',
|
|
13
|
+
extensions: [],
|
|
14
|
+
options: [],
|
|
15
|
+
render(out, data, _opts: Options, cols) {
|
|
16
|
+
const rows = normalise(data);
|
|
17
|
+
if (rows.length === 0) return;
|
|
18
|
+
|
|
19
|
+
const allHeaders = Object.keys(rows[0] ?? {});
|
|
20
|
+
const headers =
|
|
21
|
+
cols.length > 0 ? cols.filter(c => allHeaders.includes(c)) : allHeaders;
|
|
22
|
+
if (headers.length === 0) return;
|
|
23
|
+
|
|
24
|
+
const cells = rows.map(row =>
|
|
25
|
+
headers.map(h => String(row[h] ?? '')),
|
|
26
|
+
);
|
|
27
|
+
const widths = headers.map((h, ci) =>
|
|
28
|
+
Math.max(h.length, ...cells.map(r => r[ci].length)),
|
|
29
|
+
);
|
|
30
|
+
const pad = (s: string, w: number) => s + ' '.repeat(w - s.length);
|
|
31
|
+
const line = (parts: string[]) =>
|
|
32
|
+
parts.map((c, i) => pad(c, widths[i])).join(' ');
|
|
33
|
+
|
|
34
|
+
out.write(line(headers) + '\n');
|
|
35
|
+
for (const row of cells) {
|
|
36
|
+
out.write(line(row) + '\n');
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function normalise(v: unknown): Record<string, unknown>[] {
|
|
42
|
+
if (Array.isArray(v)) return v as Record<string, unknown>[];
|
|
43
|
+
return [v as Record<string, unknown>];
|
|
44
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output/formatters/text
|
|
3
|
+
*
|
|
4
|
+
* Built-in text formatter. Three styles: kv, lines, paragraph.
|
|
5
|
+
*
|
|
6
|
+
* - kv: "key{sep}value" lines, blank line between records
|
|
7
|
+
* - lines: tab-separated, no header, one row per line
|
|
8
|
+
* - paragraph: "Record N:" header line then " field: value" lines
|
|
9
|
+
*
|
|
10
|
+
* Zero deps. Mirrors Go text formatter byte-for-byte.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { Formatter, Options } from '../formatter';
|
|
14
|
+
|
|
15
|
+
const STYLE_KV = 'kv';
|
|
16
|
+
const STYLE_LINES = 'lines';
|
|
17
|
+
const STYLE_PARAGRAPH = 'paragraph';
|
|
18
|
+
|
|
19
|
+
export const textFormatter: Formatter = {
|
|
20
|
+
key: 'text',
|
|
21
|
+
extensions: ['.txt'],
|
|
22
|
+
options: [
|
|
23
|
+
{
|
|
24
|
+
name: 'style',
|
|
25
|
+
type: 'enum',
|
|
26
|
+
default: STYLE_KV,
|
|
27
|
+
enum: [STYLE_KV, STYLE_LINES, STYLE_PARAGRAPH],
|
|
28
|
+
usage: 'output style',
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'separator',
|
|
32
|
+
type: 'string',
|
|
33
|
+
default: '=',
|
|
34
|
+
usage: 'kv separator (kv style only)',
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
render(out, data, opts: Options, cols) {
|
|
38
|
+
const rows = normalise(data);
|
|
39
|
+
if (rows.length === 0) return;
|
|
40
|
+
|
|
41
|
+
const allHeaders = Object.keys(rows[0] ?? {});
|
|
42
|
+
const headers =
|
|
43
|
+
cols.length > 0 ? cols.filter(c => allHeaders.includes(c)) : allHeaders;
|
|
44
|
+
if (headers.length === 0) return;
|
|
45
|
+
|
|
46
|
+
const style = ((opts['style'] as string) || STYLE_KV) as
|
|
47
|
+
| 'kv'
|
|
48
|
+
| 'lines'
|
|
49
|
+
| 'paragraph';
|
|
50
|
+
const sep = (opts['separator'] as string) || '=';
|
|
51
|
+
|
|
52
|
+
switch (style) {
|
|
53
|
+
case STYLE_KV:
|
|
54
|
+
return renderKV(out, rows, headers, sep);
|
|
55
|
+
case STYLE_LINES:
|
|
56
|
+
return renderLines(out, rows, headers);
|
|
57
|
+
case STYLE_PARAGRAPH:
|
|
58
|
+
return renderParagraph(out, rows, headers);
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
function normalise(v: unknown): Record<string, unknown>[] {
|
|
64
|
+
if (Array.isArray(v)) return v as Record<string, unknown>[];
|
|
65
|
+
return [v as Record<string, unknown>];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function fmt(v: unknown): string {
|
|
69
|
+
if (v === null || v === undefined) return '';
|
|
70
|
+
return String(v);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function renderKV(
|
|
74
|
+
out: NodeJS.WritableStream,
|
|
75
|
+
rows: readonly Record<string, unknown>[],
|
|
76
|
+
headers: readonly string[],
|
|
77
|
+
sep: string,
|
|
78
|
+
): void {
|
|
79
|
+
rows.forEach((r, i) => {
|
|
80
|
+
if (i > 0) out.write('\n');
|
|
81
|
+
for (const h of headers) {
|
|
82
|
+
out.write(`${h}${sep}${fmt(r[h])}\n`);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function renderLines(
|
|
88
|
+
out: NodeJS.WritableStream,
|
|
89
|
+
rows: readonly Record<string, unknown>[],
|
|
90
|
+
headers: readonly string[],
|
|
91
|
+
): void {
|
|
92
|
+
for (const r of rows) {
|
|
93
|
+
out.write(headers.map(h => fmt(r[h])).join('\t') + '\n');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function renderParagraph(
|
|
98
|
+
out: NodeJS.WritableStream,
|
|
99
|
+
rows: readonly Record<string, unknown>[],
|
|
100
|
+
headers: readonly string[],
|
|
101
|
+
): void {
|
|
102
|
+
rows.forEach((r, i) => {
|
|
103
|
+
if (i > 0) out.write('\n');
|
|
104
|
+
out.write(`Record ${i + 1}:\n`);
|
|
105
|
+
for (const h of headers) {
|
|
106
|
+
out.write(` ${h}: ${fmt(r[h])}\n`);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output/formatters/yaml
|
|
3
|
+
*
|
|
4
|
+
* Built-in YAML formatter. Honors `flow-level` option (default -1, mapping
|
|
5
|
+
* to js-yaml's default block style). When `cols` is non-empty, projects
|
|
6
|
+
* rows to header-keyed objects.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import * as yaml from 'js-yaml';
|
|
10
|
+
import type { Formatter, Options } from '../formatter';
|
|
11
|
+
|
|
12
|
+
export const yamlFormatter: Formatter = {
|
|
13
|
+
key: 'yaml',
|
|
14
|
+
extensions: ['.yaml', '.yml'],
|
|
15
|
+
options: [
|
|
16
|
+
{
|
|
17
|
+
name: 'flow-level',
|
|
18
|
+
type: 'int',
|
|
19
|
+
default: -1,
|
|
20
|
+
usage: 'level at which to switch from block to flow style',
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
render(out, data, opts: Options, cols) {
|
|
24
|
+
const flowLevel = (opts['flow-level'] as number) ?? -1;
|
|
25
|
+
const value = projectForYaml(data, cols);
|
|
26
|
+
out.write(yaml.dump(value, { flowLevel }));
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function projectForYaml(
|
|
31
|
+
data: unknown,
|
|
32
|
+
cols: readonly string[],
|
|
33
|
+
): unknown {
|
|
34
|
+
if (cols.length === 0) return data;
|
|
35
|
+
if (Array.isArray(data)) {
|
|
36
|
+
return data.map(row => projectRow(row, cols));
|
|
37
|
+
}
|
|
38
|
+
return projectRow(data, cols);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function projectRow(row: unknown, cols: readonly string[]): unknown {
|
|
42
|
+
if (row === null || typeof row !== 'object') return row;
|
|
43
|
+
const r = row as Record<string, unknown>;
|
|
44
|
+
const out: Record<string, unknown> = {};
|
|
45
|
+
for (const c of cols) out[c] = r[c];
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output/projection
|
|
3
|
+
*
|
|
4
|
+
* Helpers for projecting structured data through ColumnSpec lists. Mirrors
|
|
5
|
+
* Go's projection.go (filterColumns / projectToMaps / TableHeaders).
|
|
6
|
+
*
|
|
7
|
+
* Unlike Go (which uses `table:""` struct tags), TS callers pass an explicit
|
|
8
|
+
* ColumnSpec[] list. When no columns are provided, callers fall back to
|
|
9
|
+
* deriving headers from the first row's own enumerable keys.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ColumnSpec } from './formatter';
|
|
13
|
+
|
|
14
|
+
/** Normalises data into a readonly array of plain objects (rows). */
|
|
15
|
+
export function normaliseRows<T>(data: T | readonly T[]): readonly T[] {
|
|
16
|
+
return Array.isArray(data) ? (data as readonly T[]) : [data as T];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Returns the list of headers for a row source given an optional ColumnSpec list. */
|
|
20
|
+
export function deriveHeaders(
|
|
21
|
+
rows: readonly unknown[],
|
|
22
|
+
columns?: readonly ColumnSpec[],
|
|
23
|
+
): readonly string[] {
|
|
24
|
+
if (columns && columns.length > 0) {
|
|
25
|
+
return columns.map(c => c.header);
|
|
26
|
+
}
|
|
27
|
+
if (rows.length === 0) return [];
|
|
28
|
+
const first = rows[0];
|
|
29
|
+
if (first === null || typeof first !== 'object') return [];
|
|
30
|
+
return Object.keys(first as Record<string, unknown>);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Filters a ColumnSpec list to those whose `header` appears in `selected`.
|
|
35
|
+
* Order is preserved from `columns` (caller's intended order). Unknown
|
|
36
|
+
* names in `selected` throw with the available header list.
|
|
37
|
+
*/
|
|
38
|
+
export function filterColumns(
|
|
39
|
+
columns: readonly ColumnSpec[],
|
|
40
|
+
selected: readonly string[],
|
|
41
|
+
): readonly ColumnSpec[] {
|
|
42
|
+
const have = new Set(columns.map(c => c.header));
|
|
43
|
+
for (const name of selected) {
|
|
44
|
+
if (!have.has(name)) {
|
|
45
|
+
const valid = columns.map(c => c.header).join(', ');
|
|
46
|
+
throw new Error(`unknown column "${name}" (valid: ${valid})`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const want = new Set(selected);
|
|
50
|
+
return columns.filter(c => want.has(c.header));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Builds a header→key lookup. Used to project rows for json/yaml/csv/text.
|
|
55
|
+
* When no ColumnSpec list is provided, headers map to themselves (the row
|
|
56
|
+
* already keys by the same names).
|
|
57
|
+
*/
|
|
58
|
+
export function buildHeaderToKey(
|
|
59
|
+
columns?: readonly ColumnSpec[],
|
|
60
|
+
): Map<string, string> {
|
|
61
|
+
const out = new Map<string, string>();
|
|
62
|
+
if (!columns) return out;
|
|
63
|
+
for (const c of columns) {
|
|
64
|
+
out.set(c.header, c.key);
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Projects rows to plain objects keyed by header, optionally filtered by
|
|
71
|
+
* `cols`. When `columns` is undefined, rows are returned as-is for JSON/
|
|
72
|
+
* YAML pass-through. When `cols` is empty AND `columns` is defined,
|
|
73
|
+
* every column is included.
|
|
74
|
+
*/
|
|
75
|
+
export function projectRows(
|
|
76
|
+
rows: readonly unknown[],
|
|
77
|
+
columns: readonly ColumnSpec[] | undefined,
|
|
78
|
+
cols: readonly string[],
|
|
79
|
+
): readonly Record<string, unknown>[] {
|
|
80
|
+
const active = columns
|
|
81
|
+
? cols.length > 0
|
|
82
|
+
? filterColumns(columns, cols)
|
|
83
|
+
: columns
|
|
84
|
+
: undefined;
|
|
85
|
+
|
|
86
|
+
return rows.map(row => {
|
|
87
|
+
const r = (row ?? {}) as Record<string, unknown>;
|
|
88
|
+
if (active) {
|
|
89
|
+
const out: Record<string, unknown> = {};
|
|
90
|
+
for (const c of active) {
|
|
91
|
+
out[c.header] = r[c.key];
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
if (cols.length > 0) {
|
|
96
|
+
// No ColumnSpec provided — treat cols as direct keys on the row.
|
|
97
|
+
const out: Record<string, unknown> = {};
|
|
98
|
+
for (const k of cols) out[k] = r[k];
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
return r;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Validates `cols` against either a ColumnSpec list (header set) or, when
|
|
107
|
+
* absent, the keys of the first row. Returns silently when no headers can
|
|
108
|
+
* be derived (caller's data has no schema we can check).
|
|
109
|
+
*/
|
|
110
|
+
export function validateCols(
|
|
111
|
+
rows: readonly unknown[],
|
|
112
|
+
columns: readonly ColumnSpec[] | undefined,
|
|
113
|
+
cols: readonly string[],
|
|
114
|
+
): void {
|
|
115
|
+
if (cols.length === 0) return;
|
|
116
|
+
const headers = columns
|
|
117
|
+
? columns.map(c => c.header)
|
|
118
|
+
: deriveHeaders(rows);
|
|
119
|
+
if (headers.length === 0) return;
|
|
120
|
+
const have = new Set(headers);
|
|
121
|
+
for (const c of cols) {
|
|
122
|
+
if (!have.has(c)) {
|
|
123
|
+
throw new Error(`unknown column "${c}" (valid: ${headers.join(', ')})`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output/registry
|
|
3
|
+
*
|
|
4
|
+
* Registry holds Formatter implementations keyed by Formatter.key. Mirrors
|
|
5
|
+
* Go's *Registry. Built-in formatters register against `defaultRegistry` at
|
|
6
|
+
* module-load time.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { Formatter } from './formatter';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Registry holds Formatter implementations.
|
|
13
|
+
*
|
|
14
|
+
* - `register` throws on duplicate keys (init-time fail-loud).
|
|
15
|
+
* - `override` quietly replaces — use when intentionally swapping a built-in.
|
|
16
|
+
* - `lookup` returns the formatter for a key, or undefined.
|
|
17
|
+
* - `keys` returns sorted format identifiers.
|
|
18
|
+
* - `formatters` returns sorted formatters.
|
|
19
|
+
* - `extensionMap` returns ext→key (e.g. ".csv" → "csv"); later
|
|
20
|
+
* registrations win on collision (alphabetical key order is the tie-break).
|
|
21
|
+
*/
|
|
22
|
+
export class Registry {
|
|
23
|
+
private readonly byKey = new Map<string, Formatter>();
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Adds f to the registry. Throws Error on duplicate key or empty key.
|
|
27
|
+
* Use `override` to intentionally replace.
|
|
28
|
+
*/
|
|
29
|
+
register<T>(f: Formatter<T>): void {
|
|
30
|
+
if (f.key === '') {
|
|
31
|
+
throw new Error('output: formatter key is empty');
|
|
32
|
+
}
|
|
33
|
+
if (this.byKey.has(f.key)) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
`output: formatter "${f.key}" already registered (use override to replace)`,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
this.byKey.set(f.key, f as Formatter);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Replaces (or registers) the formatter for f.key. */
|
|
42
|
+
override<T>(f: Formatter<T>): void {
|
|
43
|
+
if (f.key === '') {
|
|
44
|
+
throw new Error('output: formatter key is empty');
|
|
45
|
+
}
|
|
46
|
+
this.byKey.set(f.key, f as Formatter);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Returns the formatter registered under key, if any. */
|
|
50
|
+
lookup(key: string): Formatter | undefined {
|
|
51
|
+
return this.byKey.get(key);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Returns all registered format keys, sorted alphabetically. */
|
|
55
|
+
keys(): readonly string[] {
|
|
56
|
+
return [...this.byKey.keys()].sort();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Returns all registered formatters in key order. */
|
|
60
|
+
formatters(): readonly Formatter[] {
|
|
61
|
+
return this.keys().map(k => this.byKey.get(k) as Formatter);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Returns extension→key mappings (e.g. ".csv" → "csv") across all
|
|
66
|
+
* registered formatters. Later registrations win on collision; iteration
|
|
67
|
+
* is in sorted-key order so resolution is deterministic.
|
|
68
|
+
*/
|
|
69
|
+
extensionMap(): ReadonlyMap<string, string> {
|
|
70
|
+
const out = new Map<string, string>();
|
|
71
|
+
for (const k of this.keys()) {
|
|
72
|
+
const f = this.byKey.get(k);
|
|
73
|
+
if (!f) continue;
|
|
74
|
+
for (const ext of f.extensions) {
|
|
75
|
+
out.set(ext.toLowerCase(), k);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Module-level Registry. Built-ins register here at module-load time. */
|
|
83
|
+
export const defaultRegistry = new Registry();
|
|
84
|
+
|
|
85
|
+
/** Returns a new, empty Registry — no built-ins registered. */
|
|
86
|
+
export function newRegistry(): Registry {
|
|
87
|
+
return new Registry();
|
|
88
|
+
}
|