@ipab/cli 0.0.0 → 0.1.0-rc.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/LICENSE +21 -0
- package/README.md +24 -1
- package/dist/app-authoring.d.ts +22 -0
- package/dist/app-authoring.d.ts.map +1 -0
- package/dist/app-authoring.js +448 -0
- package/dist/app-authoring.js.map +1 -0
- package/dist/app-definition.d.ts +103 -0
- package/dist/app-definition.d.ts.map +1 -0
- package/dist/app-definition.js +201 -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 +295 -0
- package/dist/app-templates.js.map +1 -0
- package/dist/authoring.d.ts +63 -0
- package/dist/authoring.d.ts.map +1 -0
- package/dist/authoring.js +411 -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 +41 -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 +1110 -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 +154 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +43 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +174 -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 +27 -0
- package/dist/ipc.d.ts.map +1 -0
- package/dist/ipc.js +341 -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 +165 -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 +15 -0
- package/dist/source-bundle.d.ts.map +1 -0
- package/dist/source-bundle.js +102 -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 +102 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +335 -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 +98 -0
- package/dist/template-definition.d.ts.map +1 -0
- package/dist/template-definition.js +179 -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 +60 -3
- package/src/app-authoring.ts +616 -0
- package/src/app-definition.ts +250 -0
- package/src/app-prebuild.ts +289 -0
- package/src/app-templates.ts +395 -0
- package/src/authoring.ts +577 -0
- package/src/builtins.ts +49 -0
- package/src/child.ts +32 -0
- package/src/cli.ts +1330 -0
- package/src/config.ts +221 -0
- package/src/context.ts +242 -0
- package/src/errors.ts +46 -0
- package/src/history.ts +391 -0
- package/src/ipc.ts +366 -0
- package/src/logs.ts +132 -0
- package/src/main.ts +4 -0
- package/src/pins.ts +200 -0
- package/src/runtime.ts +301 -0
- package/src/source-bundle.ts +135 -0
- package/src/space.ts +379 -0
- package/src/state.ts +430 -0
- package/src/supported-schemas.ts +151 -0
- package/src/template-definition.ts +233 -0
- package/src/template-source.ts +128 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { encodeSchema, parseData } from "@ipab/bundle";
|
|
2
|
+
import { CID } from "multiformats/cid";
|
|
3
|
+
import { HostError, object } from "./errors.js";
|
|
4
|
+
|
|
5
|
+
const parameterSchema = {
|
|
6
|
+
type: "object",
|
|
7
|
+
additionalProperties: false,
|
|
8
|
+
required: ["type"],
|
|
9
|
+
properties: {
|
|
10
|
+
type: { enum: ["string", "number", "boolean", "json"] },
|
|
11
|
+
required: { type: "boolean" },
|
|
12
|
+
default: true,
|
|
13
|
+
},
|
|
14
|
+
} as const;
|
|
15
|
+
|
|
16
|
+
export const templateSchema = {
|
|
17
|
+
name: "IPAB Project Template",
|
|
18
|
+
description: "Data-only Liquid project content bundle.",
|
|
19
|
+
entry: {
|
|
20
|
+
path: "template.yaml",
|
|
21
|
+
contentType: "application/yaml",
|
|
22
|
+
schema: {
|
|
23
|
+
$id: "urn:ipab:project-template.v1",
|
|
24
|
+
type: "object",
|
|
25
|
+
additionalProperties: false,
|
|
26
|
+
required: ["version", "root", "render", "parameters"],
|
|
27
|
+
properties: {
|
|
28
|
+
version: { enum: [1] },
|
|
29
|
+
root: { enum: ["project"] },
|
|
30
|
+
render: {
|
|
31
|
+
type: "object",
|
|
32
|
+
additionalProperties: false,
|
|
33
|
+
required: ["suffix"],
|
|
34
|
+
properties: { suffix: { enum: [".liquid"] } },
|
|
35
|
+
},
|
|
36
|
+
parameters: {
|
|
37
|
+
type: "object",
|
|
38
|
+
required: ["name", "description"],
|
|
39
|
+
additionalProperties: parameterSchema,
|
|
40
|
+
properties: {
|
|
41
|
+
name: {
|
|
42
|
+
type: "object",
|
|
43
|
+
additionalProperties: false,
|
|
44
|
+
required: ["type", "required"],
|
|
45
|
+
properties: {
|
|
46
|
+
type: { enum: ["string"] },
|
|
47
|
+
required: { enum: [true] },
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
description: {
|
|
51
|
+
type: "object",
|
|
52
|
+
additionalProperties: false,
|
|
53
|
+
required: ["type", "default"],
|
|
54
|
+
properties: {
|
|
55
|
+
type: { enum: ["string"] },
|
|
56
|
+
default: { enum: [""] },
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
} as const;
|
|
65
|
+
|
|
66
|
+
export const templateBuiltinAlias = "builtin:project-template.v1";
|
|
67
|
+
export const templateSchemaCid = CID.parse(
|
|
68
|
+
"bafyreihydmyx5qvemxlidzyc7yflwxnidllorpaq7xoxlh264ozjnalqra",
|
|
69
|
+
);
|
|
70
|
+
export async function builtinTemplateSchema() {
|
|
71
|
+
const block = await encodeSchema(templateSchema);
|
|
72
|
+
if (!block.cid.equals(templateSchemaCid))
|
|
73
|
+
throw new HostError(
|
|
74
|
+
"Builtin Template definition does not match its canonical CID.",
|
|
75
|
+
);
|
|
76
|
+
return block;
|
|
77
|
+
}
|
|
78
|
+
export interface TemplateParameter {
|
|
79
|
+
type: "string" | "number" | "boolean" | "json";
|
|
80
|
+
required?: boolean;
|
|
81
|
+
default?: unknown;
|
|
82
|
+
}
|
|
83
|
+
export interface TemplateDefinition {
|
|
84
|
+
version: 1;
|
|
85
|
+
root: "project";
|
|
86
|
+
render: { suffix: ".liquid" };
|
|
87
|
+
parameters: Record<string, TemplateParameter>;
|
|
88
|
+
}
|
|
89
|
+
function parameterName(name: string) {
|
|
90
|
+
if (
|
|
91
|
+
!/^[A-Za-z][A-Za-z0-9_]*$/.test(name) ||
|
|
92
|
+
["constructor", "prototype", "__proto__"].includes(name)
|
|
93
|
+
)
|
|
94
|
+
throw new HostError(`Invalid template parameter name: ${name}.`);
|
|
95
|
+
}
|
|
96
|
+
function jsonData(value: unknown, depth = 0): void {
|
|
97
|
+
if (depth > 32) throw new HostError("Template parameter JSON is too deep.");
|
|
98
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
99
|
+
return;
|
|
100
|
+
if (typeof value === "number" && Number.isFinite(value)) return;
|
|
101
|
+
if (Array.isArray(value)) {
|
|
102
|
+
for (const item of value) jsonData(item, depth + 1);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const item = object(value, "Template JSON parameter");
|
|
106
|
+
for (const [key, child] of Object.entries(item)) {
|
|
107
|
+
if (["constructor", "prototype", "__proto__"].includes(key))
|
|
108
|
+
throw new HostError(`Unsafe template JSON key: ${key}.`);
|
|
109
|
+
jsonData(child, depth + 1);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function parameterValue(
|
|
113
|
+
value: unknown,
|
|
114
|
+
declaration: TemplateParameter,
|
|
115
|
+
name: string,
|
|
116
|
+
) {
|
|
117
|
+
jsonData(value);
|
|
118
|
+
if (declaration.type !== "json" && typeof value !== declaration.type)
|
|
119
|
+
throw new HostError(
|
|
120
|
+
`Template parameter ${name} must be ${declaration.type}.`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
export function templateDefinition(bytes: Uint8Array): TemplateDefinition {
|
|
124
|
+
const value = object(
|
|
125
|
+
parseData(bytes, "application/yaml"),
|
|
126
|
+
"Template definition",
|
|
127
|
+
);
|
|
128
|
+
const render = object(value.render, "Template render");
|
|
129
|
+
if (
|
|
130
|
+
value.version !== 1 ||
|
|
131
|
+
value.root !== "project" ||
|
|
132
|
+
render.suffix !== ".liquid" ||
|
|
133
|
+
Object.keys(render).length !== 1 ||
|
|
134
|
+
Object.keys(value).some(
|
|
135
|
+
(key) => !["version", "root", "render", "parameters"].includes(key),
|
|
136
|
+
)
|
|
137
|
+
)
|
|
138
|
+
throw new HostError(
|
|
139
|
+
"Template requires version: 1, root: project and render.suffix: .liquid; no other options are supported.",
|
|
140
|
+
);
|
|
141
|
+
const parameters = object(value.parameters, "Template parameters");
|
|
142
|
+
if (Object.keys(parameters).length > 64)
|
|
143
|
+
throw new HostError("Template supports at most 64 parameters.");
|
|
144
|
+
for (const [name, item] of Object.entries(parameters)) {
|
|
145
|
+
parameterName(name);
|
|
146
|
+
const declaration = object(item, `Template parameter ${name}`);
|
|
147
|
+
if (
|
|
148
|
+
!["string", "number", "boolean", "json"].includes(
|
|
149
|
+
String(declaration.type),
|
|
150
|
+
) ||
|
|
151
|
+
(declaration.required !== undefined &&
|
|
152
|
+
typeof declaration.required !== "boolean") ||
|
|
153
|
+
Object.keys(declaration).some(
|
|
154
|
+
(key) => !["type", "required", "default"].includes(key),
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
throw new HostError(`Invalid template parameter declaration: ${name}.`);
|
|
158
|
+
if (Object.hasOwn(declaration, "default"))
|
|
159
|
+
parameterValue(
|
|
160
|
+
declaration.default,
|
|
161
|
+
declaration as unknown as TemplateParameter,
|
|
162
|
+
name,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
const name = object(parameters.name, "Template name parameter");
|
|
166
|
+
const description = object(
|
|
167
|
+
parameters.description,
|
|
168
|
+
"Template description parameter",
|
|
169
|
+
);
|
|
170
|
+
if (
|
|
171
|
+
name.type !== "string" ||
|
|
172
|
+
name.required !== true ||
|
|
173
|
+
Object.keys(name).length !== 2 ||
|
|
174
|
+
description.type !== "string" ||
|
|
175
|
+
description.default !== "" ||
|
|
176
|
+
Object.keys(description).length !== 2
|
|
177
|
+
)
|
|
178
|
+
throw new HostError(
|
|
179
|
+
"Template must declare required string name and string description with default ''.",
|
|
180
|
+
);
|
|
181
|
+
return value as unknown as TemplateDefinition;
|
|
182
|
+
}
|
|
183
|
+
export function templateParameters(
|
|
184
|
+
definition: TemplateDefinition,
|
|
185
|
+
name: string,
|
|
186
|
+
inputs: readonly string[] = [],
|
|
187
|
+
): Record<string, unknown> {
|
|
188
|
+
const supplied = new Map<string, unknown>([["name", name]]);
|
|
189
|
+
let bytes = Buffer.byteLength(name);
|
|
190
|
+
if (bytes > 64 * 1024)
|
|
191
|
+
throw new HostError("Template parameters exceed 64 KiB.");
|
|
192
|
+
for (const input of inputs) {
|
|
193
|
+
bytes += Buffer.byteLength(input);
|
|
194
|
+
if (bytes > 64 * 1024)
|
|
195
|
+
throw new HostError("Template parameters exceed 64 KiB.");
|
|
196
|
+
const equals = input.indexOf("=");
|
|
197
|
+
const key = input.slice(0, equals);
|
|
198
|
+
parameterName(key);
|
|
199
|
+
if (equals < 1) throw new HostError("--param requires KEY=VALUE.");
|
|
200
|
+
if (supplied.has(key))
|
|
201
|
+
throw new HostError(
|
|
202
|
+
`Duplicate template parameter: ${key}. NAME supplies name.`,
|
|
203
|
+
);
|
|
204
|
+
if (!Object.hasOwn(definition.parameters, key))
|
|
205
|
+
throw new HostError(`Unknown template parameter: ${key}.`);
|
|
206
|
+
const declaration = definition.parameters[key]!;
|
|
207
|
+
const text = input.slice(equals + 1);
|
|
208
|
+
let value: unknown = text;
|
|
209
|
+
if (declaration.type !== "string") {
|
|
210
|
+
try {
|
|
211
|
+
value = JSON.parse(text) as unknown;
|
|
212
|
+
} catch {
|
|
213
|
+
throw new HostError(
|
|
214
|
+
`Template parameter ${key} requires valid JSON (${declaration.type}).`,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
parameterValue(value, declaration, key);
|
|
219
|
+
supplied.set(key, value);
|
|
220
|
+
}
|
|
221
|
+
const values: Record<string, unknown> = {};
|
|
222
|
+
for (const [key, declaration] of Object.entries(definition.parameters)) {
|
|
223
|
+
if (supplied.has(key)) values[key] = supplied.get(key);
|
|
224
|
+
else if (Object.hasOwn(declaration, "default"))
|
|
225
|
+
values[key] = declaration.default;
|
|
226
|
+
else if (declaration.required)
|
|
227
|
+
throw new HostError(
|
|
228
|
+
`Missing template parameter: ${key}. Use --param ${key}=VALUE.`,
|
|
229
|
+
);
|
|
230
|
+
else values[key] = null;
|
|
231
|
+
}
|
|
232
|
+
return values;
|
|
233
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { lstat } from "node:fs/promises";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { CID } from "multiformats/cid";
|
|
5
|
+
import { bundleLimits, openBundle } from "@ipab/bundle";
|
|
6
|
+
import { encodeBundleCar, type ReadonlyFileTree } from "@ipab/bundle-builder";
|
|
7
|
+
import { openContent } from "@ipab/ipfs-store";
|
|
8
|
+
import { readCar, selectedTree, type AuthoringOptions } from "./authoring.js";
|
|
9
|
+
import { HostError } from "./errors.js";
|
|
10
|
+
import { builtinTemplateSchema } from "./template-definition.js";
|
|
11
|
+
|
|
12
|
+
export async function builtinTemplateSource(
|
|
13
|
+
selector: "node" | "web-app",
|
|
14
|
+
options: AuthoringOptions = {},
|
|
15
|
+
) {
|
|
16
|
+
const car = await readCar(
|
|
17
|
+
fileURLToPath(
|
|
18
|
+
new URL(`../dist/builtins/template.${selector}.car`, import.meta.url),
|
|
19
|
+
),
|
|
20
|
+
options,
|
|
21
|
+
);
|
|
22
|
+
const source = await templateSource(car.root.toString(), {
|
|
23
|
+
...options,
|
|
24
|
+
resolveCid: car.resolveCid,
|
|
25
|
+
});
|
|
26
|
+
return { ...source, blocks: car.blocks };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function templateSource(
|
|
30
|
+
source: string,
|
|
31
|
+
options: AuthoringOptions,
|
|
32
|
+
) {
|
|
33
|
+
let cid: CID | undefined;
|
|
34
|
+
try {
|
|
35
|
+
cid = CID.parse(source);
|
|
36
|
+
} catch {
|
|
37
|
+
/* A local path is not a CID. */
|
|
38
|
+
}
|
|
39
|
+
let tree: ReadonlyFileTree;
|
|
40
|
+
let identity: string;
|
|
41
|
+
const expected = (await builtinTemplateSchema()).cid.toString();
|
|
42
|
+
if (!cid && (await lstat(resolve(source))).isDirectory()) {
|
|
43
|
+
tree = await selectedTree(
|
|
44
|
+
resolve(source),
|
|
45
|
+
undefined,
|
|
46
|
+
resolve(source, "dist"),
|
|
47
|
+
options,
|
|
48
|
+
);
|
|
49
|
+
const encoded = await encodeBundleCar(tree, options);
|
|
50
|
+
try {
|
|
51
|
+
if (encoded.schema.cid.toString() !== expected)
|
|
52
|
+
throw new HostError(
|
|
53
|
+
"Template Bundle must use the exact builtin project-template type CID.",
|
|
54
|
+
);
|
|
55
|
+
identity = encoded.cid.toString();
|
|
56
|
+
} finally {
|
|
57
|
+
await encoded.close();
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
let resolver = options.resolveCid;
|
|
61
|
+
if (!cid) {
|
|
62
|
+
if (!source.toLowerCase().endsWith(".car"))
|
|
63
|
+
throw new HostError(
|
|
64
|
+
"Template source must be a Bundle directory, single-root .car or exact CID.",
|
|
65
|
+
);
|
|
66
|
+
const car = await readCar(resolve(source), options);
|
|
67
|
+
cid = car.root;
|
|
68
|
+
resolver = car.resolveCid;
|
|
69
|
+
}
|
|
70
|
+
if (!resolver)
|
|
71
|
+
throw new HostError("Template CID input requires a configured Store.");
|
|
72
|
+
const resolveCid = resolver;
|
|
73
|
+
const bundle = await openBundle(cid, { ...options, resolveCid });
|
|
74
|
+
if (bundle.schemaKey !== expected)
|
|
75
|
+
throw new HostError(
|
|
76
|
+
"Template Bundle must use the exact builtin project-template type CID.",
|
|
77
|
+
);
|
|
78
|
+
identity = cid.toV1().toString();
|
|
79
|
+
const root = identity;
|
|
80
|
+
tree = {
|
|
81
|
+
read: (path, request) => bundle.readFile(path, request),
|
|
82
|
+
async list(path, request = {}) {
|
|
83
|
+
const access = {
|
|
84
|
+
block: {
|
|
85
|
+
get: (key, readOptions) =>
|
|
86
|
+
resolveCid(CID.parse(key), readOptions ?? {}),
|
|
87
|
+
},
|
|
88
|
+
} satisfies Parameters<typeof openContent>[0];
|
|
89
|
+
const settings = {
|
|
90
|
+
...request,
|
|
91
|
+
signal:
|
|
92
|
+
request.signal ?? options.signal ?? new AbortController().signal,
|
|
93
|
+
cacheBlocks: false,
|
|
94
|
+
retainedBlocks: new Map(),
|
|
95
|
+
retentionBudget: { bytes: 0, limit: bundleLimits.bytes },
|
|
96
|
+
maxBlockBytes: bundleLimits.blockBytes,
|
|
97
|
+
maxDagBlocks: bundleLimits.blocks,
|
|
98
|
+
depth: bundleLimits.depth,
|
|
99
|
+
};
|
|
100
|
+
const node = await openContent(
|
|
101
|
+
access,
|
|
102
|
+
path ? `${root}/${path}` : root,
|
|
103
|
+
settings,
|
|
104
|
+
);
|
|
105
|
+
if (node.type !== "directory")
|
|
106
|
+
throw new HostError("Template project root must be a directory.");
|
|
107
|
+
const entries = [];
|
|
108
|
+
for await (const entry of node.entries()) {
|
|
109
|
+
request.signal?.throwIfAborted();
|
|
110
|
+
const child = await openContent(
|
|
111
|
+
access,
|
|
112
|
+
entry.cid.toString(),
|
|
113
|
+
settings,
|
|
114
|
+
);
|
|
115
|
+
entries.push({
|
|
116
|
+
name: entry.name,
|
|
117
|
+
kind:
|
|
118
|
+
child.type === "directory"
|
|
119
|
+
? ("directory" as const)
|
|
120
|
+
: ("file" as const),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return entries;
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return { tree, cid: identity };
|
|
128
|
+
}
|