omgbase 0.4.0 → 0.4.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/dist/src/cmd/_mutate.d.ts +12 -0
- package/dist/src/cmd/_mutate.js +26 -13
- package/dist/src/cmd/admin.js +25 -22
- package/dist/src/cmd/bootstrap.js +14 -22
- package/dist/src/cmd/cat.js +10 -12
- package/dist/src/cmd/diff.js +39 -27
- package/dist/src/cmd/docs.js +44 -6
- package/dist/src/cmd/find.js +3 -15
- package/dist/src/cmd/hist.js +4 -10
- package/dist/src/cmd/links.js +21 -16
- package/dist/src/cmd/log.js +8 -18
- package/dist/src/cmd/ls.js +3 -15
- package/dist/src/cmd/mcp.js +13 -32
- package/dist/src/cmd/mutate.js +24 -3
- package/dist/src/cmd/node.js +7 -3
- package/dist/src/cmd/oqx.d.ts +10 -0
- package/dist/src/cmd/oqx.js +25 -28
- package/dist/src/cmd/outline.js +5 -8
- package/dist/src/cmd/retarget.js +6 -21
- package/dist/src/cmd/run.js +5 -32
- package/dist/src/cmd/show.js +11 -12
- package/dist/src/cmd/source.js +18 -32
- package/dist/src/cmd/status.js +13 -25
- package/dist/src/cmd/sync.js +8 -9
- package/dist/src/cmd/update.js +10 -5
- package/dist/src/context.d.ts +4 -0
- package/dist/src/context.js +14 -0
- package/dist/src/dispatch.d.ts +3 -0
- package/dist/src/dispatch.js +35 -18
- package/dist/src/main.d.ts +7 -0
- package/dist/src/main.js +46 -12
- package/dist/src/output.d.ts +28 -1
- package/dist/src/output.js +66 -20
- package/dist/src/seams.d.ts +13 -0
- package/dist/src/seams.js +37 -0
- package/dist/src/shell/session.js +4 -2
- package/package.json +3 -3
package/dist/src/cmd/status.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { parseArgs } from "node:util";
|
|
2
|
-
import { reposStatus, syncStatus, watchLeaseLive } from "@omgbase/core";
|
|
3
|
-
import { EXIT_OK, renderHelp } from "../output.js";
|
|
2
|
+
import { reposStatus, syncStatus, watchLeaseLive, staleEmbedCount, resolveSettings, embeddingSettings } from "@omgbase/core";
|
|
3
|
+
import { EXIT_OK, emitMachine, renderHelp } from "../output.js";
|
|
4
4
|
// `omg status` (11 §5.2): repos_status + sync_status + watch-lease probe +
|
|
5
|
-
// embedding queue depth. The "where am I" command — the visual showcase.
|
|
5
|
+
// embedding queue depth. The "where am I" command — the visual showcase. The
|
|
6
|
+
// machine shape is the two tools' results, nested as they are on the wire
|
|
7
|
+
// (spec/cli §6): `{ ...repos_status, sync: sync_status, watcher, embedQueue }`.
|
|
6
8
|
function runStatus(cli, args) {
|
|
7
9
|
const { values } = parseArgs({ args, allowPositionals: true, options: { help: { type: "boolean" } } });
|
|
8
10
|
if (values.help) {
|
|
@@ -10,7 +12,7 @@ function runStatus(cli, args) {
|
|
|
10
12
|
name: "status",
|
|
11
13
|
summary: "Where am I: the active repo, doc/block/commit counts, sync convergence, watcher, embed queue",
|
|
12
14
|
usage: "status [--repo <slug>] [--json]",
|
|
13
|
-
options: [["--json", "
|
|
15
|
+
options: [["--json", "the repos_status result, plus `sync` (the sync_status result), `watcher` (live|none) and `embedQueue` (stale embeddable blocks)"]],
|
|
14
16
|
});
|
|
15
17
|
}
|
|
16
18
|
const ws = cli.workspace();
|
|
@@ -18,27 +20,13 @@ function runStatus(cli, args) {
|
|
|
18
20
|
const rs = reposStatus(ws.store, repo.repoId, repo.rootPath ?? undefined);
|
|
19
21
|
const ss = syncStatus(ws.store, repo.repoId, repo.rootPath ?? undefined);
|
|
20
22
|
const watcher = watchLeaseLive(ws.omgbaseDir);
|
|
21
|
-
// Embedding queue depth:
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
blocks: rs.blocks,
|
|
29
|
-
commits: rs.commits,
|
|
30
|
-
openEdges: rs.openEdges,
|
|
31
|
-
unconverged: rs.unconverged,
|
|
32
|
-
convergent: ss.convergent,
|
|
33
|
-
disk: ss.disk,
|
|
34
|
-
lastCommitSeq: ss.lastCommitSeq,
|
|
35
|
-
watcher: watcher ? "live" : "none",
|
|
36
|
-
embedQueue: queued,
|
|
37
|
-
};
|
|
38
|
-
if (cli.flags.mode !== "human") {
|
|
39
|
-
cli.io.out(JSON.stringify(payload));
|
|
40
|
-
return EXIT_OK;
|
|
41
|
-
}
|
|
23
|
+
// Embedding queue depth (spec/search §2.3): the embeddable blocks whose
|
|
24
|
+
// current context has no cached vector — for the configured model when one is
|
|
25
|
+
// named, else for any. Counted from the cache alone: no provider is spawned.
|
|
26
|
+
const queued = staleEmbedCount(ws.store, repo.repoId, embeddingSettings(resolveSettings(ws.store, repo.repoId)).model);
|
|
27
|
+
const payload = { ...rs, sync: ss, watcher: watcher ? "live" : "none", embedQueue: queued };
|
|
28
|
+
if (cli.flags.mode !== "human")
|
|
29
|
+
return emitMachine(cli, payload);
|
|
42
30
|
const { render, style, io } = cli;
|
|
43
31
|
const g = render.g;
|
|
44
32
|
io.out(render.wordmark(repo.slug));
|
package/dist/src/cmd/sync.js
CHANGED
|
@@ -2,7 +2,7 @@ import { resolve } from "node:path";
|
|
|
2
2
|
import { parseArgs } from "node:util";
|
|
3
3
|
import { Watcher, WatchLease, EmbedDrainer, freshnessSweep, awaitReady, WATCH_READY_PATIENCE_MS, EngineError } from "@omgbase/core";
|
|
4
4
|
import { runFsMirror } from "@omgbase/sync";
|
|
5
|
-
import { EngineErrorLike, EXIT_OK, renderHelp } from "../output.js";
|
|
5
|
+
import { EngineErrorLike, EXIT_OK, emitMachine, renderHelp } from "../output.js";
|
|
6
6
|
import { loadEmbedding } from "./_embed.js";
|
|
7
7
|
import { remoteSpec } from "./_remote.js";
|
|
8
8
|
import { openRepoSource } from "./_source.js";
|
|
@@ -62,17 +62,16 @@ async function runSync(cli, args) {
|
|
|
62
62
|
}
|
|
63
63
|
function runOneShot(cli, ws, repo) {
|
|
64
64
|
if (!repo.rootPath) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
// Nothing to sweep: an empty-result note (stderr, §3.1), or the empty sweep shape.
|
|
66
|
+
if (cli.flags.mode !== "human")
|
|
67
|
+
return emitMachine(cli, { scanned: 0, ingested: [], deleted: [], conflicted: [], changed: false });
|
|
68
|
+
cli.io.err(cli.style.dim(` ${repo.slug} has no filesystem source — nothing to sync`));
|
|
69
69
|
return EXIT_OK;
|
|
70
70
|
}
|
|
71
71
|
const result = freshnessSweep(ws.store, repo.repoId, repo.rootPath);
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return
|
|
75
|
-
}
|
|
72
|
+
// A `SweepResult` is a record (its lists are per-kind): every machine mode is the document.
|
|
73
|
+
if (cli.flags.mode !== "human")
|
|
74
|
+
return emitMachine(cli, result);
|
|
76
75
|
const { render, style, io } = cli;
|
|
77
76
|
const g = render.g;
|
|
78
77
|
io.out(render.wordmark("sync"));
|
package/dist/src/cmd/update.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parseArgs } from "node:util";
|
|
2
|
-
import { docsUpdate, renderOpsetPlan
|
|
2
|
+
import { docsUpdate, renderOpsetPlan } from "@omgbase/core";
|
|
3
3
|
import { CliUsageError, EXIT_OK, renderHelp } from "../output.js";
|
|
4
4
|
import { readContent, extractContentOpts } from "./_mutate.js";
|
|
5
5
|
import { cmdUpdate as cmdUpdateBlock } from "./mutate.js";
|
|
@@ -13,11 +13,13 @@ import { remoteCall } from "./_remote.js";
|
|
|
13
13
|
// --dry-run (or --plan) prints the plan (identity effects + summary), commits
|
|
14
14
|
// nothing.
|
|
15
15
|
function runUpdate(cli, args) {
|
|
16
|
-
// Peek at the target to dispatch: a b_
|
|
17
|
-
//
|
|
16
|
+
// Peek at the target to dispatch: a `b_` token is a block replace — a token
|
|
17
|
+
// that names no live block fails there as block_missing (spec/cli §6), never
|
|
18
|
+
// as a document path; anything else (d_ id or path) is a whole-document
|
|
19
|
+
// update. The block path re-parses args.
|
|
18
20
|
const peek = extractContentOpts(args);
|
|
19
21
|
const target = peek.rest.find((a) => !a.startsWith("-"));
|
|
20
|
-
if (target &&
|
|
22
|
+
if (target && target.startsWith("b_"))
|
|
21
23
|
return cmdUpdateBlock.run(cli, args);
|
|
22
24
|
return runDocUpdate(cli, args);
|
|
23
25
|
}
|
|
@@ -92,7 +94,10 @@ async function runDocUpdate(cli, args) {
|
|
|
92
94
|
}
|
|
93
95
|
const g = cli.render.g;
|
|
94
96
|
cli.io.err(cli.style.dim(` ${cli.style.ok(g.ok)} updated ${cli.style.accent(opset.target.path)} · ${opset.summary.preserved} preserved, ${opset.summary.updated} updated, ${opset.summary.moved} moved, ${opset.summary.created} created, ${opset.summary.removed} removed`));
|
|
95
|
-
|
|
97
|
+
// Every id the opset touched, once each: a block appears in several ops'
|
|
98
|
+
// results (an update then a retile of the same block), the confirmation
|
|
99
|
+
// lists it a single time.
|
|
100
|
+
for (const id of new Set(result.results.flatMap((r) => r.ids)))
|
|
96
101
|
cli.io.out(id);
|
|
97
102
|
return EXIT_OK;
|
|
98
103
|
}
|
package/dist/src/context.d.ts
CHANGED
|
@@ -68,6 +68,10 @@ export interface CliOptions {
|
|
|
68
68
|
export declare function progName(argv1: string | undefined): string;
|
|
69
69
|
export declare function makeCli(flags: GlobalFlags, io: IO, opts?: CliOptions): Cli;
|
|
70
70
|
export declare const NO_WORKSPACE_OK: Set<string>;
|
|
71
|
+
export declare const HELP_WORD_OK: Set<string>;
|
|
72
|
+
export declare const BARE_IS_HELP: Set<string>;
|
|
73
|
+
/** Does this invocation ask for a command's help card? (the flag, the word, or a bare card-only command) */
|
|
74
|
+
export declare function asksForHelp(flags: GlobalFlags, name: string, args: string[]): boolean;
|
|
71
75
|
export declare const SKIP_FRESHNESS: Set<string>;
|
|
72
76
|
export declare const REMOTE_OK: Set<string>;
|
|
73
77
|
export { CliUsageError };
|
package/dist/src/context.js
CHANGED
|
@@ -57,6 +57,20 @@ function resolveDir(dir) {
|
|
|
57
57
|
}
|
|
58
58
|
// Which commands may run without a workspace (11 §2.1).
|
|
59
59
|
export const NO_WORKSPACE_OK = new Set(["init", "help", "version"]);
|
|
60
|
+
// Commands whose first argument may be the word `help` (their card), and those
|
|
61
|
+
// whose card is also their bare invocation (spec/cli §5): help is documentation,
|
|
62
|
+
// not work, so these route to the card before any workspace is opened or swept —
|
|
63
|
+
// exactly like the `--help` flag.
|
|
64
|
+
export const HELP_WORD_OK = new Set(["config", "embed", "node", "source"]);
|
|
65
|
+
export const BARE_IS_HELP = new Set(["node", "source"]);
|
|
66
|
+
/** Does this invocation ask for a command's help card? (the flag, the word, or a bare card-only command) */
|
|
67
|
+
export function asksForHelp(flags, name, args) {
|
|
68
|
+
if (flags.help)
|
|
69
|
+
return true;
|
|
70
|
+
if (HELP_WORD_OK.has(name) && args[0] === "help")
|
|
71
|
+
return true;
|
|
72
|
+
return BARE_IS_HELP.has(name) && args.length === 0;
|
|
73
|
+
}
|
|
60
74
|
// Commands that manage sync themselves — skip the freshness sweep (11 §3.3).
|
|
61
75
|
// `shell` is exempt because each line it runs sweeps on its own. `source`
|
|
62
76
|
// creates/binds sources and runs its own initial sync, so it skips the pre-sweep.
|
package/dist/src/dispatch.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { type Cli, type GlobalFlags } from "./context.js";
|
|
2
|
+
import { CliUsageError } from "./output.js";
|
|
2
3
|
export interface Parsed {
|
|
3
4
|
flags: GlobalFlags;
|
|
4
5
|
command: string | null;
|
|
5
6
|
rest: string[];
|
|
7
|
+
/** the first malformed global flag, when any (exit 2) */
|
|
8
|
+
error?: CliUsageError;
|
|
6
9
|
}
|
|
7
10
|
export declare function parseGlobals(argv: string[]): Parsed;
|
|
8
11
|
export declare function runCommand(cli: Cli, command: string, rest: string[]): Promise<number>;
|
package/dist/src/dispatch.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { freshnessSweep, watchLeaseLive } from "@omgbase/core";
|
|
2
|
-
import { NO_WORKSPACE_OK, SKIP_FRESHNESS, REMOTE_OK } from "./context.js";
|
|
3
|
-
import { CliUsageError, EngineErrorLike, renderError } from "./output.js";
|
|
2
|
+
import { NO_WORKSPACE_OK, SKIP_FRESHNESS, REMOTE_OK, asksForHelp } from "./context.js";
|
|
3
|
+
import { CliUsageError, EngineErrorLike, renderError, usageFromParseArgs } from "./output.js";
|
|
4
4
|
import { resolveCommand, unknownCommandError } from "./commands.js";
|
|
5
5
|
export function parseGlobals(argv) {
|
|
6
6
|
const flags = {
|
|
@@ -14,6 +14,12 @@ export function parseGlobals(argv) {
|
|
|
14
14
|
let command = null;
|
|
15
15
|
const rest = [];
|
|
16
16
|
let passthrough = false;
|
|
17
|
+
let error;
|
|
18
|
+
// Record the first malformed flag and keep parsing (a later `--json` still
|
|
19
|
+
// selects the mode the error is rendered in).
|
|
20
|
+
const fail = (message) => {
|
|
21
|
+
error ??= new CliUsageError(message);
|
|
22
|
+
};
|
|
17
23
|
for (let i = 0; i < argv.length; i++) {
|
|
18
24
|
const a = argv[i];
|
|
19
25
|
if (passthrough) {
|
|
@@ -31,30 +37,35 @@ export function parseGlobals(argv) {
|
|
|
31
37
|
case "--directory": {
|
|
32
38
|
const dir = argv[++i];
|
|
33
39
|
if (dir === undefined)
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
fail(`${a} requires a directory`);
|
|
41
|
+
else
|
|
42
|
+
flags.directory = dir;
|
|
36
43
|
continue;
|
|
37
44
|
}
|
|
38
45
|
case "--repo": {
|
|
39
46
|
const slug = argv[++i];
|
|
40
47
|
if (slug === undefined)
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
fail("--repo requires a slug");
|
|
49
|
+
else
|
|
50
|
+
flags.repo = slug;
|
|
43
51
|
continue;
|
|
44
52
|
}
|
|
45
53
|
case "--server": {
|
|
54
|
+
// An empty value is a mistake, not "local": the flag was given.
|
|
46
55
|
const s = argv[++i];
|
|
47
|
-
if (s === undefined)
|
|
48
|
-
|
|
49
|
-
|
|
56
|
+
if (s === undefined || s === "")
|
|
57
|
+
fail("--server requires a command or url");
|
|
58
|
+
else
|
|
59
|
+
flags.server = s;
|
|
50
60
|
continue;
|
|
51
61
|
}
|
|
52
62
|
case "-H":
|
|
53
63
|
case "--header": {
|
|
54
64
|
const h = argv[++i];
|
|
55
65
|
if (h === undefined)
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
fail(`${a} requires a "Name: value" header`);
|
|
67
|
+
else
|
|
68
|
+
(flags.headers ??= []).push(h);
|
|
58
69
|
continue;
|
|
59
70
|
}
|
|
60
71
|
case "--json":
|
|
@@ -90,12 +101,14 @@ export function parseGlobals(argv) {
|
|
|
90
101
|
}
|
|
91
102
|
// Once the command is known, anything unrecognized is its residual;
|
|
92
103
|
// a stray flag before any command is a usage error.
|
|
93
|
-
if (!command)
|
|
94
|
-
|
|
104
|
+
if (!command) {
|
|
105
|
+
fail(`unknown flag ${a}`);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
95
108
|
rest.push(a);
|
|
96
109
|
}
|
|
97
110
|
}
|
|
98
|
-
return { flags, command, rest };
|
|
111
|
+
return { flags, command, rest, ...(error ? { error } : {}) };
|
|
99
112
|
}
|
|
100
113
|
// Per-command execution shared by the one-shot entry (main.ts) and the
|
|
101
114
|
// interactive shell (cmd/shell.ts). Given a resolved Cli, a command name, and
|
|
@@ -110,18 +123,20 @@ export async function runCommand(cli, command, rest) {
|
|
|
110
123
|
// `--help` after a command → route to help for that command. Help is
|
|
111
124
|
// documentation, not work: it must never need a workspace, so it also skips
|
|
112
125
|
// the freshness sweep below (which would otherwise fail with repo_not_found
|
|
113
|
-
// from any directory without a `.omgbase/`).
|
|
126
|
+
// from any directory without a `.omgbase/`). The help *words* (`config help`,
|
|
127
|
+
// a bare `node`, …) are the same request spelled differently (spec/cli §5).
|
|
114
128
|
const args = cli.flags.help ? ["--help", ...rest] : rest;
|
|
129
|
+
const help = asksForHelp(cli.flags, resolved.name, rest);
|
|
115
130
|
// Global `--server` (remote/MCP mode) is only implemented by some commands so
|
|
116
131
|
// far (REMOTE_OK); reject it elsewhere rather than silently running locally.
|
|
117
|
-
if (cli.flags.server !== undefined && !
|
|
132
|
+
if (cli.flags.server !== undefined && !help && !REMOTE_OK.has(resolved.name)) {
|
|
118
133
|
return renderError(new CliUsageError(`--server is not supported for '${resolved.name}' — it needs local ref resolution or a working tree; run it against a local workspace`), cli.io, cli.style, cli.flags.mode !== "human");
|
|
119
134
|
}
|
|
120
135
|
try {
|
|
121
136
|
// Freshness sweep (11 §3.3): current-by-default, unless --stale, a live
|
|
122
137
|
// watcher holds the lease, remote (`--server`) mode (no local tree to sweep),
|
|
123
138
|
// or the command manages sync itself.
|
|
124
|
-
if (!
|
|
139
|
+
if (!help && !cli.flags.stale && cli.flags.server === undefined && !SKIP_FRESHNESS.has(resolved.name) && !NO_WORKSPACE_OK.has(resolved.name)) {
|
|
125
140
|
const ws = cli.workspace();
|
|
126
141
|
if (!watchLeaseLive(ws.omgbaseDir)) {
|
|
127
142
|
// Best-effort: a workspace with no attached repo (or an unresolvable
|
|
@@ -142,7 +157,9 @@ export async function runCommand(cli, command, rest) {
|
|
|
142
157
|
return await resolved.run(cli, args);
|
|
143
158
|
}
|
|
144
159
|
catch (err) {
|
|
145
|
-
|
|
160
|
+
// A command's own `parseArgs` rejecting an option is a usage error (exit 2),
|
|
161
|
+
// rendered here once for every verb rather than in each parser.
|
|
162
|
+
return renderError(usageFromParseArgs(err, cli.prog, resolved.name), cli.io, cli.style, cli.flags.mode !== "human");
|
|
146
163
|
}
|
|
147
164
|
}
|
|
148
165
|
//# sourceMappingURL=dispatch.js.map
|
package/dist/src/main.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { type IO } from "./render.js";
|
|
3
|
+
/**
|
|
4
|
+
* `--version` prints THIS package's version (spec/cli §2.5), read from the
|
|
5
|
+
* nearest `package.json` named `omgbase` above the running module (the source
|
|
6
|
+
* tree and the built `dist/src/` sit at different depths). The engine's own
|
|
7
|
+
* `VERSION` is the fallback only if the package file cannot be found.
|
|
8
|
+
*/
|
|
9
|
+
export declare function cliVersion(): string;
|
|
3
10
|
export declare function run(argv: string[], io?: IO, prog?: string): Promise<number>;
|
package/dist/src/main.js
CHANGED
|
@@ -1,30 +1,52 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
2
5
|
import { VERSION } from "@omgbase/core";
|
|
3
6
|
import { processIO } from "./render.js";
|
|
4
7
|
import { makeCli, progName } from "./context.js";
|
|
5
|
-
import { renderError, EXIT_OK
|
|
8
|
+
import { renderError, EXIT_OK } from "./output.js";
|
|
6
9
|
import { resolveCommand, unknownCommandError } from "./commands.js";
|
|
7
10
|
import { runCommand, parseGlobals } from "./dispatch.js";
|
|
8
11
|
import { closeRemote } from "./cmd/_remote.js";
|
|
12
|
+
import { installSpecSeams } from "./seams.js";
|
|
9
13
|
// Hand-rolled router (11 §8: no commander). Splits global flags from the
|
|
10
14
|
// command + its residual argv (parseGlobals, in dispatch.ts), resolves the
|
|
11
15
|
// command (with aliases), then delegates to runCommand for the freshness sweep +
|
|
12
16
|
// dispatch + error mapping — the same path the shell reuses per line.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
/**
|
|
18
|
+
* `--version` prints THIS package's version (spec/cli §2.5), read from the
|
|
19
|
+
* nearest `package.json` named `omgbase` above the running module (the source
|
|
20
|
+
* tree and the built `dist/src/` sit at different depths). The engine's own
|
|
21
|
+
* `VERSION` is the fallback only if the package file cannot be found.
|
|
22
|
+
*/
|
|
23
|
+
export function cliVersion() {
|
|
24
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
25
|
+
for (;;) {
|
|
26
|
+
const candidate = join(dir, "package.json");
|
|
27
|
+
if (existsSync(candidate)) {
|
|
28
|
+
try {
|
|
29
|
+
const pkg = JSON.parse(readFileSync(candidate, "utf8"));
|
|
30
|
+
if (pkg.name === "omgbase" && typeof pkg.version === "string")
|
|
31
|
+
return pkg.version;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
/* keep walking */
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const parent = dirname(dir);
|
|
38
|
+
if (parent === dir)
|
|
39
|
+
return VERSION;
|
|
40
|
+
dir = parent;
|
|
22
41
|
}
|
|
42
|
+
}
|
|
43
|
+
export async function run(argv, io = processIO, prog = "omg") {
|
|
44
|
+
const parsed = parseGlobals(argv);
|
|
23
45
|
const { flags } = parsed;
|
|
24
46
|
let command = parsed.command;
|
|
25
47
|
// --version / --help without a command.
|
|
26
|
-
if (flags.version && !command) {
|
|
27
|
-
io.out(
|
|
48
|
+
if (flags.version && !command && !parsed.error) {
|
|
49
|
+
io.out(cliVersion());
|
|
28
50
|
return EXIT_OK;
|
|
29
51
|
}
|
|
30
52
|
if (flags.help && !command)
|
|
@@ -32,6 +54,18 @@ export async function run(argv, io = processIO, prog = "omg") {
|
|
|
32
54
|
if (!command)
|
|
33
55
|
command = "help";
|
|
34
56
|
const cli = makeCli(flags, io, { prog });
|
|
57
|
+
// A malformed global flag is a usage error like any other (spec/cli §2.2):
|
|
58
|
+
// rendered with the `usage:` prefix, in the mode the argv selected.
|
|
59
|
+
if (parsed.error)
|
|
60
|
+
return renderError(parsed.error, io, cli.style, flags.mode !== "human");
|
|
61
|
+
// The conformance seams (spec/cli §2.6) apply to every verb: install them
|
|
62
|
+
// before any command can open the workspace, mint an id or read the clock.
|
|
63
|
+
try {
|
|
64
|
+
installSpecSeams();
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
return renderError(err, io, cli.style, flags.mode !== "human");
|
|
68
|
+
}
|
|
35
69
|
if (!resolveCommand(command)) {
|
|
36
70
|
return renderError(unknownCommandError(prog, command), io, cli.style, flags.mode !== "human");
|
|
37
71
|
}
|
package/dist/src/output.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type IO } from "./render.js";
|
|
2
2
|
import type { Style } from "./style.js";
|
|
3
|
+
import type { OutputMode } from "./context.js";
|
|
3
4
|
export declare const EXIT_OK = 0;
|
|
4
5
|
export declare const EXIT_ERROR = 1;
|
|
5
6
|
export declare const EXIT_USAGE = 2;
|
|
@@ -9,6 +10,14 @@ export declare class CliUsageError extends Error {
|
|
|
9
10
|
readonly hint?: string | undefined;
|
|
10
11
|
constructor(message: string, hint?: string | undefined);
|
|
11
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Node's `parseArgs` failures are the user's mistake, not the engine's (spec/cli
|
|
15
|
+
* §2.2): an option the command does not declare, a value-taking option without
|
|
16
|
+
* its value, a boolean given one. Each becomes a usage error with a portable
|
|
17
|
+
* message — the wording is ours, so a port need not reproduce Node's — and the
|
|
18
|
+
* hint points at the command's card. Anything else is returned untouched.
|
|
19
|
+
*/
|
|
20
|
+
export declare function usageFromParseArgs(err: unknown, prog: string, command: string): unknown;
|
|
12
21
|
export interface HelpSpec {
|
|
13
22
|
name: string;
|
|
14
23
|
summary: string;
|
|
@@ -52,9 +61,27 @@ export declare class EngineErrorLike extends Error {
|
|
|
52
61
|
/**
|
|
53
62
|
* Render a thrown error to stderr and return the process exit code. `json`
|
|
54
63
|
* selects machine output (typed object); otherwise the human form with any
|
|
55
|
-
* current-truth payload pretty-printed.
|
|
64
|
+
* current-truth payload pretty-printed. Both forms carry the hint; an empty
|
|
65
|
+
* payload (`{}`) prints nothing.
|
|
56
66
|
*/
|
|
57
67
|
export declare function renderError(err: unknown, io: IO, style: Style, json: boolean): number;
|
|
68
|
+
export interface MachineShape {
|
|
69
|
+
/** the list `--jsonl` streams (absent: the result is not list-shaped) */
|
|
70
|
+
items?: unknown[];
|
|
71
|
+
/** the ids `--ids` prints (absent: the result carries no id list) */
|
|
72
|
+
ids?: string[];
|
|
73
|
+
/** the cursor of a truncated result, for the footer */
|
|
74
|
+
cursor?: string | number | null;
|
|
75
|
+
}
|
|
76
|
+
interface MachineIO {
|
|
77
|
+
io: IO;
|
|
78
|
+
style: Style;
|
|
79
|
+
flags: {
|
|
80
|
+
mode: OutputMode;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** Print `doc` in the current machine mode (`--json`/`--jsonl`/`--ids`); returns EXIT_OK. */
|
|
84
|
+
export declare function emitMachine(cli: MachineIO, doc: unknown, shape?: MachineShape): number;
|
|
58
85
|
export interface HitLike {
|
|
59
86
|
id: string;
|
|
60
87
|
path: string;
|
package/dist/src/output.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { errorBody } from "@omgbase/core";
|
|
1
2
|
import { columns, visibleWidth } from "./render.js";
|
|
2
3
|
// Output contract + error rendering (11 §2.4, §4).
|
|
3
4
|
//
|
|
@@ -20,6 +21,34 @@ export class CliUsageError extends Error {
|
|
|
20
21
|
this.name = "CliUsageError";
|
|
21
22
|
}
|
|
22
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Node's `parseArgs` failures are the user's mistake, not the engine's (spec/cli
|
|
26
|
+
* §2.2): an option the command does not declare, a value-taking option without
|
|
27
|
+
* its value, a boolean given one. Each becomes a usage error with a portable
|
|
28
|
+
* message — the wording is ours, so a port need not reproduce Node's — and the
|
|
29
|
+
* hint points at the command's card. Anything else is returned untouched.
|
|
30
|
+
*/
|
|
31
|
+
export function usageFromParseArgs(err, prog, command) {
|
|
32
|
+
const e = err;
|
|
33
|
+
if (!e || typeof e !== "object" || typeof e.code !== "string" || !e.code.startsWith("ERR_PARSE_ARGS_") || typeof e.message !== "string")
|
|
34
|
+
return err;
|
|
35
|
+
const hint = `run '${prog} ${command} --help' for the options`;
|
|
36
|
+
const unknown = /^Unknown option '([^']+)'/.exec(e.message);
|
|
37
|
+
if (unknown)
|
|
38
|
+
return new CliUsageError(`unknown option '${unknown[1]}'`, hint);
|
|
39
|
+
const missing = /^Option '([^']+?)(?: <value>)?' argument missing/.exec(e.message);
|
|
40
|
+
if (missing)
|
|
41
|
+
return new CliUsageError(`option '${lastSpelling(missing[1])}' requires a value`, hint);
|
|
42
|
+
const extra = /^Option '([^']+)' does not take an argument/.exec(e.message);
|
|
43
|
+
if (extra)
|
|
44
|
+
return new CliUsageError(`option '${lastSpelling(extra[1])}' does not take a value`, hint);
|
|
45
|
+
return new CliUsageError(e.message, hint);
|
|
46
|
+
}
|
|
47
|
+
/** Node names an option with a short alias as `-n, --name`; quote the long form. */
|
|
48
|
+
function lastSpelling(spellings) {
|
|
49
|
+
const parts = spellings.split(",").map((p) => p.trim()).filter(Boolean);
|
|
50
|
+
return parts[parts.length - 1] ?? spellings;
|
|
51
|
+
}
|
|
23
52
|
export function renderHelp(cli, spec) {
|
|
24
53
|
const { io, style, prog } = cli;
|
|
25
54
|
io.out(` ${style.bold(spec.name)} — ${spec.summary}`);
|
|
@@ -60,7 +89,14 @@ export class EngineErrorLike extends Error {
|
|
|
60
89
|
};
|
|
61
90
|
}
|
|
62
91
|
}
|
|
63
|
-
/**
|
|
92
|
+
/**
|
|
93
|
+
* Coerce any thrown value into a typed error body for rendering. The CLI's own
|
|
94
|
+
* `EngineErrorLike` carries its code and hint; everything else goes through the
|
|
95
|
+
* engine's one mapping (`errorBody`, the same the MCP server renders — spec/cli
|
|
96
|
+
* §3.5): typed errors keep their code, OQX errors and a bad cursor are
|
|
97
|
+
* `filter_invalid`, an unknown revision `target_missing`, and anything else the
|
|
98
|
+
* catch-all `repo_not_found`.
|
|
99
|
+
*/
|
|
64
100
|
function toBody(err) {
|
|
65
101
|
if (err instanceof EngineErrorLike) {
|
|
66
102
|
const b = err.body();
|
|
@@ -71,27 +107,22 @@ function toBody(err) {
|
|
|
71
107
|
...(err.extra.hint ? { hint: err.extra.hint } : {}),
|
|
72
108
|
};
|
|
73
109
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (
|
|
82
|
-
return
|
|
83
|
-
|
|
84
|
-
message: anyErr.message ?? String(err),
|
|
85
|
-
...(anyErr.data !== undefined ? { data: anyErr.data } : {}),
|
|
86
|
-
...(anyErr.hint ? { hint: anyErr.hint } : {}),
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
return { code: "error", message: anyErr?.message ?? String(err) };
|
|
110
|
+
const b = errorBody(err);
|
|
111
|
+
return { code: b.error, message: b.message, ...(b.data !== undefined ? { data: b.data } : {}) };
|
|
112
|
+
}
|
|
113
|
+
/** A payload worth printing: anything but `undefined`, `null` and an empty record. */
|
|
114
|
+
function hasPayload(data) {
|
|
115
|
+
if (data === undefined || data === null)
|
|
116
|
+
return false;
|
|
117
|
+
if (typeof data === "object" && !Array.isArray(data) && Object.keys(data).length === 0)
|
|
118
|
+
return false;
|
|
119
|
+
return true;
|
|
90
120
|
}
|
|
91
121
|
/**
|
|
92
122
|
* Render a thrown error to stderr and return the process exit code. `json`
|
|
93
123
|
* selects machine output (typed object); otherwise the human form with any
|
|
94
|
-
* current-truth payload pretty-printed.
|
|
124
|
+
* current-truth payload pretty-printed. Both forms carry the hint; an empty
|
|
125
|
+
* payload (`{}`) prints nothing.
|
|
95
126
|
*/
|
|
96
127
|
export function renderError(err, io, style, json) {
|
|
97
128
|
if (err instanceof CliUsageError) {
|
|
@@ -106,7 +137,7 @@ export function renderError(err, io, style, json) {
|
|
|
106
137
|
}
|
|
107
138
|
const body = toBody(err);
|
|
108
139
|
if (json) {
|
|
109
|
-
io.err(JSON.stringify({ error: body.code, message: body.message, ...(body.data
|
|
140
|
+
io.err(JSON.stringify({ error: body.code, message: body.message, ...(hasPayload(body.data) ? { data: body.data } : {}), ...(body.hint ? { hint: body.hint } : {}), retriable: false }));
|
|
110
141
|
return EXIT_ERROR;
|
|
111
142
|
}
|
|
112
143
|
io.err(`${style.err(`error[${body.code}]`)}: ${body.message}`);
|
|
@@ -114,11 +145,26 @@ export function renderError(err, io, style, json) {
|
|
|
114
145
|
io.err(style.dim(` hint: ${body.hint}`));
|
|
115
146
|
// Conflict objects carry current truth (04 §4) — print all of it; the retry
|
|
116
147
|
// is built from it.
|
|
117
|
-
if (body.data
|
|
148
|
+
if (hasPayload(body.data)) {
|
|
118
149
|
io.err(style.dim(indent(JSON.stringify(body.data, null, 2), " ")));
|
|
119
150
|
}
|
|
120
151
|
return EXIT_ERROR;
|
|
121
152
|
}
|
|
153
|
+
/** Print `doc` in the current machine mode (`--json`/`--jsonl`/`--ids`); returns EXIT_OK. */
|
|
154
|
+
export function emitMachine(cli, doc, shape = {}) {
|
|
155
|
+
const { io, style, flags } = cli;
|
|
156
|
+
if (flags.mode === "jsonl" && shape.items)
|
|
157
|
+
for (const item of shape.items)
|
|
158
|
+
io.out(JSON.stringify(item));
|
|
159
|
+
else if (flags.mode === "ids" && shape.ids)
|
|
160
|
+
for (const id of shape.ids)
|
|
161
|
+
io.out(id);
|
|
162
|
+
else
|
|
163
|
+
io.out(JSON.stringify(doc));
|
|
164
|
+
if (flags.mode !== "json" && shape.cursor !== undefined && shape.cursor !== null)
|
|
165
|
+
truncationFooter(io, style, shape.cursor);
|
|
166
|
+
return EXIT_OK;
|
|
167
|
+
}
|
|
122
168
|
function indent(s, pad) {
|
|
123
169
|
return s
|
|
124
170
|
.split("\n")
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface SpecSeams {
|
|
2
|
+
/** the sequential minter is installed */
|
|
3
|
+
minter: boolean;
|
|
4
|
+
/** the pinned instant (canonical ISO form), when the clock is pinned */
|
|
5
|
+
clock: string | null;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Read + install the seams (idempotent: a second call returns what the first
|
|
9
|
+
* installed). Throws `CliUsageError` on an invalid value; the caller renders it.
|
|
10
|
+
*/
|
|
11
|
+
export declare function installSpecSeams(env?: NodeJS.ProcessEnv): SpecSeams;
|
|
12
|
+
/** The seams installed by `installSpecSeams` (none when it has not run). */
|
|
13
|
+
export declare function activeSpecSeams(): SpecSeams;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { pinClock, sequentialMinter, setIdMinter } from "@omgbase/core";
|
|
2
|
+
import { CliUsageError } from "./output.js";
|
|
3
|
+
let installed = null;
|
|
4
|
+
/**
|
|
5
|
+
* Read + install the seams (idempotent: a second call returns what the first
|
|
6
|
+
* installed). Throws `CliUsageError` on an invalid value; the caller renders it.
|
|
7
|
+
*/
|
|
8
|
+
export function installSpecSeams(env = process.env) {
|
|
9
|
+
if (installed)
|
|
10
|
+
return installed;
|
|
11
|
+
const seams = { minter: false, clock: null };
|
|
12
|
+
const minter = env.OMGBASE_SPEC_MINTER;
|
|
13
|
+
if (minter !== undefined && minter !== "") {
|
|
14
|
+
if (minter !== "sequential") {
|
|
15
|
+
throw new CliUsageError(`OMGBASE_SPEC_MINTER=${JSON.stringify(minter)}: the only value is "sequential" (spec/surface §7.1)`, "unset it, or set OMGBASE_SPEC_MINTER=sequential for a conformance run");
|
|
16
|
+
}
|
|
17
|
+
setIdMinter(sequentialMinter());
|
|
18
|
+
seams.minter = true;
|
|
19
|
+
}
|
|
20
|
+
const clock = env.OMGBASE_SPEC_CLOCK;
|
|
21
|
+
if (clock !== undefined && clock !== "") {
|
|
22
|
+
try {
|
|
23
|
+
pinClock(clock);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
throw new CliUsageError(`OMGBASE_SPEC_CLOCK=${JSON.stringify(clock)}: not an RFC 3339 instant (spec/surface §7.1)`, "e.g. OMGBASE_SPEC_CLOCK=2026-09-27T00:00:00.000Z");
|
|
27
|
+
}
|
|
28
|
+
seams.clock = new Date().toISOString();
|
|
29
|
+
}
|
|
30
|
+
installed = seams;
|
|
31
|
+
return seams;
|
|
32
|
+
}
|
|
33
|
+
/** The seams installed by `installSpecSeams` (none when it has not run). */
|
|
34
|
+
export function activeSpecSeams() {
|
|
35
|
+
return installed ?? { minter: false, clock: null };
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=seams.js.map
|
|
@@ -142,7 +142,7 @@ export class ShellSession {
|
|
|
142
142
|
line(`${style.accent("@1 @2 …")} ${style.dim("rows of the last displayed collection")}`);
|
|
143
143
|
line(`${style.accent("@_")} ${style.dim("the previous command's result")}`);
|
|
144
144
|
line(`${style.accent("@name")} ${style.dim("a named binding (also @name[i], @name.field)")}`);
|
|
145
|
-
|
|
145
|
+
io.out("");
|
|
146
146
|
line(`${style.accent("@x = <cmd|@ref>")} ${style.dim("bind a snapshot of a result")}`);
|
|
147
147
|
line(`${style.accent("unset x")} ${style.dim("drop a binding")}`);
|
|
148
148
|
line(`${style.accent("bindings")} ${style.dim("list bindings")}`);
|
|
@@ -162,7 +162,9 @@ export class ShellSession {
|
|
|
162
162
|
* (used by `let`, which snapshots rather than displays).
|
|
163
163
|
*/
|
|
164
164
|
async dispatch(tokens, sink, quiet) {
|
|
165
|
-
const { flags, command, rest } = parseGlobals(tokens);
|
|
165
|
+
const { flags, command, rest, error } = parseGlobals(tokens);
|
|
166
|
+
if (error)
|
|
167
|
+
throw error;
|
|
166
168
|
if (!command)
|
|
167
169
|
return EXIT_OK;
|
|
168
170
|
// In a remote session, thread the server address into every line (unless the
|