@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,616 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { lstat, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { isBuiltin } from "node:module";
|
|
4
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
5
|
+
import { StringDecoder } from "node:string_decoder";
|
|
6
|
+
import { compileFunction } from "node:vm";
|
|
7
|
+
import { stringify } from "yaml";
|
|
8
|
+
import { parseData } from "@ipab/bundle";
|
|
9
|
+
import { filesystemTree } from "@ipab/bundle-builder/node";
|
|
10
|
+
import { encodeBundleCar, memoryTree, readBytes } from "@ipab/bundle-builder";
|
|
11
|
+
import {
|
|
12
|
+
writeCar,
|
|
13
|
+
outputAvailable,
|
|
14
|
+
scaffoldFiles,
|
|
15
|
+
nodeError,
|
|
16
|
+
type AuthoringOptions,
|
|
17
|
+
type BuildResult,
|
|
18
|
+
} from "./authoring.js";
|
|
19
|
+
import { HostError, object } from "./errors.js";
|
|
20
|
+
import { relativePath } from "./space.js";
|
|
21
|
+
import {
|
|
22
|
+
buildSupportedSchemas,
|
|
23
|
+
supportedSchemasPath,
|
|
24
|
+
} from "./supported-schemas.js";
|
|
25
|
+
import {
|
|
26
|
+
appDescription,
|
|
27
|
+
appSchema,
|
|
28
|
+
builtinAppSchema,
|
|
29
|
+
} from "./app-definition.js";
|
|
30
|
+
import { appTemplate } from "./app-templates.js";
|
|
31
|
+
import { buildChain, projectBuild, runPrebuild } from "./app-prebuild.js";
|
|
32
|
+
import { captureSource, sourceFingerprint } from "./source-bundle.js";
|
|
33
|
+
|
|
34
|
+
const json = (value: unknown) => `${JSON.stringify(value, null, 2)}\n`;
|
|
35
|
+
|
|
36
|
+
export async function initApp(
|
|
37
|
+
name: string,
|
|
38
|
+
directory = name,
|
|
39
|
+
options: AuthoringOptions & {
|
|
40
|
+
template?: string;
|
|
41
|
+
parameters?: readonly string[];
|
|
42
|
+
} = {},
|
|
43
|
+
) {
|
|
44
|
+
const { files, cid: templateCid } = await appTemplate(
|
|
45
|
+
options.template ?? "node",
|
|
46
|
+
name,
|
|
47
|
+
options,
|
|
48
|
+
);
|
|
49
|
+
const root = await scaffoldFiles(directory, files, options);
|
|
50
|
+
return {
|
|
51
|
+
kind: "project-init" as const,
|
|
52
|
+
directory: root,
|
|
53
|
+
name,
|
|
54
|
+
template: options.template ?? "node",
|
|
55
|
+
templateCid,
|
|
56
|
+
files: [...files.keys()],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function within(root: string, path: string): boolean {
|
|
61
|
+
const part = relative(root, path);
|
|
62
|
+
return !isAbsolute(part) && part !== ".." && !part.startsWith(`..${sep}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function typecheck(
|
|
66
|
+
root: string,
|
|
67
|
+
entry: string,
|
|
68
|
+
options: AuthoringOptions,
|
|
69
|
+
config = "tsconfig.json",
|
|
70
|
+
) {
|
|
71
|
+
const compiler = join(root, "node_modules", "typescript", "bin", "tsc");
|
|
72
|
+
try {
|
|
73
|
+
await lstat(compiler);
|
|
74
|
+
} catch (cause) {
|
|
75
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
76
|
+
const error = new HostError(
|
|
77
|
+
"Project-local TypeScript is missing. Install this project's declared dependencies explicitly before ipab build.",
|
|
78
|
+
1,
|
|
79
|
+
"APP_BUILD_FAILED",
|
|
80
|
+
"validation",
|
|
81
|
+
);
|
|
82
|
+
error.cause = cause;
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
await new Promise<void>((accept, reject) => {
|
|
86
|
+
const child = spawn(
|
|
87
|
+
process.execPath,
|
|
88
|
+
[
|
|
89
|
+
compiler,
|
|
90
|
+
"--project",
|
|
91
|
+
join(root, ...config.split("/")),
|
|
92
|
+
"--noEmit",
|
|
93
|
+
"--incremental",
|
|
94
|
+
"false",
|
|
95
|
+
"--composite",
|
|
96
|
+
"false",
|
|
97
|
+
"--strict",
|
|
98
|
+
"--noCheck",
|
|
99
|
+
"false",
|
|
100
|
+
"--noImplicitAny",
|
|
101
|
+
"--noImplicitThis",
|
|
102
|
+
"--strictNullChecks",
|
|
103
|
+
"--strictFunctionTypes",
|
|
104
|
+
"--strictBindCallApply",
|
|
105
|
+
"--strictPropertyInitialization",
|
|
106
|
+
"--strictBuiltinIteratorReturn",
|
|
107
|
+
"--alwaysStrict",
|
|
108
|
+
"--useUnknownInCatchVariables",
|
|
109
|
+
"--pretty",
|
|
110
|
+
"false",
|
|
111
|
+
"--listFiles",
|
|
112
|
+
],
|
|
113
|
+
{ cwd: root, signal: options.signal, stdio: ["ignore", "pipe", "pipe"] },
|
|
114
|
+
);
|
|
115
|
+
let diagnostic = "";
|
|
116
|
+
let included = false;
|
|
117
|
+
const selected = resolve(root, entry);
|
|
118
|
+
const checkLine = (line: string) => {
|
|
119
|
+
const path = resolve(root, line);
|
|
120
|
+
if (
|
|
121
|
+
process.platform === "win32"
|
|
122
|
+
? path.toLowerCase() === selected.toLowerCase()
|
|
123
|
+
: path === selected
|
|
124
|
+
)
|
|
125
|
+
included = true;
|
|
126
|
+
};
|
|
127
|
+
let failure: Error | undefined;
|
|
128
|
+
const fail = (error: unknown) => {
|
|
129
|
+
failure ??=
|
|
130
|
+
error instanceof Error
|
|
131
|
+
? error
|
|
132
|
+
: new Error("App typecheck stream failed.", { cause: error });
|
|
133
|
+
child.kill();
|
|
134
|
+
};
|
|
135
|
+
for (const stream of [child.stdout, child.stderr]) {
|
|
136
|
+
const decoder = new StringDecoder("utf8");
|
|
137
|
+
let pending = "";
|
|
138
|
+
const collect = (text: string) => {
|
|
139
|
+
diagnostic = (diagnostic + text).slice(-1024 * 1024);
|
|
140
|
+
const lines = (pending + text).split(/\r?\n/);
|
|
141
|
+
pending = lines.pop()!;
|
|
142
|
+
for (const line of lines) checkLine(line);
|
|
143
|
+
};
|
|
144
|
+
stream.on("data", (chunk: Buffer) => {
|
|
145
|
+
collect(decoder.write(chunk));
|
|
146
|
+
});
|
|
147
|
+
stream.once("end", () => {
|
|
148
|
+
collect(decoder.end());
|
|
149
|
+
checkLine(pending);
|
|
150
|
+
});
|
|
151
|
+
stream.once("error", fail);
|
|
152
|
+
}
|
|
153
|
+
child.once("error", fail);
|
|
154
|
+
child.once("close", (code) => {
|
|
155
|
+
if (failure) reject(failure);
|
|
156
|
+
else if (options.signal?.aborted)
|
|
157
|
+
reject(
|
|
158
|
+
options.signal.reason instanceof Error
|
|
159
|
+
? options.signal.reason
|
|
160
|
+
: new Error("App typecheck cancelled.", {
|
|
161
|
+
cause: options.signal.reason as unknown,
|
|
162
|
+
}),
|
|
163
|
+
);
|
|
164
|
+
else if (code === 0 && included) accept();
|
|
165
|
+
else
|
|
166
|
+
reject(
|
|
167
|
+
new HostError(
|
|
168
|
+
code === 0
|
|
169
|
+
? `App entry is not included in ${config}; include the selected source before building.`
|
|
170
|
+
: `App typecheck failed (${String(code)}).\n${diagnostic
|
|
171
|
+
.split(/\r?\n/)
|
|
172
|
+
.filter(
|
|
173
|
+
(line) => line.includes("error TS") || !isAbsolute(line),
|
|
174
|
+
)
|
|
175
|
+
.join("\n")
|
|
176
|
+
.trim()}`,
|
|
177
|
+
),
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export async function buildApp(
|
|
184
|
+
directory = ".",
|
|
185
|
+
options: AuthoringOptions & {
|
|
186
|
+
output?: string;
|
|
187
|
+
outputDir?: string;
|
|
188
|
+
replace?: boolean;
|
|
189
|
+
includeSource?: boolean;
|
|
190
|
+
} = {},
|
|
191
|
+
): Promise<
|
|
192
|
+
Omit<BuildResult, "kind"> & {
|
|
193
|
+
kind: "app-bundle";
|
|
194
|
+
source?: Omit<BuildResult, "kind">;
|
|
195
|
+
}
|
|
196
|
+
> {
|
|
197
|
+
await builtinAppSchema();
|
|
198
|
+
const root = resolve(directory);
|
|
199
|
+
const stat = await lstat(root);
|
|
200
|
+
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
201
|
+
throw new HostError("App build requires a regular project directory.");
|
|
202
|
+
const chain = await buildChain(root);
|
|
203
|
+
const tree = await filesystemTree(root);
|
|
204
|
+
const configurations = await Promise.all(
|
|
205
|
+
["ipab.yaml", "ipab.json"].map(async (name) => {
|
|
206
|
+
try {
|
|
207
|
+
await lstat(join(root, name));
|
|
208
|
+
return name;
|
|
209
|
+
} catch (cause) {
|
|
210
|
+
if (nodeError(cause, "ENOENT")) return undefined;
|
|
211
|
+
throw cause;
|
|
212
|
+
}
|
|
213
|
+
}),
|
|
214
|
+
);
|
|
215
|
+
const selected = configurations.filter((name) => name !== undefined);
|
|
216
|
+
if (selected.length !== 1)
|
|
217
|
+
throw new HostError(
|
|
218
|
+
selected.length
|
|
219
|
+
? "Both ipab.yaml and legacy ipab.json exist. Keep exactly one App configuration file."
|
|
220
|
+
: "App configuration is missing. Create ipab.yaml (or keep a legacy ipab.json).",
|
|
221
|
+
);
|
|
222
|
+
const configPath = selected[0]!;
|
|
223
|
+
const config = object(
|
|
224
|
+
parseData(
|
|
225
|
+
await readBytes(tree, configPath, 64 * 1024, options),
|
|
226
|
+
configPath === "ipab.yaml" ? "application/yaml" : "application/json",
|
|
227
|
+
),
|
|
228
|
+
configPath,
|
|
229
|
+
);
|
|
230
|
+
if (
|
|
231
|
+
typeof config.entry !== "string" ||
|
|
232
|
+
!Array.isArray(config.resources) ||
|
|
233
|
+
Object.keys(config).some(
|
|
234
|
+
(key) =>
|
|
235
|
+
!["entry", "resources", "supportedSchemas", "app", "build"].includes(
|
|
236
|
+
key,
|
|
237
|
+
),
|
|
238
|
+
) ||
|
|
239
|
+
(config.supportedSchemas !== undefined &&
|
|
240
|
+
!Array.isArray(config.supportedSchemas))
|
|
241
|
+
)
|
|
242
|
+
throw new HostError(
|
|
243
|
+
`${configPath} requires entry and an explicit resources array, with optional supportedSchemas array and app metadata overrides; unknown fields are not supported.`,
|
|
244
|
+
);
|
|
245
|
+
const prebuild = projectBuild(config.build);
|
|
246
|
+
const buildOptions =
|
|
247
|
+
config.build === undefined ? {} : object(config.build, "App build");
|
|
248
|
+
const includeSource =
|
|
249
|
+
options.includeSource ?? buildOptions.includeSource !== false;
|
|
250
|
+
const entry = relativePath(config.entry);
|
|
251
|
+
if (!prebuild && (!entry.startsWith("src/") || !entry.endsWith(".ts")))
|
|
252
|
+
throw new HostError("App entry must be a .ts file under src/.");
|
|
253
|
+
if (prebuild && !entry.endsWith(".cjs"))
|
|
254
|
+
throw new HostError(
|
|
255
|
+
"Prebuilt App entry must be a relative .cjs file in IPAB_BUILD_DIR.",
|
|
256
|
+
);
|
|
257
|
+
if (!prebuild) await readBytes(tree, entry, 16 * 1024 * 1024, options);
|
|
258
|
+
const resources = new Set<string>();
|
|
259
|
+
for (const item of config.resources) {
|
|
260
|
+
if (typeof item !== "string")
|
|
261
|
+
throw new HostError(
|
|
262
|
+
"App resources must be explicit relative file paths.",
|
|
263
|
+
);
|
|
264
|
+
const path = relativePath(item);
|
|
265
|
+
if (
|
|
266
|
+
(!prebuild && !path.startsWith("assets/")) ||
|
|
267
|
+
path.split("/").some((part) => part.startsWith(".")) ||
|
|
268
|
+
path === entry ||
|
|
269
|
+
(prebuild && (path === "app.cjs" || path.startsWith("app.cjs/"))) ||
|
|
270
|
+
path.startsWith(`${entry}/`) ||
|
|
271
|
+
entry.startsWith(`${path}/`) ||
|
|
272
|
+
[...resources].some(
|
|
273
|
+
(existing) =>
|
|
274
|
+
path.startsWith(`${existing}/`) || existing.startsWith(`${path}/`),
|
|
275
|
+
) ||
|
|
276
|
+
resources.has(path)
|
|
277
|
+
)
|
|
278
|
+
throw new HostError(
|
|
279
|
+
prebuild
|
|
280
|
+
? "Prebuilt resources must be unique, non-overlapping paths separate from the entry."
|
|
281
|
+
: "Resources must be unique non-hidden files under assets/ (no globs or directories).",
|
|
282
|
+
);
|
|
283
|
+
resources.add(path);
|
|
284
|
+
}
|
|
285
|
+
const schemas = new Set<string>();
|
|
286
|
+
for (const item of (config.supportedSchemas ?? []) as unknown[]) {
|
|
287
|
+
if (typeof item !== "string")
|
|
288
|
+
throw new HostError(
|
|
289
|
+
"Supported Schemas must be explicit relative file paths.",
|
|
290
|
+
);
|
|
291
|
+
const path = relativePath(item);
|
|
292
|
+
if (
|
|
293
|
+
!path.startsWith("schemas/") ||
|
|
294
|
+
!path.endsWith(".json") ||
|
|
295
|
+
path.split("/").some((part) => part.startsWith(".")) ||
|
|
296
|
+
schemas.has(path)
|
|
297
|
+
)
|
|
298
|
+
throw new HostError(
|
|
299
|
+
"Supported Schemas must be unique non-hidden .json files under schemas/ (no globs or directories).",
|
|
300
|
+
);
|
|
301
|
+
schemas.add(path);
|
|
302
|
+
}
|
|
303
|
+
const pkg = object(
|
|
304
|
+
JSON.parse(
|
|
305
|
+
new TextDecoder("utf-8", { fatal: true }).decode(
|
|
306
|
+
await readBytes(tree, "package.json", 64 * 1024, options),
|
|
307
|
+
),
|
|
308
|
+
) as unknown,
|
|
309
|
+
"package.json",
|
|
310
|
+
);
|
|
311
|
+
const defaults: Record<string, unknown> = {};
|
|
312
|
+
for (const key of ["name", "version", "description", "author", "license"])
|
|
313
|
+
if (pkg[key] !== undefined) defaults[key] = pkg[key];
|
|
314
|
+
const overrides =
|
|
315
|
+
config.app === undefined
|
|
316
|
+
? {}
|
|
317
|
+
: object(config.app, "App metadata overrides");
|
|
318
|
+
if (Object.hasOwn(overrides, "entry"))
|
|
319
|
+
throw new HostError(
|
|
320
|
+
"App metadata cannot override the compiled program entry.",
|
|
321
|
+
);
|
|
322
|
+
if (Object.hasOwn(overrides, "supportedSchemas"))
|
|
323
|
+
throw new HostError(
|
|
324
|
+
"App metadata cannot override the generated supportedSchemas declaration.",
|
|
325
|
+
);
|
|
326
|
+
if (Object.hasOwn(overrides, "source"))
|
|
327
|
+
throw new HostError(
|
|
328
|
+
"App metadata cannot override the generated source declaration.",
|
|
329
|
+
);
|
|
330
|
+
const supportedSchemas = await buildSupportedSchemas(tree, schemas, options);
|
|
331
|
+
const description = appDescription({
|
|
332
|
+
...defaults,
|
|
333
|
+
...overrides,
|
|
334
|
+
entry: "resources/app.cjs",
|
|
335
|
+
...(supportedSchemas ? { supportedSchemas: supportedSchemasPath } : {}),
|
|
336
|
+
});
|
|
337
|
+
if (description.icon !== undefined) {
|
|
338
|
+
const icon = relativePath(description.icon);
|
|
339
|
+
if (
|
|
340
|
+
!icon.startsWith("resources/") ||
|
|
341
|
+
![...resources].some(
|
|
342
|
+
(path) =>
|
|
343
|
+
icon === `resources/${path}` ||
|
|
344
|
+
(prebuild && icon.startsWith(`resources/${path}/`)),
|
|
345
|
+
)
|
|
346
|
+
)
|
|
347
|
+
throw new HostError(
|
|
348
|
+
prebuild
|
|
349
|
+
? "App icon must reference a selected prebuilt resource file."
|
|
350
|
+
: "App icon must reference a selected resources/assets/ file.",
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
const outputDirectory = resolve(root, options.outputDir ?? "dist");
|
|
354
|
+
const output = options.output
|
|
355
|
+
? resolve(options.output)
|
|
356
|
+
: join(outputDirectory, "app.car");
|
|
357
|
+
if (
|
|
358
|
+
!output.toLowerCase().endsWith(".car") ||
|
|
359
|
+
(within(root, output) && !within(join(root, "dist"), output)) ||
|
|
360
|
+
[...resources].some((path) => within(resolve(root, path), output))
|
|
361
|
+
)
|
|
362
|
+
throw new HostError(
|
|
363
|
+
"App CAR output must end in .car and be inside project dist/ or outside the project; source files cannot be replaced.",
|
|
364
|
+
);
|
|
365
|
+
await outputAvailable(output, Boolean(options.replace));
|
|
366
|
+
const sourceOutput = `${output.slice(0, -4)}.source.car`;
|
|
367
|
+
if (includeSource)
|
|
368
|
+
await outputAvailable(sourceOutput, Boolean(options.replace));
|
|
369
|
+
let parent = dirname(output);
|
|
370
|
+
for (;;) {
|
|
371
|
+
try {
|
|
372
|
+
if ((await lstat(parent)).isSymbolicLink())
|
|
373
|
+
throw new HostError("App output cannot traverse symbolic links.");
|
|
374
|
+
} catch (cause) {
|
|
375
|
+
if (!(
|
|
376
|
+
cause instanceof Error &&
|
|
377
|
+
"code" in cause &&
|
|
378
|
+
cause.code === "ENOENT"
|
|
379
|
+
))
|
|
380
|
+
throw cause;
|
|
381
|
+
}
|
|
382
|
+
const next = dirname(parent);
|
|
383
|
+
if (next === parent) break;
|
|
384
|
+
parent = next;
|
|
385
|
+
}
|
|
386
|
+
const excludedOutputs = [output, sourceOutput, outputDirectory];
|
|
387
|
+
const sourceFiles = includeSource
|
|
388
|
+
? await captureSource(root, excludedOutputs, options)
|
|
389
|
+
: undefined;
|
|
390
|
+
if (sourceFiles) {
|
|
391
|
+
const required = [
|
|
392
|
+
configPath,
|
|
393
|
+
"package.json",
|
|
394
|
+
...(prebuild
|
|
395
|
+
? prebuild.typecheck.flatMap((check) => [check.config, check.entry])
|
|
396
|
+
: ["tsconfig.json", entry, ...resources]),
|
|
397
|
+
...schemas,
|
|
398
|
+
];
|
|
399
|
+
for (const path of required)
|
|
400
|
+
if (!sourceFiles.has(path))
|
|
401
|
+
throw new HostError(
|
|
402
|
+
`Required App build input is excluded from source: ${path}. Fix .gitignore or use --without-source.`,
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
if (prebuild) {
|
|
406
|
+
for (const check of prebuild.typecheck) {
|
|
407
|
+
await readBytes(tree, check.config, 1024 * 1024, options);
|
|
408
|
+
await readBytes(tree, check.entry, 16 * 1024 * 1024, options);
|
|
409
|
+
await typecheck(root, check.entry, options, check.config);
|
|
410
|
+
}
|
|
411
|
+
} else await typecheck(root, entry, options);
|
|
412
|
+
options.signal?.throwIfAborted();
|
|
413
|
+
const stage = await mkdtemp(join(root, ".ipab-app-"));
|
|
414
|
+
try {
|
|
415
|
+
const { build } = await import("esbuild");
|
|
416
|
+
const bundleStage = join(stage, "bundle");
|
|
417
|
+
const input = prebuild ? join(stage, "input") : root;
|
|
418
|
+
if (prebuild) {
|
|
419
|
+
await mkdir(input);
|
|
420
|
+
await runPrebuild(root, input, pkg, chain, options);
|
|
421
|
+
}
|
|
422
|
+
const inputTree = prebuild ? await filesystemTree(input) : tree;
|
|
423
|
+
const prebuiltSource = prebuild
|
|
424
|
+
? await readBytes(inputTree, entry, 8 * 1024 * 1024, options)
|
|
425
|
+
: undefined;
|
|
426
|
+
if (prebuiltSource) {
|
|
427
|
+
try {
|
|
428
|
+
const source = new TextDecoder("utf-8", { fatal: true }).decode(
|
|
429
|
+
prebuiltSource,
|
|
430
|
+
);
|
|
431
|
+
compileFunction(source.replace(/^#![^\r\n]*/, ""), [
|
|
432
|
+
"exports",
|
|
433
|
+
"require",
|
|
434
|
+
"module",
|
|
435
|
+
"__filename",
|
|
436
|
+
"__dirname",
|
|
437
|
+
]);
|
|
438
|
+
} catch (cause) {
|
|
439
|
+
const error = new HostError(
|
|
440
|
+
"Prebuilt App entry must be valid Node CommonJS; it is packaged without transformation.",
|
|
441
|
+
);
|
|
442
|
+
error.cause = cause;
|
|
443
|
+
throw error;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
await mkdir(join(bundleStage, "resources"), { recursive: true });
|
|
447
|
+
const program = await build({
|
|
448
|
+
absWorkingDir: input,
|
|
449
|
+
entryPoints: [entry],
|
|
450
|
+
outfile: join(bundleStage, "resources", "app.cjs"),
|
|
451
|
+
bundle: true,
|
|
452
|
+
platform: "node",
|
|
453
|
+
format: "cjs",
|
|
454
|
+
target: "node22.15",
|
|
455
|
+
packages: prebuild ? "external" : "bundle",
|
|
456
|
+
metafile: true,
|
|
457
|
+
write: false,
|
|
458
|
+
logLevel: "silent",
|
|
459
|
+
logOverride: {
|
|
460
|
+
"unsupported-dynamic-import": "error",
|
|
461
|
+
"unsupported-require-call": "error",
|
|
462
|
+
"indirect-require": "error",
|
|
463
|
+
},
|
|
464
|
+
});
|
|
465
|
+
if (
|
|
466
|
+
program.outputFiles.length !== 1 ||
|
|
467
|
+
program.outputFiles[0]!.contents.length > 8 * 1024 * 1024 ||
|
|
468
|
+
(prebuild && Object.keys(program.metafile.inputs).length !== 1) ||
|
|
469
|
+
Object.values(program.metafile.outputs).some((file) =>
|
|
470
|
+
file.imports.some((item) => item.external && !isBuiltin(item.path)),
|
|
471
|
+
)
|
|
472
|
+
)
|
|
473
|
+
throw new HostError(
|
|
474
|
+
"App must bundle into one self-contained .cjs of at most 8 MiB; only Node builtins may remain external.",
|
|
475
|
+
);
|
|
476
|
+
if (sourceFiles && !prebuild)
|
|
477
|
+
for (const path of Object.keys(program.metafile.inputs))
|
|
478
|
+
if (
|
|
479
|
+
!path.split(/[\\/]/).includes("node_modules") &&
|
|
480
|
+
!sourceFiles.has(
|
|
481
|
+
relative(root, resolve(root, path)).split(sep).join("/"),
|
|
482
|
+
)
|
|
483
|
+
)
|
|
484
|
+
throw new HostError(
|
|
485
|
+
`Compiled input is outside the source snapshot: ${path}. Include it in the project or use --without-source.`,
|
|
486
|
+
);
|
|
487
|
+
options.signal?.throwIfAborted();
|
|
488
|
+
const source = prebuiltSource ?? program.outputFiles[0]!.contents;
|
|
489
|
+
await writeFile(join(bundleStage, "resources", "app.cjs"), source, {
|
|
490
|
+
flag: "wx",
|
|
491
|
+
signal: options.signal,
|
|
492
|
+
});
|
|
493
|
+
await writeFile(join(bundleStage, ".bundle.yaml"), stringify(appSchema), {
|
|
494
|
+
flag: "wx",
|
|
495
|
+
signal: options.signal,
|
|
496
|
+
});
|
|
497
|
+
if (supportedSchemas)
|
|
498
|
+
await writeFile(
|
|
499
|
+
join(bundleStage, ...supportedSchemasPath.split("/")),
|
|
500
|
+
supportedSchemas,
|
|
501
|
+
{ flag: "wx", signal: options.signal },
|
|
502
|
+
);
|
|
503
|
+
let resourceCount = 0;
|
|
504
|
+
const copyFile = async (path: string) => {
|
|
505
|
+
if (++resourceCount > 4096)
|
|
506
|
+
throw new HostError("App resource file count exceeds 4096.");
|
|
507
|
+
relativePath(path);
|
|
508
|
+
if (
|
|
509
|
+
path
|
|
510
|
+
.split("/")
|
|
511
|
+
.some(
|
|
512
|
+
(part, index, parts) =>
|
|
513
|
+
part.startsWith(".") &&
|
|
514
|
+
!(
|
|
515
|
+
part === ".bundle.yaml" &&
|
|
516
|
+
index === parts.length - 1 &&
|
|
517
|
+
index > 0
|
|
518
|
+
),
|
|
519
|
+
)
|
|
520
|
+
)
|
|
521
|
+
throw new HostError("Hidden prebuilt resources are not supported.");
|
|
522
|
+
const bytes = await readBytes(inputTree, path, 16 * 1024 * 1024, options);
|
|
523
|
+
const target = join(bundleStage, "resources", ...path.split("/"));
|
|
524
|
+
await mkdir(dirname(target), { recursive: true });
|
|
525
|
+
await writeFile(target, bytes, { flag: "wx", signal: options.signal });
|
|
526
|
+
};
|
|
527
|
+
const copyDirectory = async (path: string): Promise<void> => {
|
|
528
|
+
relativePath(path);
|
|
529
|
+
if (++resourceCount > 4096)
|
|
530
|
+
throw new HostError("App resource path count exceeds 4096.");
|
|
531
|
+
if (path.split("/").some((part) => part.startsWith(".")))
|
|
532
|
+
throw new HostError("Hidden prebuilt resources are not supported.");
|
|
533
|
+
for (const item of await inputTree.list(path, options)) {
|
|
534
|
+
const child = `${path}/${item.name}`;
|
|
535
|
+
if (item.kind === "directory") await copyDirectory(child);
|
|
536
|
+
else await copyFile(child);
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
for (const path of resources) {
|
|
540
|
+
if (
|
|
541
|
+
prebuild &&
|
|
542
|
+
(await lstat(join(input, ...path.split("/")))).isDirectory()
|
|
543
|
+
)
|
|
544
|
+
await copyDirectory(path);
|
|
545
|
+
else await copyFile(path);
|
|
546
|
+
}
|
|
547
|
+
if (description.icon !== undefined)
|
|
548
|
+
await readBytes(
|
|
549
|
+
await filesystemTree(bundleStage),
|
|
550
|
+
description.icon,
|
|
551
|
+
16 * 1024 * 1024,
|
|
552
|
+
options,
|
|
553
|
+
);
|
|
554
|
+
if (
|
|
555
|
+
sourceFiles &&
|
|
556
|
+
sourceFingerprint(sourceFiles) !==
|
|
557
|
+
sourceFingerprint(await captureSource(root, excludedOutputs, options))
|
|
558
|
+
)
|
|
559
|
+
throw new HostError(
|
|
560
|
+
"App source changed during the build. Retry with stable project inputs.",
|
|
561
|
+
);
|
|
562
|
+
const encodedSource = sourceFiles
|
|
563
|
+
? await encodeBundleCar(memoryTree(sourceFiles), options)
|
|
564
|
+
: undefined;
|
|
565
|
+
try {
|
|
566
|
+
await writeFile(
|
|
567
|
+
join(bundleStage, "app.json"),
|
|
568
|
+
json({
|
|
569
|
+
...description,
|
|
570
|
+
...(encodedSource
|
|
571
|
+
? { source: { cid: encodedSource.cid.toString() } }
|
|
572
|
+
: {}),
|
|
573
|
+
}),
|
|
574
|
+
{ flag: "wx", signal: options.signal },
|
|
575
|
+
);
|
|
576
|
+
const encoded = await encodeBundleCar(
|
|
577
|
+
await filesystemTree(bundleStage),
|
|
578
|
+
options,
|
|
579
|
+
);
|
|
580
|
+
try {
|
|
581
|
+
const source = encodedSource
|
|
582
|
+
? {
|
|
583
|
+
cid: encodedSource.cid.toString(),
|
|
584
|
+
schemaCid: encodedSource.schema.cid.toString(),
|
|
585
|
+
output: sourceOutput,
|
|
586
|
+
bytes: await writeCar(
|
|
587
|
+
encodedSource,
|
|
588
|
+
sourceOutput,
|
|
589
|
+
Boolean(options.replace),
|
|
590
|
+
options,
|
|
591
|
+
),
|
|
592
|
+
}
|
|
593
|
+
: undefined;
|
|
594
|
+
return {
|
|
595
|
+
kind: "app-bundle",
|
|
596
|
+
cid: encoded.cid.toString(),
|
|
597
|
+
schemaCid: encoded.schema.cid.toString(),
|
|
598
|
+
output,
|
|
599
|
+
bytes: await writeCar(
|
|
600
|
+
encoded,
|
|
601
|
+
output,
|
|
602
|
+
Boolean(options.replace),
|
|
603
|
+
options,
|
|
604
|
+
),
|
|
605
|
+
...(source ? { source } : {}),
|
|
606
|
+
};
|
|
607
|
+
} finally {
|
|
608
|
+
await encoded.close();
|
|
609
|
+
}
|
|
610
|
+
} finally {
|
|
611
|
+
await encodedSource?.close();
|
|
612
|
+
}
|
|
613
|
+
} finally {
|
|
614
|
+
await rm(stage, { recursive: true, force: true });
|
|
615
|
+
}
|
|
616
|
+
}
|