@ipab/cli 0.0.0-stage → 0.1.0-rc.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/LICENSE +21 -0
- package/README.md +19 -2
- package/dist/app-authoring.d.ts +23 -0
- package/dist/app-authoring.d.ts.map +1 -0
- package/dist/app-authoring.js +449 -0
- package/dist/app-authoring.js.map +1 -0
- package/dist/app-definition.d.ts +109 -0
- package/dist/app-definition.d.ts.map +1 -0
- package/dist/app-definition.js +207 -0
- package/dist/app-definition.js.map +1 -0
- package/dist/app-prebuild.d.ts +12 -0
- package/dist/app-prebuild.d.ts.map +1 -0
- package/dist/app-prebuild.js +231 -0
- package/dist/app-prebuild.js.map +1 -0
- package/dist/app-templates.d.ts +13 -0
- package/dist/app-templates.d.ts.map +1 -0
- package/dist/app-templates.js +287 -0
- package/dist/app-templates.js.map +1 -0
- package/dist/authoring.d.ts +72 -0
- package/dist/authoring.d.ts.map +1 -0
- package/dist/authoring.js +440 -0
- package/dist/authoring.js.map +1 -0
- package/dist/builtins/template.node.car +0 -0
- package/dist/builtins/template.web-app.car +0 -0
- package/dist/builtins.d.ts +6 -0
- package/dist/builtins.d.ts.map +1 -0
- package/dist/builtins.js +46 -0
- package/dist/builtins.js.map +1 -0
- package/dist/child.d.ts +5 -0
- package/dist/child.d.ts.map +1 -0
- package/dist/child.js +28 -0
- package/dist/child.js.map +1 -0
- package/dist/cli.d.ts +24 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +1072 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +156 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +42 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +164 -0
- package/dist/context.js.map +1 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +40 -0
- package/dist/errors.js.map +1 -0
- package/dist/history.d.ts +34 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +286 -0
- package/dist/history.js.map +1 -0
- package/dist/ipc.d.ts +26 -0
- package/dist/ipc.d.ts.map +1 -0
- package/dist/ipc.js +332 -0
- package/dist/ipc.js.map +1 -0
- package/dist/logs.d.ts +15 -0
- package/dist/logs.d.ts.map +1 -0
- package/dist/logs.js +118 -0
- package/dist/logs.js.map +1 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +4 -0
- package/dist/main.js.map +1 -0
- package/dist/pins.d.ts +33 -0
- package/dist/pins.d.ts.map +1 -0
- package/dist/pins.js +183 -0
- package/dist/pins.js.map +1 -0
- package/dist/runtime.d.ts +20 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +237 -0
- package/dist/runtime.js.map +1 -0
- package/dist/source-bundle.d.ts +16 -0
- package/dist/source-bundle.d.ts.map +1 -0
- package/dist/source-bundle.js +103 -0
- package/dist/source-bundle.js.map +1 -0
- package/dist/space.d.ts +33 -0
- package/dist/space.d.ts.map +1 -0
- package/dist/space.js +269 -0
- package/dist/space.js.map +1 -0
- package/dist/state.d.ts +92 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +324 -0
- package/dist/state.js.map +1 -0
- package/dist/supported-schemas.d.ts +5 -0
- package/dist/supported-schemas.d.ts.map +1 -0
- package/dist/supported-schemas.js +90 -0
- package/dist/supported-schemas.js.map +1 -0
- package/dist/template-definition.d.ts +115 -0
- package/dist/template-definition.d.ts.map +1 -0
- package/dist/template-definition.js +188 -0
- package/dist/template-definition.js.map +1 -0
- package/dist/template-source.d.ts +12 -0
- package/dist/template-source.d.ts.map +1 -0
- package/dist/template-source.js +97 -0
- package/dist/template-source.js.map +1 -0
- package/package.json +67 -3
- package/src/app-authoring.ts +614 -0
- package/src/app-definition.ts +256 -0
- package/src/app-prebuild.ts +289 -0
- package/src/app-templates.ts +382 -0
- package/src/authoring.ts +610 -0
- package/src/builtins.ts +55 -0
- package/src/child.ts +32 -0
- package/src/cli.ts +1285 -0
- package/src/config.ts +220 -0
- package/src/context.ts +232 -0
- package/src/errors.ts +46 -0
- package/src/history.ts +391 -0
- package/src/ipc.ts +358 -0
- package/src/logs.ts +132 -0
- package/src/main.ts +4 -0
- package/src/pins.ts +214 -0
- package/src/runtime.ts +300 -0
- package/src/source-bundle.ts +136 -0
- package/src/space.ts +379 -0
- package/src/state.ts +416 -0
- package/src/supported-schemas.ts +151 -0
- package/src/template-definition.ts +242 -0
- package/src/template-source.ts +128 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,1072 @@
|
|
|
1
|
+
import { createInterface } from "node:readline/promises";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { IpfsStore } from "@ipab/ipfs-store";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import stringWidth from "string-width";
|
|
6
|
+
import { configKey, effectiveConfig, parseValue, publicUrl, stateLocation, storeConfiguration, } from "./config.js";
|
|
7
|
+
import { HostError, message, object, syntax } from "./errors.js";
|
|
8
|
+
import { filters, listHistory, selectedIds, Ownership, recoverInactive, removeHistory, } from "./history.js";
|
|
9
|
+
import { execute } from "./runtime.js";
|
|
10
|
+
import { validatedBundle } from "./space.js";
|
|
11
|
+
import { exactCid, pinName, State } from "./state.js";
|
|
12
|
+
import { buildBundle, initBundle, inspectBundle, importBundle, } from "./authoring.js";
|
|
13
|
+
import { buildApp, initApp } from "./app-authoring.js";
|
|
14
|
+
import { schemaAlias } from "./app-definition.js";
|
|
15
|
+
import { addPin, deletePin, pinDetails, renamePin } from "./pins.js";
|
|
16
|
+
import { logFiles } from "./logs.js";
|
|
17
|
+
import { builtinContent } from "./builtins.js";
|
|
18
|
+
export const processIO = {
|
|
19
|
+
out(text) {
|
|
20
|
+
process.stdout.write(`${text}\n`);
|
|
21
|
+
},
|
|
22
|
+
err(text) {
|
|
23
|
+
process.stderr.write(`${text}\n`);
|
|
24
|
+
},
|
|
25
|
+
tty: Boolean(process.stdin.isTTY && process.stderr.isTTY),
|
|
26
|
+
columns() {
|
|
27
|
+
return process.stdout.columns;
|
|
28
|
+
},
|
|
29
|
+
async confirm(text) {
|
|
30
|
+
const reader = createInterface({
|
|
31
|
+
input: process.stdin,
|
|
32
|
+
output: process.stderr,
|
|
33
|
+
});
|
|
34
|
+
try {
|
|
35
|
+
return /^(y|yes)$/i.test((await reader.question(`${text} [y/N] `)).trim());
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
reader.close();
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
const commands = {
|
|
43
|
+
init: "init NAME [DIR] [OPTIONS]",
|
|
44
|
+
build: "build [DIR] [OPTIONS]",
|
|
45
|
+
"bundle init": "bundle init TYPE DIR",
|
|
46
|
+
"bundle build": "bundle build SOURCE [OPTIONS]",
|
|
47
|
+
"bundle inspect": "bundle inspect SOURCE",
|
|
48
|
+
"bundle import": "bundle import FILE.car",
|
|
49
|
+
exec: "exec [--log] APP [APP_ARGS...]",
|
|
50
|
+
open: "open [--log] CONTENT",
|
|
51
|
+
"pin add": "pin add NAME CID",
|
|
52
|
+
"pin delete": "pin delete NAME",
|
|
53
|
+
"pin list": "pin list [--kind bundle|schema] [--type TYPE]",
|
|
54
|
+
"pin show": "pin show NAME",
|
|
55
|
+
"pin rename": "pin rename OLD_NAME NEW_NAME",
|
|
56
|
+
"register list": "register list",
|
|
57
|
+
"register set": "register set SCHEMA APP [--replace] [-- TEMPLATE...]",
|
|
58
|
+
"register remove": "register remove SCHEMA",
|
|
59
|
+
"config get": "config get [KEY]",
|
|
60
|
+
"config set": "config set KEY VALUE",
|
|
61
|
+
"config reset": "config reset KEY",
|
|
62
|
+
"history list": "history list [OPTIONS]",
|
|
63
|
+
"history show": "history show RUN_ID",
|
|
64
|
+
"history remove": "history remove RUN_ID... [--yes]\n ipab history remove FILTERS [--yes]",
|
|
65
|
+
};
|
|
66
|
+
const groups = {
|
|
67
|
+
bundle: "Create, inspect and import content bundles",
|
|
68
|
+
pin: "Retain content and manage short names",
|
|
69
|
+
register: "Associate content types with Apps",
|
|
70
|
+
config: "Manage local configuration",
|
|
71
|
+
history: "View and remove execution history",
|
|
72
|
+
};
|
|
73
|
+
const commandHelp = {
|
|
74
|
+
init: {
|
|
75
|
+
summary: "Create a private TypeScript App project",
|
|
76
|
+
arguments: ["NAME Project name; DIR defaults to NAME (new/empty only)."],
|
|
77
|
+
options: [
|
|
78
|
+
"--template TEMPLATE node (default), web-app, directory, CAR, CID or pin.",
|
|
79
|
+
"--param KEY=VALUE Template parameter; repeatable. Strings are literal;",
|
|
80
|
+
" other parameter types use JSON.",
|
|
81
|
+
],
|
|
82
|
+
examples: [
|
|
83
|
+
"ipab init hello",
|
|
84
|
+
"ipab init dashboard --template web-app",
|
|
85
|
+
"ipab init hello --param description=Demo",
|
|
86
|
+
],
|
|
87
|
+
notes: ["Does not install dependencies or execute template code."],
|
|
88
|
+
},
|
|
89
|
+
build: {
|
|
90
|
+
summary: "Build an App CAR from a project",
|
|
91
|
+
arguments: ["DIR Project directory (default: current directory)."],
|
|
92
|
+
options: [
|
|
93
|
+
"-o, --output FILE Destination (default: dist/app.car).",
|
|
94
|
+
"--replace Allow replacing existing regular artifacts.",
|
|
95
|
+
"--no-import Write CAR files without importing the App into Store.",
|
|
96
|
+
"--with-source Include a separate source CAR (default: enabled).",
|
|
97
|
+
"--without-source Omit source CAR; conflicts with --with-source.",
|
|
98
|
+
],
|
|
99
|
+
examples: ["ipab build", "ipab build hello --replace --without-source"],
|
|
100
|
+
notes: [
|
|
101
|
+
"Uses local tsc and bundles Node CommonJS; ipab.yaml configures the build.",
|
|
102
|
+
"Explicit ipab-prebuild strategy executes trusted project code.",
|
|
103
|
+
"Source follows project .gitignore rules; review private files before sharing.",
|
|
104
|
+
"Imports only the App, without pinning; source needs separate import/retention.",
|
|
105
|
+
"Store failures preserve the CAR; retry with ipab bundle import FILE.car.",
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
"bundle init": {
|
|
109
|
+
summary: "Create content from a Bundle type",
|
|
110
|
+
arguments: [
|
|
111
|
+
"TYPE Definition file, type CAR, exact type CID or Schema pin.",
|
|
112
|
+
"DIR New or empty destination directory.",
|
|
113
|
+
],
|
|
114
|
+
examples: ["ipab bundle init types\\dist\\schema.car note"],
|
|
115
|
+
notes: ["Writes .bundle.yaml, .bundle-ignore and the default entry."],
|
|
116
|
+
},
|
|
117
|
+
"bundle build": {
|
|
118
|
+
summary: "Build a content or type CAR without compiling code",
|
|
119
|
+
arguments: ["SOURCE Bundle directory or definition file."],
|
|
120
|
+
options: [
|
|
121
|
+
"--schema-only Emit only the type CAR (default: dist/schema.car).",
|
|
122
|
+
"-o, --output FILE Destination (full build default: dist/bundle.car).",
|
|
123
|
+
"--replace Allow replacing an existing regular artifact.",
|
|
124
|
+
"--no-import Write the CAR without importing it into Store.",
|
|
125
|
+
],
|
|
126
|
+
examples: [
|
|
127
|
+
"ipab bundle build note",
|
|
128
|
+
"ipab bundle build types\\.bundle.yaml --schema-only",
|
|
129
|
+
],
|
|
130
|
+
notes: [
|
|
131
|
+
"Imports into the configured Store without pinning or executing Apps.",
|
|
132
|
+
"Store failures preserve the CAR; retry with ipab bundle import FILE.car.",
|
|
133
|
+
],
|
|
134
|
+
},
|
|
135
|
+
"bundle inspect": {
|
|
136
|
+
summary: "Inspect a Bundle or type definition",
|
|
137
|
+
arguments: ["SOURCE Directory, definition file, CAR, CID or named pin."],
|
|
138
|
+
examples: ["ipab bundle inspect note\\dist\\bundle.car"],
|
|
139
|
+
notes: ["Does not execute Apps, import content or create pins."],
|
|
140
|
+
},
|
|
141
|
+
"bundle import": {
|
|
142
|
+
summary: "Validate and import a CAR into the configured Store",
|
|
143
|
+
arguments: ["FILE.car Self-contained single-root Bundle or type CAR."],
|
|
144
|
+
examples: ["ipab bundle import hello\\dist\\app.car"],
|
|
145
|
+
notes: [
|
|
146
|
+
"Repeated imports are safe; no App execution or daemon startup.",
|
|
147
|
+
"Does not pin or retain content. Use pin add for persistent retention.",
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
exec: {
|
|
151
|
+
summary: "Run an App by CID or named pin",
|
|
152
|
+
arguments: [
|
|
153
|
+
"APP_ARGS Everything after APP is passed verbatim, including --help.",
|
|
154
|
+
],
|
|
155
|
+
options: [
|
|
156
|
+
"--log Record stdout/stderr; must precede APP. Requires history enabled.",
|
|
157
|
+
],
|
|
158
|
+
examples: [
|
|
159
|
+
"ipab exec reader",
|
|
160
|
+
"ipab exec reader --help",
|
|
161
|
+
"ipab exec --log reader",
|
|
162
|
+
],
|
|
163
|
+
notes: [
|
|
164
|
+
"Apps are NOT sandboxed; run only Apps you trust.",
|
|
165
|
+
"History stores argv by default; do not put secrets in arguments.",
|
|
166
|
+
"Logs may contain secrets; stdin is never logged. Logging makes output non-TTY.",
|
|
167
|
+
"--json is not supported; App stdio is passed through.",
|
|
168
|
+
],
|
|
169
|
+
},
|
|
170
|
+
open: {
|
|
171
|
+
summary: "Open content with its registered App",
|
|
172
|
+
arguments: ["CONTENT Bundle CID or named pin."],
|
|
173
|
+
options: ["--log Record stdout/stderr; requires history enabled."],
|
|
174
|
+
examples: ["ipab open CONTENT_CID", "ipab open --log my-note"],
|
|
175
|
+
notes: [
|
|
176
|
+
"Requires a saved registration and App support for the exact content type.",
|
|
177
|
+
"Apps are NOT sandboxed; run only Apps you trust.",
|
|
178
|
+
"Logs may contain secrets; stdin is never logged. Logging makes output non-TTY.",
|
|
179
|
+
"--json is not supported; App stdio is passed through.",
|
|
180
|
+
],
|
|
181
|
+
},
|
|
182
|
+
"pin add": {
|
|
183
|
+
summary: "Verify, retain and name content",
|
|
184
|
+
arguments: [
|
|
185
|
+
"NAME Unused short name.",
|
|
186
|
+
"CID Exact content CID, not another pin name.",
|
|
187
|
+
],
|
|
188
|
+
examples: ["ipab pin add reader APP_CID"],
|
|
189
|
+
notes: [
|
|
190
|
+
"Verifies the full DAG and retains it in the configured Store.",
|
|
191
|
+
"Does not execute or trust Apps, install dependencies or start a daemon.",
|
|
192
|
+
"The builtin namespace is read-only.",
|
|
193
|
+
],
|
|
194
|
+
},
|
|
195
|
+
"pin delete": {
|
|
196
|
+
summary: "Remove a user pin and release its retention",
|
|
197
|
+
examples: ["ipab pin delete reader"],
|
|
198
|
+
notes: [
|
|
199
|
+
"Releases only this pin's ownership on its original backend; no block deletion or GC.",
|
|
200
|
+
"Does not change registrations or history. Built-in pins are read-only.",
|
|
201
|
+
],
|
|
202
|
+
},
|
|
203
|
+
"pin list": {
|
|
204
|
+
summary: "List user and built-in pins",
|
|
205
|
+
options: [
|
|
206
|
+
"--kind bundle|schema Filter by kind; omit to include ordinary content.",
|
|
207
|
+
"--type TYPE Exact schema CID, Schema pin or builtin shortcut.",
|
|
208
|
+
],
|
|
209
|
+
examples: [
|
|
210
|
+
"ipab pin list",
|
|
211
|
+
"ipab pin list --kind bundle --type builtin:node-app.v1",
|
|
212
|
+
],
|
|
213
|
+
notes: [
|
|
214
|
+
"Filters combine with AND. Unfinished user operations are reported separately.",
|
|
215
|
+
"Reads local registrations and packaged built-ins, not live node status.",
|
|
216
|
+
],
|
|
217
|
+
},
|
|
218
|
+
"pin show": {
|
|
219
|
+
summary: "Show a pin's CID and type metadata",
|
|
220
|
+
examples: ["ipab pin show reader", "ipab pin show builtin:template.node"],
|
|
221
|
+
notes: [
|
|
222
|
+
"Reads local metadata; built-in pins do not claim user Store retention.",
|
|
223
|
+
],
|
|
224
|
+
},
|
|
225
|
+
"pin rename": {
|
|
226
|
+
summary: "Rename a user pin without changing its content",
|
|
227
|
+
examples: ["ipab pin rename reader my-reader"],
|
|
228
|
+
notes: [
|
|
229
|
+
"NEW_NAME must be unused. Built-in pins are read-only.",
|
|
230
|
+
"CID, ownership, registrations and history are unchanged.",
|
|
231
|
+
],
|
|
232
|
+
},
|
|
233
|
+
"register list": {
|
|
234
|
+
summary: "List saved content-type associations",
|
|
235
|
+
examples: ["ipab register list"],
|
|
236
|
+
},
|
|
237
|
+
"register set": {
|
|
238
|
+
summary: "Associate a content type with an App",
|
|
239
|
+
arguments: [
|
|
240
|
+
"SCHEMA Exact type CID or builtin:node-app.v1.",
|
|
241
|
+
"APP App CID or named pin; the exact CID is saved.",
|
|
242
|
+
"TEMPLATE App argv after --; must contain a whole argument % (default: %).",
|
|
243
|
+
],
|
|
244
|
+
options: ["--replace Allow changing an existing association."],
|
|
245
|
+
examples: [
|
|
246
|
+
"ipab register set SCHEMA_CID reader",
|
|
247
|
+
"ipab register set SCHEMA_CID reader '--' '--cid' '%'",
|
|
248
|
+
],
|
|
249
|
+
notes: [
|
|
250
|
+
"Replaces only the whole % argument with the content CID; no shell expansion.",
|
|
251
|
+
"Does not verify App type support; open checks it before execution.",
|
|
252
|
+
],
|
|
253
|
+
},
|
|
254
|
+
"register remove": {
|
|
255
|
+
summary: "Remove a saved content-type association",
|
|
256
|
+
arguments: ["SCHEMA Exact type CID or builtin:node-app.v1."],
|
|
257
|
+
examples: ["ipab register remove SCHEMA_CID"],
|
|
258
|
+
notes: ["Does not remove Apps, pins or content."],
|
|
259
|
+
},
|
|
260
|
+
"config get": {
|
|
261
|
+
summary: "Show effective configuration",
|
|
262
|
+
arguments: ["KEY Optional configuration key; omit to show all values."],
|
|
263
|
+
examples: ["ipab config get", "ipab config get store.rpc"],
|
|
264
|
+
},
|
|
265
|
+
"config set": {
|
|
266
|
+
summary: "Save a local configuration value",
|
|
267
|
+
examples: [
|
|
268
|
+
"ipab config set store.rpc http://127.0.0.1:5001",
|
|
269
|
+
"ipab config set history.enabled false",
|
|
270
|
+
],
|
|
271
|
+
notes: [
|
|
272
|
+
"store.rpc is a Kubo RPC API URL, not a gateway; no daemon is started.",
|
|
273
|
+
],
|
|
274
|
+
},
|
|
275
|
+
"config reset": {
|
|
276
|
+
summary: "Remove a saved configuration override",
|
|
277
|
+
examples: ["ipab config reset store.rpc"],
|
|
278
|
+
},
|
|
279
|
+
"history list": {
|
|
280
|
+
summary: "List execution history",
|
|
281
|
+
options: [
|
|
282
|
+
"--app APP Filter by App CID or named pin.",
|
|
283
|
+
"--status STATUS succeeded, failed or cancelled.",
|
|
284
|
+
"--since TIME Include runs ended at or after TIME.",
|
|
285
|
+
"--until TIME Include runs ended before TIME.",
|
|
286
|
+
"--limit N Maximum number of records.",
|
|
287
|
+
"--before RUN_ID Continue before a previous record.",
|
|
288
|
+
],
|
|
289
|
+
examples: [
|
|
290
|
+
"ipab history list",
|
|
291
|
+
"ipab history list --status failed --limit 10",
|
|
292
|
+
],
|
|
293
|
+
notes: [
|
|
294
|
+
"Filters combine with AND; times use RFC3339 with an explicit timezone.",
|
|
295
|
+
],
|
|
296
|
+
},
|
|
297
|
+
"history show": {
|
|
298
|
+
summary: "Show an execution record and available log paths",
|
|
299
|
+
examples: ["ipab history show RUN_ID"],
|
|
300
|
+
notes: ["Only runs started with --log have recorded stdout/stderr."],
|
|
301
|
+
},
|
|
302
|
+
"history remove": {
|
|
303
|
+
summary: "Remove execution records and their logs",
|
|
304
|
+
options: [
|
|
305
|
+
"--app APP Filter by App CID or named pin.",
|
|
306
|
+
"--status STATUS succeeded, failed or cancelled.",
|
|
307
|
+
"--since TIME Include runs ended at or after TIME.",
|
|
308
|
+
"--until TIME Include runs ended before TIME.",
|
|
309
|
+
"--yes Skip confirmation; required outside an interactive terminal.",
|
|
310
|
+
],
|
|
311
|
+
examples: [
|
|
312
|
+
"ipab history remove RUN_ID --yes",
|
|
313
|
+
"ipab history remove --status failed --until 2026-11-01T00:00:00Z --yes",
|
|
314
|
+
],
|
|
315
|
+
notes: [
|
|
316
|
+
"Use explicit IDs OR at least one filter, not both.",
|
|
317
|
+
"Filters combine with AND; times use RFC3339 with an explicit timezone.",
|
|
318
|
+
"Deletes associated logs and releases history retention; does not request Store GC.",
|
|
319
|
+
],
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
export function help(path = "") {
|
|
323
|
+
const globalOptions = `Global options (before COMMAND):
|
|
324
|
+
--state-dir PATH Set the local state directory
|
|
325
|
+
--store-rpc URL Use a Kubo RPC endpoint
|
|
326
|
+
--json Output management/development metadata, not App stdio`;
|
|
327
|
+
const footer = "Full reference: https://github.com/silvermoon-work/ipab/blob/main/docs/reference.md";
|
|
328
|
+
if (!path)
|
|
329
|
+
return `IPAB — run Apps and open content by CID.
|
|
330
|
+
|
|
331
|
+
Usage: ipab [GLOBAL_OPTIONS] COMMAND
|
|
332
|
+
|
|
333
|
+
Run Apps:
|
|
334
|
+
exec ${commandHelp.exec.summary}
|
|
335
|
+
open ${commandHelp.open.summary}
|
|
336
|
+
|
|
337
|
+
Create:
|
|
338
|
+
init ${commandHelp.init.summary}
|
|
339
|
+
build ${commandHelp.build.summary}
|
|
340
|
+
bundle ${groups.bundle}
|
|
341
|
+
|
|
342
|
+
Manage:
|
|
343
|
+
pin ${groups.pin}
|
|
344
|
+
register ${groups.register}
|
|
345
|
+
config ${groups.config}
|
|
346
|
+
history ${groups.history}
|
|
347
|
+
|
|
348
|
+
${globalOptions}
|
|
349
|
+
-h, --help Show help for the current command
|
|
350
|
+
--version Show version
|
|
351
|
+
|
|
352
|
+
Run 'ipab COMMAND --help' for command details.
|
|
353
|
+
Apps are NOT sandboxed; run only Apps you trust.
|
|
354
|
+
${footer}`;
|
|
355
|
+
const usage = commands[path];
|
|
356
|
+
if (usage) {
|
|
357
|
+
const topic = commandHelp[path];
|
|
358
|
+
const section = (title, lines) => `\n\n${title}:\n${lines.map((line) => ` ${line}`).join("\n")}`;
|
|
359
|
+
return [
|
|
360
|
+
`${topic.summary}.\n\nUsage: ipab [GLOBAL_OPTIONS] ${usage}`,
|
|
361
|
+
...(topic.arguments ? [section("Arguments", topic.arguments)] : []),
|
|
362
|
+
...(topic.options ? [section("Options", topic.options)] : []),
|
|
363
|
+
section("Examples", topic.examples),
|
|
364
|
+
...(topic.notes ? [section("Notes", topic.notes)] : []),
|
|
365
|
+
`\n\n-h, --help shows this help. Global options: see 'ipab --help'.\n${footer}`,
|
|
366
|
+
].join("");
|
|
367
|
+
}
|
|
368
|
+
const leaves = Object.keys(commands).filter((key) => key.startsWith(`${path} `));
|
|
369
|
+
if (!leaves.length)
|
|
370
|
+
syntax(`Unknown command "${path}".`);
|
|
371
|
+
return `${groups[path]}.
|
|
372
|
+
|
|
373
|
+
Usage: ipab [GLOBAL_OPTIONS] ${path} COMMAND
|
|
374
|
+
|
|
375
|
+
Commands:
|
|
376
|
+
${leaves.map((key) => ` ${key.slice(path.length + 1).padEnd(8)}${commandHelp[key].summary}`).join("\n")}
|
|
377
|
+
|
|
378
|
+
Run 'ipab ${path} COMMAND --help' for command details.
|
|
379
|
+
Global options: see 'ipab --help'.
|
|
380
|
+
${footer}`;
|
|
381
|
+
}
|
|
382
|
+
function printable(text) {
|
|
383
|
+
return [...text]
|
|
384
|
+
.map((char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127
|
|
385
|
+
? JSON.stringify(char).slice(1, -1)
|
|
386
|
+
: char)
|
|
387
|
+
.join("");
|
|
388
|
+
}
|
|
389
|
+
function runTime(value, field) {
|
|
390
|
+
const date = new Date(value);
|
|
391
|
+
if (!Number.isFinite(date.getTime()))
|
|
392
|
+
throw new HostError(`Execution record has an invalid ${field} time.`);
|
|
393
|
+
const pad = (part) => String(part).padStart(2, "0");
|
|
394
|
+
const day = [
|
|
395
|
+
String(date.getFullYear()).padStart(4, "0"),
|
|
396
|
+
pad(date.getMonth() + 1),
|
|
397
|
+
pad(date.getDate()),
|
|
398
|
+
].join("-");
|
|
399
|
+
const time = [date.getHours(), date.getMinutes(), date.getSeconds()]
|
|
400
|
+
.map(pad)
|
|
401
|
+
.join(":");
|
|
402
|
+
const offset = -date.getTimezoneOffset();
|
|
403
|
+
const zone = `${offset < 0 ? "-" : "+"}${pad(Math.floor(Math.abs(offset) / 60))}${pad(Math.abs(offset) % 60)}`;
|
|
404
|
+
return `${day} ${time} ${zone}`;
|
|
405
|
+
}
|
|
406
|
+
function runDuration(record) {
|
|
407
|
+
const started = Date.parse(record.startedAt);
|
|
408
|
+
if (!Number.isFinite(started))
|
|
409
|
+
throw new HostError("Execution record has an invalid start time.");
|
|
410
|
+
const duration = Date.parse(record.endedAt) - started;
|
|
411
|
+
if (!Number.isFinite(duration) || duration < 0)
|
|
412
|
+
throw new HostError("Execution record has an invalid duration.");
|
|
413
|
+
return `${Math.floor(duration / 1000)}s`;
|
|
414
|
+
}
|
|
415
|
+
function table(headers, rows, empty) {
|
|
416
|
+
if (!rows.length)
|
|
417
|
+
return empty;
|
|
418
|
+
const cells = rows.map((row) => row.map(printable));
|
|
419
|
+
const widths = headers.map((header, column) => cells.reduce((width, row) => Math.max(width, stringWidth(row[column])), stringWidth(header)));
|
|
420
|
+
return [headers, ...cells]
|
|
421
|
+
.map((row) => row
|
|
422
|
+
.map((cell, column) => column === headers.length - 1
|
|
423
|
+
? cell
|
|
424
|
+
: cell.padEnd(cell.length + widths[column] - stringWidth(cell)))
|
|
425
|
+
.join(" "))
|
|
426
|
+
.join("\n");
|
|
427
|
+
}
|
|
428
|
+
function runDetails(record) {
|
|
429
|
+
const fields = [
|
|
430
|
+
["Run", record.id],
|
|
431
|
+
["Origin", record.origin],
|
|
432
|
+
["App CID", record.appCid],
|
|
433
|
+
["Argv", JSON.stringify(record.argv)],
|
|
434
|
+
["Started", runTime(record.startedAt, "start")],
|
|
435
|
+
["Ended", runTime(record.endedAt, "end")],
|
|
436
|
+
["Status", record.status],
|
|
437
|
+
[
|
|
438
|
+
"App exit",
|
|
439
|
+
record.appExitCode === null ? "unavailable" : String(record.appExitCode),
|
|
440
|
+
],
|
|
441
|
+
["Output CID", record.outputCid ?? "none"],
|
|
442
|
+
[
|
|
443
|
+
"Retention",
|
|
444
|
+
`${record.retention.state} (${record.retention.owned ? "record-owned" : "not record-owned"})`,
|
|
445
|
+
],
|
|
446
|
+
[
|
|
447
|
+
"Error",
|
|
448
|
+
record.error
|
|
449
|
+
? `${record.error.phase}/${record.error.code}: ${record.error.message}`
|
|
450
|
+
: "none",
|
|
451
|
+
],
|
|
452
|
+
];
|
|
453
|
+
if (record.retention.owner)
|
|
454
|
+
fields.push(["Retention owner", record.retention.owner]);
|
|
455
|
+
return fields.map(([key, value]) => `${key}: ${printable(value)}`).join("\n");
|
|
456
|
+
}
|
|
457
|
+
async function closeStore(store, appExitCode) {
|
|
458
|
+
try {
|
|
459
|
+
await store?.close();
|
|
460
|
+
}
|
|
461
|
+
catch (error) {
|
|
462
|
+
throw new HostError(`Store cleanup failed: ${message(error)}`, appExitCode || 1, "CLEANUP_FAILED", "cleanup");
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
export function parse(argv) {
|
|
466
|
+
const tokens = [...argv];
|
|
467
|
+
const flags = { json: false };
|
|
468
|
+
while (tokens[0]?.startsWith("-")) {
|
|
469
|
+
const flag = tokens.shift();
|
|
470
|
+
if (flag === "-h" || flag === "--help")
|
|
471
|
+
return {
|
|
472
|
+
flags,
|
|
473
|
+
path: "",
|
|
474
|
+
positional: [],
|
|
475
|
+
options: {},
|
|
476
|
+
help: true,
|
|
477
|
+
version: false,
|
|
478
|
+
};
|
|
479
|
+
if (flag === "--version") {
|
|
480
|
+
if (tokens.length)
|
|
481
|
+
syntax("--version does not accept a command.");
|
|
482
|
+
return {
|
|
483
|
+
flags,
|
|
484
|
+
path: "",
|
|
485
|
+
positional: [],
|
|
486
|
+
options: {},
|
|
487
|
+
help: false,
|
|
488
|
+
version: true,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
if (flag === "--json")
|
|
492
|
+
flags.json = true;
|
|
493
|
+
else if (flag === "--state-dir" || flag === "--store-rpc") {
|
|
494
|
+
const value = tokens.shift();
|
|
495
|
+
if (!value || value.startsWith("-"))
|
|
496
|
+
syntax(`${flag} needs a value.`);
|
|
497
|
+
if (flag === "--state-dir")
|
|
498
|
+
flags.stateDir = value;
|
|
499
|
+
else {
|
|
500
|
+
flags.storeRpc = publicUrl(value);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
else
|
|
504
|
+
syntax(`Unknown global option "${flag}".`);
|
|
505
|
+
}
|
|
506
|
+
const group = tokens.shift() ?? "";
|
|
507
|
+
const result = {
|
|
508
|
+
flags,
|
|
509
|
+
path: group,
|
|
510
|
+
positional: [],
|
|
511
|
+
options: {},
|
|
512
|
+
help: !group,
|
|
513
|
+
version: false,
|
|
514
|
+
};
|
|
515
|
+
if (!group)
|
|
516
|
+
return result;
|
|
517
|
+
if (group === "exec") {
|
|
518
|
+
if (flags.json)
|
|
519
|
+
syntax("--json is not supported for exec/open.");
|
|
520
|
+
if (tokens[0] === "--log") {
|
|
521
|
+
result.options.log = true;
|
|
522
|
+
tokens.shift();
|
|
523
|
+
}
|
|
524
|
+
if (tokens[0] === "-h" || tokens[0] === "--help") {
|
|
525
|
+
result.help = true;
|
|
526
|
+
return result;
|
|
527
|
+
}
|
|
528
|
+
if (!tokens.length || tokens[0].startsWith("-"))
|
|
529
|
+
syntax(`Expected APP.\n${help("exec")}`);
|
|
530
|
+
result.positional = tokens;
|
|
531
|
+
return result;
|
|
532
|
+
}
|
|
533
|
+
if (!["open", "init", "build"].includes(group)) {
|
|
534
|
+
if (!["bundle", "pin", "register", "config", "history"].includes(group))
|
|
535
|
+
syntax(`Unknown command "${group}".`);
|
|
536
|
+
if (!tokens.length || tokens[0] === "-h" || tokens[0] === "--help") {
|
|
537
|
+
result.help = true;
|
|
538
|
+
return result;
|
|
539
|
+
}
|
|
540
|
+
result.path = `${group} ${tokens.shift()}`;
|
|
541
|
+
}
|
|
542
|
+
else if (group === "open" && flags.json)
|
|
543
|
+
syntax("--json is not supported for exec/open.");
|
|
544
|
+
if (!commands[result.path])
|
|
545
|
+
syntax(`Unknown command "${result.path}".`);
|
|
546
|
+
const separator = result.path === "register set" ? tokens.indexOf("--") : -1;
|
|
547
|
+
if (separator >= 0)
|
|
548
|
+
result.template = tokens.splice(separator + 1);
|
|
549
|
+
if (separator >= 0)
|
|
550
|
+
tokens.splice(separator);
|
|
551
|
+
if (tokens.includes("-h") || tokens.includes("--help")) {
|
|
552
|
+
result.help = true;
|
|
553
|
+
return result;
|
|
554
|
+
}
|
|
555
|
+
const booleans = result.path === "bundle build"
|
|
556
|
+
? ["replace", "schema-only", "no-import"]
|
|
557
|
+
: result.path === "build"
|
|
558
|
+
? ["replace", "no-import", "with-source", "without-source"]
|
|
559
|
+
: result.path === "register set"
|
|
560
|
+
? ["replace"]
|
|
561
|
+
: result.path === "history remove"
|
|
562
|
+
? ["yes"]
|
|
563
|
+
: result.path === "open"
|
|
564
|
+
? ["log"]
|
|
565
|
+
: [];
|
|
566
|
+
const values = result.path === "init"
|
|
567
|
+
? ["template"]
|
|
568
|
+
: result.path === "bundle build" || result.path === "build"
|
|
569
|
+
? ["output"]
|
|
570
|
+
: result.path === "history list"
|
|
571
|
+
? ["app", "status", "since", "until", "limit", "before"]
|
|
572
|
+
: result.path === "history remove"
|
|
573
|
+
? ["app", "status", "since", "until"]
|
|
574
|
+
: result.path === "pin list"
|
|
575
|
+
? ["kind", "type"]
|
|
576
|
+
: [];
|
|
577
|
+
while (tokens.length) {
|
|
578
|
+
const value = tokens.shift();
|
|
579
|
+
if (value.startsWith("--") || value === "-o") {
|
|
580
|
+
const option = value === "-o" ? "output" : value.slice(2);
|
|
581
|
+
if (result.path === "init" && option === "param") {
|
|
582
|
+
const input = tokens.shift();
|
|
583
|
+
if (!input || input.startsWith("--") || input.indexOf("=") < 1)
|
|
584
|
+
syntax("--param requires KEY=VALUE.");
|
|
585
|
+
(result.parameters ??= []).push(input);
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
if (Object.hasOwn(result.options, option))
|
|
589
|
+
syntax(`Duplicate option ${value}.`);
|
|
590
|
+
if (booleans.includes(option))
|
|
591
|
+
result.options[option] = true;
|
|
592
|
+
else if (values.includes(option)) {
|
|
593
|
+
const input = tokens.shift();
|
|
594
|
+
if (!input || input.startsWith("--"))
|
|
595
|
+
syntax(`${value} needs a value.`);
|
|
596
|
+
result.options[option] = input;
|
|
597
|
+
}
|
|
598
|
+
else
|
|
599
|
+
syntax(`Unknown option "${value}".`);
|
|
600
|
+
}
|
|
601
|
+
else
|
|
602
|
+
result.positional.push(value);
|
|
603
|
+
}
|
|
604
|
+
const counts = {
|
|
605
|
+
init: [1, 2],
|
|
606
|
+
build: [0, 1],
|
|
607
|
+
"bundle init": [2, 2],
|
|
608
|
+
"bundle build": [1, 1],
|
|
609
|
+
"bundle inspect": [1, 1],
|
|
610
|
+
"bundle import": [1, 1],
|
|
611
|
+
open: [1, 1],
|
|
612
|
+
"pin add": [2, 2],
|
|
613
|
+
"pin delete": [1, 1],
|
|
614
|
+
"pin list": [0, 0],
|
|
615
|
+
"pin show": [1, 1],
|
|
616
|
+
"pin rename": [2, 2],
|
|
617
|
+
"register list": [0, 0],
|
|
618
|
+
"register set": [2, 2],
|
|
619
|
+
"register remove": [1, 1],
|
|
620
|
+
"config get": [0, 1],
|
|
621
|
+
"config set": [2, 2],
|
|
622
|
+
"config reset": [1, 1],
|
|
623
|
+
"history list": [0, 0],
|
|
624
|
+
"history show": [1, 1],
|
|
625
|
+
"history remove": [0, Number.MAX_SAFE_INTEGER],
|
|
626
|
+
};
|
|
627
|
+
if (result.options["with-source"] && result.options["without-source"])
|
|
628
|
+
syntax("--with-source and --without-source are mutually exclusive.");
|
|
629
|
+
const [minimum, maximum] = counts[result.path];
|
|
630
|
+
if (result.positional.length < minimum || result.positional.length > maximum)
|
|
631
|
+
syntax(`Invalid arguments.\n${help(result.path)}`);
|
|
632
|
+
if (result.template &&
|
|
633
|
+
(!result.template.length || !result.template.includes("%")))
|
|
634
|
+
syntax('Argument template must contain a whole "%" token.');
|
|
635
|
+
if (result.path === "history remove") {
|
|
636
|
+
const selected = values.some((value) => Object.hasOwn(result.options, value));
|
|
637
|
+
if ((!selected && !result.positional.length) ||
|
|
638
|
+
(selected && result.positional.length))
|
|
639
|
+
syntax("history remove requires explicit IDs OR filters, not both.");
|
|
640
|
+
}
|
|
641
|
+
// Validate syntactic values before state access, let alone a backend connection.
|
|
642
|
+
if (result.path.startsWith("pin ") && result.path !== "pin list") {
|
|
643
|
+
pinName(result.positional[0], result.path === "pin show");
|
|
644
|
+
if (result.path === "pin add")
|
|
645
|
+
exactCid(result.positional[1]);
|
|
646
|
+
if (result.path === "pin rename")
|
|
647
|
+
pinName(result.positional[1]);
|
|
648
|
+
}
|
|
649
|
+
if (result.options.kind !== undefined &&
|
|
650
|
+
!["bundle", "schema"].includes(String(result.options.kind)))
|
|
651
|
+
syntax("--kind must be bundle or schema.");
|
|
652
|
+
if (result.path.startsWith("config ") && result.positional[0]) {
|
|
653
|
+
if (!(result.path === "config get" && result.positional[0] === "state-dir"))
|
|
654
|
+
configKey(result.positional[0]);
|
|
655
|
+
if (result.path === "config set")
|
|
656
|
+
parseValue(configKey(result.positional[0]), result.positional[1], true);
|
|
657
|
+
}
|
|
658
|
+
if (typeof result.options.limit === "string")
|
|
659
|
+
parseValue("history.limit", result.options.limit, true);
|
|
660
|
+
if (result.options.output === "-")
|
|
661
|
+
syntax("CAR output requires a file path; binary stdout is not supported.");
|
|
662
|
+
return result;
|
|
663
|
+
}
|
|
664
|
+
async function performCli(argv, io, env) {
|
|
665
|
+
let state;
|
|
666
|
+
let store;
|
|
667
|
+
let appExitCode;
|
|
668
|
+
try {
|
|
669
|
+
const parsed = parse(argv);
|
|
670
|
+
if (parsed.help) {
|
|
671
|
+
io.out(help(parsed.path));
|
|
672
|
+
return 0;
|
|
673
|
+
}
|
|
674
|
+
if (parsed.version) {
|
|
675
|
+
const pkg = object(JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf8")), "CLI package");
|
|
676
|
+
if (typeof pkg.version !== "string" || !pkg.version.trim())
|
|
677
|
+
throw new HostError("CLI package is missing its version.");
|
|
678
|
+
io.out(pkg.version);
|
|
679
|
+
return 0;
|
|
680
|
+
}
|
|
681
|
+
if (parsed.path === "history remove" && !parsed.options.yes && !io.tty)
|
|
682
|
+
syntax("Non-interactive history remove requires --yes.");
|
|
683
|
+
const builtins = await builtinContent();
|
|
684
|
+
state = new State(stateLocation(parsed.flags, env).path, builtins.pins);
|
|
685
|
+
const items = effectiveConfig(state.config(), parsed.flags, env);
|
|
686
|
+
const value = (key) => items.find((item) => item.key === key).value;
|
|
687
|
+
const emit = (result, human) => {
|
|
688
|
+
io.out(parsed.flags.json ? JSON.stringify(result) : human);
|
|
689
|
+
};
|
|
690
|
+
const [first, second] = parsed.positional;
|
|
691
|
+
const filter = parsed.path.startsWith("history ")
|
|
692
|
+
? filters(state, parsed.options)
|
|
693
|
+
: {};
|
|
694
|
+
switch (parsed.path) {
|
|
695
|
+
case "init":
|
|
696
|
+
case "build":
|
|
697
|
+
case "bundle init":
|
|
698
|
+
case "bundle build":
|
|
699
|
+
case "bundle inspect":
|
|
700
|
+
case "bundle import": {
|
|
701
|
+
const controller = new AbortController();
|
|
702
|
+
let cancelled = 0;
|
|
703
|
+
const interrupt = () => {
|
|
704
|
+
cancelled = 130;
|
|
705
|
+
controller.abort(new Error("Interrupted."));
|
|
706
|
+
};
|
|
707
|
+
const terminate = () => {
|
|
708
|
+
cancelled = 143;
|
|
709
|
+
controller.abort(new Error("Terminated."));
|
|
710
|
+
};
|
|
711
|
+
process.once("SIGINT", interrupt);
|
|
712
|
+
process.once("SIGTERM", terminate);
|
|
713
|
+
try {
|
|
714
|
+
let pendingStore;
|
|
715
|
+
const getStore = () => {
|
|
716
|
+
pendingStore ??= storeConfiguration(value("store.rpc")).then((configuration) => {
|
|
717
|
+
store = new IpfsStore(configuration);
|
|
718
|
+
return store;
|
|
719
|
+
});
|
|
720
|
+
return pendingStore;
|
|
721
|
+
};
|
|
722
|
+
const request = {
|
|
723
|
+
signal: controller.signal,
|
|
724
|
+
diagnostic: (text) => {
|
|
725
|
+
io.err(text);
|
|
726
|
+
},
|
|
727
|
+
resolveCid: async (cid, options) => {
|
|
728
|
+
options.signal?.throwIfAborted();
|
|
729
|
+
const builtin = builtins.blocks.get(cid.toV1().toString());
|
|
730
|
+
if (builtin)
|
|
731
|
+
return builtin.slice();
|
|
732
|
+
return (await getStore()).block.get(cid.toString(), options);
|
|
733
|
+
},
|
|
734
|
+
};
|
|
735
|
+
const builtinSource = first ? state.pin(first) : undefined;
|
|
736
|
+
const source = builtinSource?.builtin && builtinSource.metadata?.kind === "bundle"
|
|
737
|
+
? builtinSource.cid
|
|
738
|
+
: first;
|
|
739
|
+
if (parsed.path === "bundle build" || parsed.path === "build")
|
|
740
|
+
io.err(parsed.path === "build"
|
|
741
|
+
? "Typechecking, bundling and building App..."
|
|
742
|
+
: "Validating and building bundle...");
|
|
743
|
+
if (parsed.path === "bundle import")
|
|
744
|
+
io.err("Validating and importing CAR into Store...");
|
|
745
|
+
const result = parsed.path === "init"
|
|
746
|
+
? await initApp(first, second, {
|
|
747
|
+
...request,
|
|
748
|
+
...(typeof parsed.options.template === "string"
|
|
749
|
+
? {
|
|
750
|
+
template: state.pin(parsed.options.template)?.builtin
|
|
751
|
+
? parsed.options.template
|
|
752
|
+
: state.pin(parsed.options.template)
|
|
753
|
+
? state.resolve(parsed.options.template)
|
|
754
|
+
: parsed.options.template,
|
|
755
|
+
}
|
|
756
|
+
: {}),
|
|
757
|
+
...(parsed.parameters
|
|
758
|
+
? { parameters: parsed.parameters }
|
|
759
|
+
: {}),
|
|
760
|
+
})
|
|
761
|
+
: parsed.path === "build"
|
|
762
|
+
? await buildApp(first, {
|
|
763
|
+
...request,
|
|
764
|
+
replace: Boolean(parsed.options.replace),
|
|
765
|
+
...(parsed.options["with-source"]
|
|
766
|
+
? { includeSource: true }
|
|
767
|
+
: parsed.options["without-source"]
|
|
768
|
+
? { includeSource: false }
|
|
769
|
+
: {}),
|
|
770
|
+
...(typeof parsed.options.output === "string"
|
|
771
|
+
? { output: parsed.options.output }
|
|
772
|
+
: {}),
|
|
773
|
+
outputDir: String(value("bundle.outputDir")),
|
|
774
|
+
})
|
|
775
|
+
: parsed.path === "bundle init"
|
|
776
|
+
? await initBundle(source, second, request)
|
|
777
|
+
: parsed.path === "bundle build"
|
|
778
|
+
? await buildBundle(source, {
|
|
779
|
+
...request,
|
|
780
|
+
schemaOnly: Boolean(parsed.options["schema-only"]),
|
|
781
|
+
replace: Boolean(parsed.options.replace),
|
|
782
|
+
...(typeof parsed.options.output === "string"
|
|
783
|
+
? { output: parsed.options.output }
|
|
784
|
+
: {}),
|
|
785
|
+
outputDir: String(value("bundle.outputDir")),
|
|
786
|
+
})
|
|
787
|
+
: parsed.path === "bundle import"
|
|
788
|
+
? await importBundle(first, getStore, request)
|
|
789
|
+
: await inspectBundle(source, request);
|
|
790
|
+
const importing = "output" in result && !parsed.options["no-import"];
|
|
791
|
+
if (importing) {
|
|
792
|
+
io.err("Importing build result into Store (without pinning)...");
|
|
793
|
+
try {
|
|
794
|
+
await importBundle(result.output, getStore, request);
|
|
795
|
+
}
|
|
796
|
+
catch (cause) {
|
|
797
|
+
throw new HostError(`${message(cause)} CAR remains at ${JSON.stringify(result.output)}; retry with ipab bundle import ${JSON.stringify(result.output)}, or build with --no-import.`, 1, "IMPORT_FAILED", "authoring");
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
const closing = store;
|
|
801
|
+
store = undefined;
|
|
802
|
+
await closeStore(closing, undefined);
|
|
803
|
+
state.close();
|
|
804
|
+
state = undefined;
|
|
805
|
+
controller.signal.throwIfAborted();
|
|
806
|
+
if ("output" in result)
|
|
807
|
+
emit({ ...result, imported: importing }, `CID: ${result.cid}\nSchema CID: ${result.schemaCid}\nCAR: ${result.output}${"source" in result ? `\nSource CID: ${result.source.cid}\nSource CAR: ${result.source.output} (not imported or pinned)` : ""}\nStore: ${importing ? "imported (not pinned)" : "not imported (--no-import)"}`);
|
|
808
|
+
else if (result.kind === "bundle-import")
|
|
809
|
+
emit(result, `Imported CID: ${result.cid}\nSchema CID: ${result.schemaCid}\nBlocks: ${result.blocks}\nCAR: ${result.source}`);
|
|
810
|
+
else if (result.kind === "app-init") {
|
|
811
|
+
emit(result, `Created App project: ${result.directory}\nSchema CID: ${result.schemaCid}`);
|
|
812
|
+
io.err(`Next: install declared dependencies in ${JSON.stringify(result.directory)}; then ipab build ${JSON.stringify(result.directory)} (requires locally linked ipab).`);
|
|
813
|
+
}
|
|
814
|
+
else if ("directory" in result) {
|
|
815
|
+
emit(result, `Created: ${result.directory}\nSchema CID: ${result.schemaCid}\nEntry: ${result.entry.path} (${result.entry.contentType})`);
|
|
816
|
+
io.err(`Next: ipab bundle build ${JSON.stringify(result.directory)}`);
|
|
817
|
+
}
|
|
818
|
+
else
|
|
819
|
+
emit(result, `Kind: ${result.kind}\nName: ${printable(result.name)}\nSchema CID: ${result.schemaCid}\nEntry: ${result.entry.path} (${result.entry.contentType})\nValidation: passed`);
|
|
820
|
+
}
|
|
821
|
+
catch (cause) {
|
|
822
|
+
if (cancelled)
|
|
823
|
+
throw new HostError(message(cause), cancelled, "ABORTED", "authoring");
|
|
824
|
+
throw cause;
|
|
825
|
+
}
|
|
826
|
+
finally {
|
|
827
|
+
process.removeListener("SIGINT", interrupt);
|
|
828
|
+
process.removeListener("SIGTERM", terminate);
|
|
829
|
+
}
|
|
830
|
+
break;
|
|
831
|
+
}
|
|
832
|
+
case "pin list": {
|
|
833
|
+
if (typeof parsed.options.type === "string") {
|
|
834
|
+
const named = state.pin(parsed.options.type);
|
|
835
|
+
if (named && pinDetails(named).kind !== "schema")
|
|
836
|
+
throw new HostError("--type short names must identify a Schema pin.");
|
|
837
|
+
}
|
|
838
|
+
const type = typeof parsed.options.type === "string"
|
|
839
|
+
? state.resolve(schemaAlias(parsed.options.type), true)
|
|
840
|
+
: undefined;
|
|
841
|
+
const pins = state.pins();
|
|
842
|
+
const listed = pins
|
|
843
|
+
.filter((pin) => pin.status === "ready")
|
|
844
|
+
.map(pinDetails)
|
|
845
|
+
.filter((pin) => (!parsed.options.kind || pin.kind === parsed.options.kind) &&
|
|
846
|
+
(type === undefined || pin.schemaCid === type));
|
|
847
|
+
const pending = pins
|
|
848
|
+
.filter((pin) => pin.status !== "ready")
|
|
849
|
+
.map(({ name, cid, status }) => ({ name, cid, status }));
|
|
850
|
+
const legacyAliases = state.aliases();
|
|
851
|
+
if (pending.length)
|
|
852
|
+
io.err(`Unfinished pin operations (not usable pins): ${JSON.stringify(pending)}. Retry pin add/delete.`);
|
|
853
|
+
if (legacyAliases.length)
|
|
854
|
+
io.err("Legacy Alias metadata is preserved but is not pinned. Use the listed exact CIDs with pin add to migrate.");
|
|
855
|
+
emit({ items: listed, pending, legacyAliases }, table(["NAME", "CID", "KIND", "BUNDLE TYPE", "SOURCE"], listed.map((pin) => [
|
|
856
|
+
pin.name,
|
|
857
|
+
pin.cid,
|
|
858
|
+
pin.kind,
|
|
859
|
+
pin.typeName ?? "(no Bundle type metadata)",
|
|
860
|
+
pin.builtin ? "built-in (read-only)" : "user",
|
|
861
|
+
]), "No named pins.") +
|
|
862
|
+
(legacyAliases.length
|
|
863
|
+
? `\nLegacy Aliases (not pinned):\n${table(["NAME", "CID"], legacyAliases.map((alias) => [alias.name, alias.cid]), "")}`
|
|
864
|
+
: ""));
|
|
865
|
+
break;
|
|
866
|
+
}
|
|
867
|
+
case "pin add": {
|
|
868
|
+
io.err("Fetching, verifying and retaining content in Store...");
|
|
869
|
+
const result = await addPin(state, first, second, await storeConfiguration(value("store.rpc")));
|
|
870
|
+
emit(result, `${result.name} → ${result.cid}\nKind: ${result.kind}\nBundle type: ${printable(result.typeName ?? "(no Bundle type metadata)")}`);
|
|
871
|
+
break;
|
|
872
|
+
}
|
|
873
|
+
case "pin delete": {
|
|
874
|
+
const result = await deletePin(state, first);
|
|
875
|
+
emit(result, `Deleted ${first}; released only its retention responsibility. No blocks deleted or GC requested.`);
|
|
876
|
+
break;
|
|
877
|
+
}
|
|
878
|
+
case "pin show": {
|
|
879
|
+
const pin = state.pin(first);
|
|
880
|
+
if (!pin)
|
|
881
|
+
throw new HostError(`Pin "${first}" does not exist. Run ipab pin list.`);
|
|
882
|
+
const result = pinDetails(pin);
|
|
883
|
+
emit(result, `Name: ${printable(result.name)}\nCID: ${result.cid}\nKind: ${result.kind}\nBundle type: ${printable(result.typeName ?? "(no Bundle type metadata)")}${result.schemaCid ? `\nSchema CID: ${result.schemaCid}` : ""}\nSource: ${result.builtin ? "built-in (read-only; content bundled with IPAB)" : "local registration (not live node verification)"}`);
|
|
884
|
+
break;
|
|
885
|
+
}
|
|
886
|
+
case "pin rename": {
|
|
887
|
+
const result = renamePin(state, first, second);
|
|
888
|
+
emit(result, `${first} → ${result.name} (${result.cid})`);
|
|
889
|
+
break;
|
|
890
|
+
}
|
|
891
|
+
case "register list": {
|
|
892
|
+
const associations = state.registrations();
|
|
893
|
+
emit({ items: associations }, table(["SCHEMA CID", "APP CID", "ARGV"], associations.map((association) => [
|
|
894
|
+
association.schemaCid,
|
|
895
|
+
association.appCid,
|
|
896
|
+
JSON.stringify(association.argv),
|
|
897
|
+
]), "No associations."));
|
|
898
|
+
break;
|
|
899
|
+
}
|
|
900
|
+
case "register set": {
|
|
901
|
+
const association = state.setAssociation(schemaAlias(first), second, parsed.template ?? ["%"], Boolean(parsed.options.replace));
|
|
902
|
+
emit(association, `${association.schemaCid} → ${association.appCid} ${JSON.stringify(association.argv)}`);
|
|
903
|
+
break;
|
|
904
|
+
}
|
|
905
|
+
case "register remove": {
|
|
906
|
+
const removed = state.removeAssociation(schemaAlias(first));
|
|
907
|
+
emit({ removed: [removed] }, `Removed association for ${removed}.`);
|
|
908
|
+
break;
|
|
909
|
+
}
|
|
910
|
+
case "config get": {
|
|
911
|
+
const selected = first
|
|
912
|
+
? items.filter((item) => item.key === first)
|
|
913
|
+
: items;
|
|
914
|
+
emit({ items: selected }, table(["KEY", "VALUE", "SOURCE"], selected.map((item) => [item.key, String(item.value), item.source]), "No configuration values."));
|
|
915
|
+
break;
|
|
916
|
+
}
|
|
917
|
+
case "config set": {
|
|
918
|
+
const key = configKey(first);
|
|
919
|
+
const saved = parseValue(key, second, true);
|
|
920
|
+
state.setConfig(key, saved);
|
|
921
|
+
const source = items.find((item) => item.key === key).source;
|
|
922
|
+
if (source === "env" || source === "flag")
|
|
923
|
+
io.err(`Warning: saved user value is masked by ${source}.`);
|
|
924
|
+
emit({ key, value: saved, source: "user" }, `Saved ${key} = ${printable(String(saved))} (user).`);
|
|
925
|
+
break;
|
|
926
|
+
}
|
|
927
|
+
case "config reset":
|
|
928
|
+
state.resetConfig(configKey(first));
|
|
929
|
+
emit({ reset: [first] }, `Reset user override: ${first}.`);
|
|
930
|
+
break;
|
|
931
|
+
case "history list": {
|
|
932
|
+
const page = listHistory(state, filter, Number(parsed.options.limit ?? value("history.limit")), typeof parsed.options.before === "string"
|
|
933
|
+
? state.resolveRunId(parsed.options.before)
|
|
934
|
+
: undefined);
|
|
935
|
+
if (parsed.flags.json) {
|
|
936
|
+
emit(page, "");
|
|
937
|
+
break;
|
|
938
|
+
}
|
|
939
|
+
const names = new Map();
|
|
940
|
+
for (const pin of state.pins())
|
|
941
|
+
if (pin.status === "ready" && !names.has(pin.cid))
|
|
942
|
+
names.set(pin.cid, pin.name);
|
|
943
|
+
const reference = (cid) => cid === null
|
|
944
|
+
? "none"
|
|
945
|
+
: cid + (names.has(cid) ? ` (${names.get(cid)})` : "");
|
|
946
|
+
const shortId = state.shortRunId.bind(state);
|
|
947
|
+
const rows = page.items.length
|
|
948
|
+
? page.items
|
|
949
|
+
.map((record) => [
|
|
950
|
+
`run ${record.id}`,
|
|
951
|
+
` Status: ${record.status}`,
|
|
952
|
+
` Started: ${runTime(record.startedAt, "start")}`,
|
|
953
|
+
` Ended: ${runTime(record.endedAt, "end")}`,
|
|
954
|
+
` Duration: ${runDuration(record)}`,
|
|
955
|
+
` App: ${reference(record.appCid)}`,
|
|
956
|
+
` Output: ${reference(record.outputCid)}`,
|
|
957
|
+
` Argv: ${record.argv.join(" ")}`,
|
|
958
|
+
]
|
|
959
|
+
.map(printable)
|
|
960
|
+
.join("\n"))
|
|
961
|
+
.join("\n\n")
|
|
962
|
+
: "No ended executions.";
|
|
963
|
+
emit(page, rows +
|
|
964
|
+
(page.nextBefore
|
|
965
|
+
? `\nMore matches; use --before ${shortId(page.nextBefore)} with the same filters.`
|
|
966
|
+
: ""));
|
|
967
|
+
break;
|
|
968
|
+
}
|
|
969
|
+
case "history show": {
|
|
970
|
+
const record = state.run(state.resolveRunId(first));
|
|
971
|
+
const logs = record.log
|
|
972
|
+
? await logFiles(state.directory, record.id)
|
|
973
|
+
: undefined;
|
|
974
|
+
emit(logs ? { ...record, logs } : record, runDetails(record) +
|
|
975
|
+
(logs
|
|
976
|
+
? `\nLog recording: ${record.log}\nStdout log: ${printable(logs.stdout ?? "unavailable")}\nStderr log: ${printable(logs.stderr ?? "unavailable")}`
|
|
977
|
+
: "\nLog recording: disabled"));
|
|
978
|
+
break;
|
|
979
|
+
}
|
|
980
|
+
case "history remove": {
|
|
981
|
+
const ids = parsed.positional.length
|
|
982
|
+
? [...new Set(parsed.positional.map(state.resolveRunId.bind(state)))]
|
|
983
|
+
: selectedIds(state, filter);
|
|
984
|
+
for (const id of ids)
|
|
985
|
+
state.run(id);
|
|
986
|
+
if (ids.length &&
|
|
987
|
+
!parsed.options.yes &&
|
|
988
|
+
!(await io.confirm(`Remove ${ids.length} ended records selected by ${JSON.stringify(parsed.positional.length ? { ids } : parsed.options)}? Release only their retention; no GC or disk reclamation is guaranteed.`)))
|
|
989
|
+
throw new HostError("History removal cancelled.");
|
|
990
|
+
const result = await removeHistory(state, ids);
|
|
991
|
+
emit(result, `Removed ${result.removed.length} records; garbage collection is not guaranteed.`);
|
|
992
|
+
break;
|
|
993
|
+
}
|
|
994
|
+
case "exec":
|
|
995
|
+
case "open": {
|
|
996
|
+
if (parsed.options.log && !value("history.enabled"))
|
|
997
|
+
throw new HostError("Execution logging requires history.enabled=true.");
|
|
998
|
+
const target = state.resolve(first);
|
|
999
|
+
await recoverInactive(state);
|
|
1000
|
+
store = new IpfsStore(await storeConfiguration(value("store.rpc")));
|
|
1001
|
+
let appCid;
|
|
1002
|
+
let appArgv;
|
|
1003
|
+
if (parsed.path === "open") {
|
|
1004
|
+
const root = target;
|
|
1005
|
+
const owner = await Ownership.create(state, store, `open:${randomUUID()}`, null);
|
|
1006
|
+
try {
|
|
1007
|
+
const content = await validatedBundle(root, store, owner);
|
|
1008
|
+
const schema = content.schemaKey;
|
|
1009
|
+
const association = state.association(schema);
|
|
1010
|
+
if (!association)
|
|
1011
|
+
throw new HostError(`No App registered for Schema ${schema}. Use ipab register set or ipab exec.`);
|
|
1012
|
+
appCid = association.appCid;
|
|
1013
|
+
appArgv = association.argv.map((argument) => argument === "%" ? root : argument);
|
|
1014
|
+
// execute owns the App/input context before this validation owner's eventual release.
|
|
1015
|
+
const result = await execute(state, store, { origin: "open", appCid, argv: appArgv }, {
|
|
1016
|
+
inputSchema: schema,
|
|
1017
|
+
history: Boolean(value("history.enabled")),
|
|
1018
|
+
retain: value("output.retention") === "history",
|
|
1019
|
+
log: Boolean(parsed.options.log),
|
|
1020
|
+
diagnostic: (text) => {
|
|
1021
|
+
io.err(text);
|
|
1022
|
+
},
|
|
1023
|
+
});
|
|
1024
|
+
appExitCode = result.exitCode;
|
|
1025
|
+
return appExitCode;
|
|
1026
|
+
}
|
|
1027
|
+
finally {
|
|
1028
|
+
await owner.release();
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
else {
|
|
1032
|
+
appCid = target;
|
|
1033
|
+
appArgv = parsed.positional.slice(1);
|
|
1034
|
+
}
|
|
1035
|
+
appExitCode = (await execute(state, store, { origin: "exec", appCid, argv: appArgv }, {
|
|
1036
|
+
history: Boolean(value("history.enabled")),
|
|
1037
|
+
retain: value("output.retention") === "history",
|
|
1038
|
+
log: Boolean(parsed.options.log),
|
|
1039
|
+
diagnostic: (text) => {
|
|
1040
|
+
io.err(text);
|
|
1041
|
+
},
|
|
1042
|
+
})).exitCode;
|
|
1043
|
+
return appExitCode;
|
|
1044
|
+
}
|
|
1045
|
+
default:
|
|
1046
|
+
syntax("Unknown command.");
|
|
1047
|
+
}
|
|
1048
|
+
return 0;
|
|
1049
|
+
}
|
|
1050
|
+
catch (error) {
|
|
1051
|
+
io.err(`Error: ${message(error)}`);
|
|
1052
|
+
return error instanceof HostError ? error.exitCode : 1;
|
|
1053
|
+
}
|
|
1054
|
+
finally {
|
|
1055
|
+
try {
|
|
1056
|
+
await closeStore(store, appExitCode);
|
|
1057
|
+
}
|
|
1058
|
+
finally {
|
|
1059
|
+
state?.close();
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
export async function runCli(argv, io = processIO, env = process.env) {
|
|
1064
|
+
try {
|
|
1065
|
+
return await performCli(argv, io, env);
|
|
1066
|
+
}
|
|
1067
|
+
catch (error) {
|
|
1068
|
+
io.err(`Error: ${message(error)}`);
|
|
1069
|
+
return error instanceof HostError ? error.exitCode : 1;
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
//# sourceMappingURL=cli.js.map
|