@turingcode/cli 0.0.0-stage → 0.1.0
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/dist/cli/create-cli.js +21 -0
- package/dist/cli/index.js +19 -0
- package/dist/core/mcp.js +80 -0
- package/dist/features/clock/command.js +66 -0
- package/dist/features/table/ast.js +177 -0
- package/dist/features/table/command.js +95 -0
- package/dist/features/table/contract.js +487 -0
- package/dist/features/table/contract_check.js +265 -0
- package/dist/features/table/describe.js +197 -0
- package/dist/features/table/diagnostics.js +129 -0
- package/dist/features/table/format.js +75 -0
- package/dist/features/table/fraction.js +130 -0
- package/dist/features/table/primitives.js +268 -0
- package/dist/features/table/shape.js +137 -0
- package/dist/features/table/source_kinds.js +41 -0
- package/dist/features/table/table_check.js +538 -0
- package/dist/features/table/tables.js +223 -0
- package/package.json +30 -3
- package/README.md +0 -3
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import yargs from "yargs/yargs";
|
|
2
|
+
import clock from "../features/clock/command.js";
|
|
3
|
+
import table from "../features/table/command.js";
|
|
4
|
+
const commands = [clock, ...table];
|
|
5
|
+
/**
|
|
6
|
+
* Builds the yargs parser with every feature command registered.
|
|
7
|
+
*
|
|
8
|
+
* @param {string[]} args - Arguments after `turingcode`.
|
|
9
|
+
* @returns The parser; call `parseAsync()` to run it.
|
|
10
|
+
*/
|
|
11
|
+
export function createCli(args) {
|
|
12
|
+
return yargs(args)
|
|
13
|
+
.scriptName("turingcode")
|
|
14
|
+
.usage("Usage: $0 <command> [options]")
|
|
15
|
+
.command(commands)
|
|
16
|
+
.demandCommand(1)
|
|
17
|
+
.strict()
|
|
18
|
+
.help()
|
|
19
|
+
.exitProcess(false)
|
|
20
|
+
.fail((message, error) => { throw error ?? new Error(message); });
|
|
21
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { hideBin } from "yargs/helpers";
|
|
3
|
+
import { createCli } from "./create-cli.js";
|
|
4
|
+
/**
|
|
5
|
+
* Runs the CLI. Errors propagate here, are printed, and exit with code 1.
|
|
6
|
+
*
|
|
7
|
+
* @param {string[]} [args] - Arguments after `turingcode`.
|
|
8
|
+
* @returns {Promise<void>} Resolves when the command finished.
|
|
9
|
+
*/
|
|
10
|
+
export async function main(args = hideBin(process.argv)) {
|
|
11
|
+
try {
|
|
12
|
+
await createCli(args).parseAsync();
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
process.stderr.write(`turingcode: ${error.message}\nRun "turingcode --help" for usage.\n`);
|
|
16
|
+
process.exitCode = 1;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
void main();
|
package/dist/core/mcp.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** Default public MCP gateway (public-mcp-tools in the turingcode repo). */
|
|
2
|
+
export const DEFAULT_URL = "https://mcp.turingcode.ai/mcp";
|
|
3
|
+
/**
|
|
4
|
+
* Calls one tool on the Turing Code MCP gateway.
|
|
5
|
+
*
|
|
6
|
+
* The gateway is stateless streamable-HTTP, so a single `tools/call` POST
|
|
7
|
+
* works without an `initialize` handshake.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} name - Tool name, e.g. `stm32_cubetools_clock_solve`.
|
|
10
|
+
* @param {Record<string, unknown>} args - Tool arguments.
|
|
11
|
+
* @param {string} token - Bearer token for the gateway.
|
|
12
|
+
* @param {string} [url=DEFAULT_URL] - Gateway endpoint.
|
|
13
|
+
* @returns {Promise<ToolResult>} The tool's text content and error flag.
|
|
14
|
+
* @throws {Error} On HTTP errors or a JSON-RPC error reply.
|
|
15
|
+
*/
|
|
16
|
+
export async function callTool(name, args, token, url = DEFAULT_URL) {
|
|
17
|
+
const response = await fetch(url, {
|
|
18
|
+
method: "POST",
|
|
19
|
+
headers: {
|
|
20
|
+
"Authorization": `Bearer ${token}`,
|
|
21
|
+
"Content-Type": "application/json",
|
|
22
|
+
"Accept": "application/json, text/event-stream",
|
|
23
|
+
},
|
|
24
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }),
|
|
25
|
+
});
|
|
26
|
+
const body = await response.text();
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
throw new Error(`gateway HTTP ${response.status}: ${body.trim() || response.statusText}`);
|
|
29
|
+
}
|
|
30
|
+
return parseToolReply(body);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Turns a gateway reply (plain JSON or an SSE stream) into a ToolResult.
|
|
34
|
+
*
|
|
35
|
+
* @param {string} body - Raw response body.
|
|
36
|
+
* @returns {ToolResult} The joined text content and error flag.
|
|
37
|
+
* @throws {Error} When the reply holds no JSON-RPC response or carries an error.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* parseToolReply('data: {"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","text":"ok"}]}}');
|
|
41
|
+
* // { text: "ok", isError: false }
|
|
42
|
+
*/
|
|
43
|
+
export function parseToolReply(body) {
|
|
44
|
+
const payloads = body.trimStart().startsWith("{")
|
|
45
|
+
? [body]
|
|
46
|
+
: body.split("\n").filter(line => line.startsWith("data:")).map(line => line.slice(5));
|
|
47
|
+
const reply = payloads.map(p => JSON.parse(p)).find(m => m.id !== undefined);
|
|
48
|
+
if (!reply) {
|
|
49
|
+
throw new Error("gateway sent no JSON-RPC response");
|
|
50
|
+
}
|
|
51
|
+
if (reply.error) {
|
|
52
|
+
throw new Error(`gateway error ${reply.error.code}: ${reply.error.message}`);
|
|
53
|
+
}
|
|
54
|
+
const content = reply.result?.content ?? [];
|
|
55
|
+
return {
|
|
56
|
+
text: content.filter(c => c.type === "text").map(c => c.text).join("\n"),
|
|
57
|
+
isError: Boolean(reply.result?.isError),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Runs a server command: calls the tool with the gateway token from
|
|
62
|
+
* TURINGCODE_TOKEN and prints its text; a tool error sets exit code 1.
|
|
63
|
+
*
|
|
64
|
+
* @param {string} name - Tool name.
|
|
65
|
+
* @param {Record<string, unknown>} args - Tool arguments; undefined entries are dropped.
|
|
66
|
+
* @returns {Promise<void>} Resolves once the reply is printed.
|
|
67
|
+
* @throws {Error} When TURINGCODE_TOKEN is not set, or the gateway call fails.
|
|
68
|
+
*/
|
|
69
|
+
export async function runServerTool(name, args) {
|
|
70
|
+
const token = process.env.TURINGCODE_TOKEN?.trim();
|
|
71
|
+
if (!token) {
|
|
72
|
+
throw new Error("set TURINGCODE_TOKEN to your Turing Code gateway token");
|
|
73
|
+
}
|
|
74
|
+
const given = Object.fromEntries(Object.entries(args).filter(([, v]) => v !== undefined));
|
|
75
|
+
const result = await callTool(name, given, token, process.env.TURINGCODE_URL || DEFAULT_URL);
|
|
76
|
+
(result.isError ? process.stderr : process.stdout).write(result.text + "\n");
|
|
77
|
+
if (result.isError) {
|
|
78
|
+
process.exitCode = 1;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { runServerTool } from "../../core/mcp.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads a JSON flag: inline JSON, or a path to a .json file.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} value - Raw flag value.
|
|
7
|
+
* @returns {unknown} The parsed JSON.
|
|
8
|
+
* @throws {Error} When the file cannot be read or the JSON is invalid.
|
|
9
|
+
*/
|
|
10
|
+
export function readJsonArg(value) {
|
|
11
|
+
const text = value.trimStart().startsWith("{") ? value : readFileSync(value, "utf-8");
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(text);
|
|
14
|
+
}
|
|
15
|
+
catch (e) {
|
|
16
|
+
throw new Error(`invalid JSON (${e.message})`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Rejects number flags that yargs parsed to NaN.
|
|
21
|
+
*
|
|
22
|
+
* @param {number} value - Parsed flag value.
|
|
23
|
+
* @returns {number} The value.
|
|
24
|
+
* @throws {Error} When it is not a finite number.
|
|
25
|
+
*/
|
|
26
|
+
const finite = (value) => {
|
|
27
|
+
if (!Number.isFinite(value)) {
|
|
28
|
+
throw new Error(`expected a number, got '${value}'`);
|
|
29
|
+
}
|
|
30
|
+
return value;
|
|
31
|
+
};
|
|
32
|
+
const sharedOptions = {
|
|
33
|
+
project: { type: "string", describe: "Project folder name on the server (see projects_list)" },
|
|
34
|
+
format: { choices: ["markdown", "json"], describe: "Output format (server default: markdown)" },
|
|
35
|
+
"hse-ppm": { type: "number", coerce: finite, describe: "HSE crystal tolerance in ppm, for the worst case" },
|
|
36
|
+
vdd: { type: "number", coerce: finite, describe: "Supply voltage in volts, for the SPI limits" },
|
|
37
|
+
};
|
|
38
|
+
const solve = {
|
|
39
|
+
command: "solve",
|
|
40
|
+
describe: "Solve a clock tree for a target SYSCLK and print the .ioc patch (server)",
|
|
41
|
+
builder: cli => cli.options({
|
|
42
|
+
...sharedOptions,
|
|
43
|
+
config: { type: "string", coerce: readJsonArg, describe: "Design as inline JSON or a .json file path" },
|
|
44
|
+
target: { type: "number", coerce: finite, describe: "Target SYSCLK in Hz" },
|
|
45
|
+
objective: { choices: ["exact_target", "min_error", "max_clock"], describe: "What to optimise" },
|
|
46
|
+
}),
|
|
47
|
+
handler: args => runServerTool("stm32_cubetools_clock_solve", {
|
|
48
|
+
project_name: args.project, config: args.config, target_sysclk_hz: args.target, objective: args.objective,
|
|
49
|
+
output_format: args.format, hse_ppm: args["hse-ppm"], vdd: args.vdd,
|
|
50
|
+
}),
|
|
51
|
+
};
|
|
52
|
+
const audit = {
|
|
53
|
+
command: "audit",
|
|
54
|
+
describe: "Audit a project's clocks and peripheral timing (server)",
|
|
55
|
+
builder: cli => cli.options(sharedOptions).demandOption("project"),
|
|
56
|
+
handler: args => runServerTool("stm32_cubetools_clock_audit", {
|
|
57
|
+
project_name: args.project, output_format: args.format, hse_ppm: args["hse-ppm"], vdd: args.vdd,
|
|
58
|
+
}),
|
|
59
|
+
};
|
|
60
|
+
const command = {
|
|
61
|
+
command: "clock",
|
|
62
|
+
describe: "STM32 clock tree tools (needs TURINGCODE_TOKEN; TURINGCODE_URL overrides the gateway)",
|
|
63
|
+
builder: cli => cli.command(solve).command(audit).demandCommand(1),
|
|
64
|
+
handler: () => { },
|
|
65
|
+
};
|
|
66
|
+
export default command;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { ArithmeticError, Fraction } from "./fraction.js";
|
|
2
|
+
/** Raised when an expression cannot be parsed or evaluated. */
|
|
3
|
+
export class AstError extends Error {
|
|
4
|
+
}
|
|
5
|
+
/** Raised when an expression exceeds its node bound (maps to SEARCH_BOUND_EXCEEDED). */
|
|
6
|
+
export class BoundError extends Error {
|
|
7
|
+
bound;
|
|
8
|
+
limit;
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} bound - The `contract.bounds` key that was exceeded.
|
|
11
|
+
* @param {number} limit - Its value.
|
|
12
|
+
*/
|
|
13
|
+
constructor(bound, limit) {
|
|
14
|
+
super(`${bound} (${limit}) exceeded`);
|
|
15
|
+
this.bound = bound;
|
|
16
|
+
this.limit = limit;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const TOKEN = new RegExp([
|
|
20
|
+
"\\s+",
|
|
21
|
+
"(0[xX][0-9a-fA-F]+|0[bB][01]+|[0-9][0-9a-fA-F]*[hH](?![0-9A-Za-z_])|[01]+[bB](?![0-9A-Za-z_])"
|
|
22
|
+
+ "|(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?)",
|
|
23
|
+
"([A-Za-z_][A-Za-z0-9_]*)",
|
|
24
|
+
"(\\*\\*|[-+*/()])",
|
|
25
|
+
].join("|"), "y");
|
|
26
|
+
/**
|
|
27
|
+
* Splits an expression into number, name and operator tokens.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} text - The expression.
|
|
30
|
+
* @returns {string[]} Tokens in order.
|
|
31
|
+
* @throws {AstError} On any character outside the grammar.
|
|
32
|
+
*/
|
|
33
|
+
function tokenize(text) {
|
|
34
|
+
const tokens = [];
|
|
35
|
+
TOKEN.lastIndex = 0;
|
|
36
|
+
while (TOKEN.lastIndex < text.length) {
|
|
37
|
+
const at = TOKEN.lastIndex;
|
|
38
|
+
const m = TOKEN.exec(text);
|
|
39
|
+
if (!m) {
|
|
40
|
+
throw new AstError(`unexpected '${text.slice(at, at + 8)}' at ${at}`);
|
|
41
|
+
}
|
|
42
|
+
const token = m[1] ?? m[2] ?? m[3];
|
|
43
|
+
if (token) {
|
|
44
|
+
tokens.push(token);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return tokens;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Parses an exact-arithmetic expression (spec §3.4): literals, names,
|
|
51
|
+
* unary +/-, + - * / and right-associative **, with parentheses.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} text - The expression, e.g. `180/pi`.
|
|
54
|
+
* @param {number} maxNodes - `bounds.max_ast_nodes`.
|
|
55
|
+
* @param {ReadonlySet<string>} names - Names the expression may use.
|
|
56
|
+
* @returns {Node} The tree.
|
|
57
|
+
* @throws {AstError} On a syntax error or unknown name.
|
|
58
|
+
* @throws {BoundError} When the tree would exceed maxNodes.
|
|
59
|
+
*/
|
|
60
|
+
export function parseExpression(text, maxNodes, names) {
|
|
61
|
+
const tokens = tokenize(text);
|
|
62
|
+
let pos = 0;
|
|
63
|
+
let count = 0;
|
|
64
|
+
const node = (n) => {
|
|
65
|
+
if (++count > maxNodes) {
|
|
66
|
+
throw new BoundError("max_ast_nodes", maxNodes);
|
|
67
|
+
}
|
|
68
|
+
return n;
|
|
69
|
+
};
|
|
70
|
+
const peek = () => tokens[pos];
|
|
71
|
+
const binary = (next, ops) => {
|
|
72
|
+
let a = next();
|
|
73
|
+
while (ops.includes(peek() ?? "")) {
|
|
74
|
+
const op = tokens[pos++];
|
|
75
|
+
a = node({ k: "bin", op, a, b: next() });
|
|
76
|
+
}
|
|
77
|
+
return a;
|
|
78
|
+
};
|
|
79
|
+
const expr = () => binary(term, ["+", "-"]);
|
|
80
|
+
const term = () => binary(unary, ["*", "/"]);
|
|
81
|
+
const unary = () => {
|
|
82
|
+
if (peek() === "-") {
|
|
83
|
+
pos++;
|
|
84
|
+
return node({ k: "neg", a: unary() });
|
|
85
|
+
}
|
|
86
|
+
if (peek() === "+") {
|
|
87
|
+
pos++;
|
|
88
|
+
return unary();
|
|
89
|
+
}
|
|
90
|
+
return power();
|
|
91
|
+
};
|
|
92
|
+
const power = () => {
|
|
93
|
+
const a = atom();
|
|
94
|
+
if (peek() !== "**") {
|
|
95
|
+
return a;
|
|
96
|
+
}
|
|
97
|
+
pos++;
|
|
98
|
+
return node({ k: "bin", op: "**", a, b: unary() });
|
|
99
|
+
};
|
|
100
|
+
const atom = () => {
|
|
101
|
+
const t = tokens[pos++];
|
|
102
|
+
if (t === undefined) {
|
|
103
|
+
throw new AstError("unexpected end of expression");
|
|
104
|
+
}
|
|
105
|
+
if (t === "(") {
|
|
106
|
+
const inner = expr();
|
|
107
|
+
if (tokens[pos++] !== ")") {
|
|
108
|
+
throw new AstError("missing ')'");
|
|
109
|
+
}
|
|
110
|
+
return inner;
|
|
111
|
+
}
|
|
112
|
+
if (/^[A-Za-z_]/.test(t)) {
|
|
113
|
+
if (!names.has(t)) {
|
|
114
|
+
throw new AstError(`unknown name '${t}'`);
|
|
115
|
+
}
|
|
116
|
+
return node({ k: "name", name: t });
|
|
117
|
+
}
|
|
118
|
+
if (/^[0-9.]/.test(t)) {
|
|
119
|
+
let v;
|
|
120
|
+
try {
|
|
121
|
+
v = Fraction.parse(t);
|
|
122
|
+
}
|
|
123
|
+
catch (e) {
|
|
124
|
+
throw new AstError(e.message);
|
|
125
|
+
}
|
|
126
|
+
return node({ k: "num", v });
|
|
127
|
+
}
|
|
128
|
+
throw new AstError(`unexpected '${t}'`);
|
|
129
|
+
};
|
|
130
|
+
const tree = expr();
|
|
131
|
+
if (pos !== tokens.length) {
|
|
132
|
+
throw new AstError(`unexpected '${tokens[pos]}'`);
|
|
133
|
+
}
|
|
134
|
+
return tree;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Evaluates a tree exactly. A name bound to null (an irrational constant
|
|
138
|
+
* such as `pi`) makes the result null; division by zero is still caught
|
|
139
|
+
* wherever both sides are exact.
|
|
140
|
+
*
|
|
141
|
+
* @param {Node} n - The tree.
|
|
142
|
+
* @param {Readonly<Record<string, Fraction | null>>} env - Values of the names.
|
|
143
|
+
* @returns {Fraction | null} The exact value, or null when it involves an irrational name.
|
|
144
|
+
* @throws {AstError} On division by zero or a bad exponent.
|
|
145
|
+
*/
|
|
146
|
+
export function evaluate(n, env) {
|
|
147
|
+
try {
|
|
148
|
+
switch (n.k) {
|
|
149
|
+
case "num":
|
|
150
|
+
return n.v;
|
|
151
|
+
case "name":
|
|
152
|
+
return env[n.name] ?? null;
|
|
153
|
+
case "neg": {
|
|
154
|
+
const a = evaluate(n.a, env);
|
|
155
|
+
return a && a.neg();
|
|
156
|
+
}
|
|
157
|
+
case "bin": {
|
|
158
|
+
const a = evaluate(n.a, env);
|
|
159
|
+
const b = evaluate(n.b, env);
|
|
160
|
+
if (n.op === "/" && b?.isZero()) {
|
|
161
|
+
throw new AstError("division by zero");
|
|
162
|
+
}
|
|
163
|
+
if (n.op === "**" && b === null) {
|
|
164
|
+
throw new AstError("exponent must be exact");
|
|
165
|
+
}
|
|
166
|
+
if (!a || !b) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return n.op === "+" ? a.add(b) : n.op === "-" ? a.sub(b) : n.op === "*" ? a.mul(b)
|
|
170
|
+
: n.op === "/" ? a.div(b) : a.pow(b);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
catch (e) {
|
|
175
|
+
throw e instanceof ArithmeticError ? new AstError(e.message) : e;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { contractCheck } from "./contract_check.js";
|
|
2
|
+
import { describeContract } from "./describe.js";
|
|
3
|
+
import { formatContractCheck, formatTableCheck } from "./format.js";
|
|
4
|
+
import { tableCheck } from "./table_check.js";
|
|
5
|
+
const EXIT_CODES = { VALID: 0, OK: 0, PASS: 0, INVALID_CONTRACT: 1, FAIL: 1 };
|
|
6
|
+
/**
|
|
7
|
+
* Prints a tool result, as compact text or (with --json) as one-line JSON,
|
|
8
|
+
* and sets the exit code from its status (0 ok, 1 invalid/fail, 2 otherwise).
|
|
9
|
+
*
|
|
10
|
+
* @param {{status: string}} result - The tool result.
|
|
11
|
+
* @param {string} text - Its compact text form.
|
|
12
|
+
* @param {boolean} [json] - Print the full JSON object instead.
|
|
13
|
+
*/
|
|
14
|
+
const report = (result, text, json) => {
|
|
15
|
+
process.stdout.write(json ? JSON.stringify(result) + "\n" : text);
|
|
16
|
+
process.exitCode = EXIT_CODES[result.status] ?? 2;
|
|
17
|
+
};
|
|
18
|
+
const contractOptions = {
|
|
19
|
+
contract: { type: "string", describe: "Stage contract JSON file" },
|
|
20
|
+
"source-kinds": { type: "string", describe: "Pinned source_kinds registry JSON file" },
|
|
21
|
+
json: { type: "boolean", describe: "Print the full spec result as one-line JSON instead of compact text" },
|
|
22
|
+
};
|
|
23
|
+
const check = {
|
|
24
|
+
command: "check",
|
|
25
|
+
describe: "Validate a table contract, or with --kit its whole cross-stage graph",
|
|
26
|
+
builder: cli => cli.options({
|
|
27
|
+
...contractOptions,
|
|
28
|
+
kit: { type: "boolean", describe: "Also validate the cross-stage reference graph" },
|
|
29
|
+
"kit-manifest": { type: "string", describe: "Kit manifest pinning every stage contract (with --kit)" },
|
|
30
|
+
}),
|
|
31
|
+
handler: args => {
|
|
32
|
+
const result = contractCheck({
|
|
33
|
+
contract_path: args.contract, source_kinds_path: args["source-kinds"], kit: args.kit,
|
|
34
|
+
kit_manifest_path: args["kit-manifest"],
|
|
35
|
+
});
|
|
36
|
+
report(result, formatContractCheck(result), args.json);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
const describe = {
|
|
40
|
+
command: "describe",
|
|
41
|
+
describe: "Print a contract as the Markdown table guide for a stage prompt",
|
|
42
|
+
builder: cli => cli.options(contractOptions),
|
|
43
|
+
handler: args => {
|
|
44
|
+
const result = describeContract({ contract_path: args.contract, source_kinds_path: args["source-kinds"] });
|
|
45
|
+
report(result, result.markdown ?? formatContractCheck(result), args.json);
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Parses `--upstream <contract_id@version>=<output path>` flags.
|
|
50
|
+
*
|
|
51
|
+
* @param {string[]} values - Raw flag values.
|
|
52
|
+
* @returns {Record<string, {output_path: string}>} pin -> output.
|
|
53
|
+
* @throws {Error} When a value has no `=`.
|
|
54
|
+
*/
|
|
55
|
+
function parseUpstreams(values) {
|
|
56
|
+
return Object.fromEntries(values.map(v => {
|
|
57
|
+
const at = v.indexOf("=");
|
|
58
|
+
if (at < 1)
|
|
59
|
+
throw new Error(`--upstream expects <contract_id@version>=<path>, got '${v}'`);
|
|
60
|
+
return [v.slice(0, at), { output_path: v.slice(at + 1) }];
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
const tableCheckCommand = {
|
|
64
|
+
command: "check",
|
|
65
|
+
describe: "Check a stage output's tables against its contract",
|
|
66
|
+
builder: cli => cli.options({
|
|
67
|
+
...contractOptions,
|
|
68
|
+
output: { type: "string", describe: "Stage output: a Markdown file (or a folder of <Table>.csv files)" },
|
|
69
|
+
"contract-id": { type: "string", describe: "Expected <contract_id>@<version> of the contract file" },
|
|
70
|
+
upstream: {
|
|
71
|
+
type: "string", array: true, coerce: parseUpstreams,
|
|
72
|
+
describe: "Upstream stage output, as <contract_id>@<version>=<path> (repeat per upstream)",
|
|
73
|
+
},
|
|
74
|
+
}),
|
|
75
|
+
handler: args => {
|
|
76
|
+
const result = tableCheck({
|
|
77
|
+
contract_path: args.contract, source_kinds_path: args["source-kinds"], output_path: args.output,
|
|
78
|
+
contract_id: args["contract-id"], upstream_outputs: args.upstream,
|
|
79
|
+
});
|
|
80
|
+
report(result, formatTableCheck(result), args.json);
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
const contract = {
|
|
84
|
+
command: "contract",
|
|
85
|
+
describe: "Table contract tools (local)",
|
|
86
|
+
builder: cli => cli.command(check).command(describe).demandCommand(1),
|
|
87
|
+
handler: () => { },
|
|
88
|
+
};
|
|
89
|
+
const table = {
|
|
90
|
+
command: "table",
|
|
91
|
+
describe: "Stage table tools (local)",
|
|
92
|
+
builder: cli => cli.command(tableCheckCommand).demandCommand(1),
|
|
93
|
+
handler: () => { },
|
|
94
|
+
};
|
|
95
|
+
export default [contract, table];
|