@hewel/zgui-build 0.0.0-stage → 0.1.2
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 +202 -0
- package/NOTICE +95 -0
- package/README.md +86 -2
- package/THIRD_PARTY_NOTICES.md +37 -0
- package/package.json +64 -4
- package/sdk/Cargo.lock +6814 -0
- package/sdk/LICENSE +202 -0
- package/sdk/NOTICE +95 -0
- package/sdk/THIRD_PARTY_NOTICES.md +37 -0
- package/sdk/bun.lock +539 -0
- package/sdk/package.json +60 -0
- package/sdk/preview/fixtures/empty.json +6 -0
- package/sdk/preview/fonts/NotoSans-LICENSE.txt +94 -0
- package/sdk/preview/fonts/NotoSans-Regular.ttf +0 -0
- package/sdk/preview/fonts/NotoSansCJK-LICENSE.txt +92 -0
- package/sdk/preview/fonts/NotoSansCJKsc-Regular.otf +0 -0
- package/sdk/preview/fonts/PROVENANCE.md +25 -0
- package/sdk/preview/fonts/fonts.json +60 -0
- package/sdk/preview/profiles/default.json +32 -0
- package/sdk/preview/scenarios/counter-capture.json +9 -0
- package/sdk/smoke/solid-smoke.ts +53 -0
- package/sdk/smoke/tsconfig.smoke.json +21 -0
- package/src/application.ts +70 -0
- package/src/build.ts +1097 -0
- package/src/check-smoke.ts +8 -0
- package/src/compiler.ts +701 -0
- package/src/css.ts +337 -0
- package/src/dev.ts +164 -0
- package/src/host.ts +578 -0
- package/src/identity.ts +60 -0
- package/src/index.ts +27 -0
- package/src/internal.ts +20 -0
- package/src/native-process.ts +178 -0
- package/src/package-app.ts +178 -0
- package/src/preview-check.ts +267 -0
- package/src/preview.ts +69 -0
- package/src/public-api.d.ts +265 -0
- package/src/publication.ts +108 -0
- package/src/sdk-packages.ts +376 -0
- package/src/sdk.ts +11 -0
- package/src/smoke.ts +99 -0
- package/src/sync-ui-resources.ts +171 -0
- package/src/typecheck.ts +177 -0
- package/src/validate.ts +1085 -0
- package/src/watch.ts +531 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { PrepareNativeHostOptions, RunNativeHostOptions } from "./index.ts";
|
|
2
|
+
import { spawn, type ChildProcess, type SpawnOptions } from "node:child_process";
|
|
3
|
+
import { constants } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { createInterface, type Interface } from "node:readline";
|
|
6
|
+
import { packagedSdkRoot, workspaceRoot } from "./sdk.ts";
|
|
7
|
+
|
|
8
|
+
interface ProcessOptions {
|
|
9
|
+
spawnProcess?: (
|
|
10
|
+
executable: string,
|
|
11
|
+
args: readonly string[],
|
|
12
|
+
options: SpawnOptions,
|
|
13
|
+
) => ChildProcess;
|
|
14
|
+
killAfter?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface CargoMessage {
|
|
18
|
+
reason?: string;
|
|
19
|
+
message?: { rendered?: string };
|
|
20
|
+
target?: { name?: string; kind?: string[] };
|
|
21
|
+
executable?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function errorMessage(error: unknown): string {
|
|
25
|
+
return error !== null && typeof error === "object" && "message" in error
|
|
26
|
+
? String(error.message)
|
|
27
|
+
: String(error);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function signalCode(signal: NodeJS.Signals | null) {
|
|
31
|
+
return signal ? 128 + (constants.signals[signal] ?? constants.signals.SIGTERM) : 1;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function cancellationSignal(signal?: AbortSignal) {
|
|
35
|
+
return signal?.reason === "SIGINT" ? "SIGINT" : "SIGTERM";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function failure(message: string, exitCode = 1) {
|
|
39
|
+
return Object.assign(new Error(message), { exitCode });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// A separate POSIX process group also owns cargo's compiler/linker children and native workers.
|
|
43
|
+
function ownedProcess(
|
|
44
|
+
executable: string,
|
|
45
|
+
args: readonly string[],
|
|
46
|
+
{
|
|
47
|
+
root,
|
|
48
|
+
signal,
|
|
49
|
+
onLine,
|
|
50
|
+
}: {
|
|
51
|
+
root: string;
|
|
52
|
+
signal?: AbortSignal;
|
|
53
|
+
onLine?: (line: string) => void;
|
|
54
|
+
},
|
|
55
|
+
{ spawnProcess = spawn, killAfter = 1500 }: ProcessOptions = {},
|
|
56
|
+
): Promise<number> {
|
|
57
|
+
if (signal?.aborted) return Promise.resolve(signalCode(cancellationSignal(signal)));
|
|
58
|
+
const { promise, resolve, reject } = Promise.withResolvers<number>();
|
|
59
|
+
try {
|
|
60
|
+
let child: ChildProcess;
|
|
61
|
+
let timer: NodeJS.Timeout | undefined;
|
|
62
|
+
let spawnError: unknown;
|
|
63
|
+
let lines: Interface | undefined;
|
|
64
|
+
const grouped = process.platform !== "win32";
|
|
65
|
+
const kill = (name: NodeJS.Signals) => {
|
|
66
|
+
if (!child?.pid) return;
|
|
67
|
+
try {
|
|
68
|
+
if (grouped) process.kill(-child.pid, name);
|
|
69
|
+
else child.kill(name);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (
|
|
72
|
+
!(
|
|
73
|
+
error !== null &&
|
|
74
|
+
typeof error === "object" &&
|
|
75
|
+
"code" in error &&
|
|
76
|
+
error.code === "ESRCH"
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
spawnError ??= error;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const abort = () => {
|
|
83
|
+
kill(cancellationSignal(signal));
|
|
84
|
+
timer ??= setTimeout(() => kill("SIGKILL"), killAfter);
|
|
85
|
+
};
|
|
86
|
+
try {
|
|
87
|
+
child = spawnProcess(executable, args, {
|
|
88
|
+
cwd: root,
|
|
89
|
+
detached: grouped,
|
|
90
|
+
stdio: ["inherit", onLine ? "pipe" : "inherit", "inherit"],
|
|
91
|
+
});
|
|
92
|
+
} catch (error) {
|
|
93
|
+
reject(failure(`Cannot start ${executable}: ${errorMessage(error)}`));
|
|
94
|
+
return promise;
|
|
95
|
+
}
|
|
96
|
+
child.once("error", error => {
|
|
97
|
+
spawnError = error;
|
|
98
|
+
});
|
|
99
|
+
child.once("close", (code, terminatingSignal) => {
|
|
100
|
+
clearTimeout(timer);
|
|
101
|
+
signal?.removeEventListener("abort", abort);
|
|
102
|
+
lines?.close();
|
|
103
|
+
// A leader may exit before a worker. Never leave a member of our owned group behind.
|
|
104
|
+
if (grouped) kill("SIGKILL");
|
|
105
|
+
if (spawnError) reject(failure(`Cannot run ${executable}: ${errorMessage(spawnError)}`));
|
|
106
|
+
else
|
|
107
|
+
resolve(
|
|
108
|
+
signal?.aborted
|
|
109
|
+
? signalCode(cancellationSignal(signal))
|
|
110
|
+
: (code ?? signalCode(terminatingSignal)),
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
if (onLine) {
|
|
114
|
+
lines = createInterface({ input: child.stdout! });
|
|
115
|
+
lines.on("line", onLine);
|
|
116
|
+
}
|
|
117
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
118
|
+
// Abort can arrive while spawn hooks are running, before the listener was attached.
|
|
119
|
+
if (signal?.aborted) abort();
|
|
120
|
+
} catch (error) {
|
|
121
|
+
reject(error);
|
|
122
|
+
}
|
|
123
|
+
return promise;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Build incrementally; reject with an exitCode on build/spawn/cancellation failure. */
|
|
127
|
+
export async function prepareNativeHost(
|
|
128
|
+
{ root = workspaceRoot, signal }: PrepareNativeHostOptions = {},
|
|
129
|
+
processOptions?: ProcessOptions,
|
|
130
|
+
) {
|
|
131
|
+
root = path.resolve(root);
|
|
132
|
+
if (root === packagedSdkRoot)
|
|
133
|
+
throw failure(
|
|
134
|
+
"The installed SDK has no Cargo workspace; pass root pointing to a zgui-js checkout",
|
|
135
|
+
);
|
|
136
|
+
let executable: string | undefined;
|
|
137
|
+
const exitCode = await ownedProcess(
|
|
138
|
+
"cargo",
|
|
139
|
+
["build", "-p", "zgui-host", "--locked", "--message-format=json-render-diagnostics"],
|
|
140
|
+
{
|
|
141
|
+
root,
|
|
142
|
+
signal,
|
|
143
|
+
onLine(line) {
|
|
144
|
+
let message: CargoMessage | null;
|
|
145
|
+
try {
|
|
146
|
+
message = JSON.parse(line) as CargoMessage | null;
|
|
147
|
+
} catch {
|
|
148
|
+
process.stderr.write(`${line}\n`);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (message?.reason === "compiler-message" && message.message?.rendered)
|
|
152
|
+
process.stderr.write(message.message.rendered);
|
|
153
|
+
if (
|
|
154
|
+
message?.reason === "compiler-artifact" &&
|
|
155
|
+
message.target?.name === "zgui-host" &&
|
|
156
|
+
message.target.kind?.includes("bin") &&
|
|
157
|
+
typeof message.executable === "string"
|
|
158
|
+
) {
|
|
159
|
+
executable = path.resolve(root, message.executable);
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
processOptions,
|
|
164
|
+
);
|
|
165
|
+
if (exitCode !== 0) throw failure(`zgui-host build exited with code ${exitCode}`, exitCode);
|
|
166
|
+
if (!executable) throw failure("Cargo did not report a zgui-host executable");
|
|
167
|
+
return executable;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Inherit native stdio; return its numeric status (signals use 128 + signal number). */
|
|
171
|
+
export async function runNativeHost(
|
|
172
|
+
executable: string,
|
|
173
|
+
args: readonly string[],
|
|
174
|
+
{ root = workspaceRoot, signal }: RunNativeHostOptions = {},
|
|
175
|
+
processOptions?: ProcessOptions,
|
|
176
|
+
) {
|
|
177
|
+
return ownedProcess(executable, args, { root: path.resolve(root), signal }, processOptions);
|
|
178
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { PackageApplicationOptions, PackagedApplication } from "./index.ts";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { chmod, copyFile, cp, mkdir, realpath, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { buildApplication } from "./build.ts";
|
|
6
|
+
import { EXPECTED_HOST_API_VERSION, resolveNativeHost } from "./host.ts";
|
|
7
|
+
import { packagedSdkRoot, workspaceRoot } from "./sdk.ts";
|
|
8
|
+
|
|
9
|
+
const DISTRIBUTED_LICENSE_FILES = ["LICENSE", "NOTICE", "THIRD_PARTY_NOTICES.md"] as const;
|
|
10
|
+
|
|
11
|
+
function resolveLicenseFile(filename: string, root: string): string {
|
|
12
|
+
const candidates = [
|
|
13
|
+
path.join(root, filename),
|
|
14
|
+
path.join(packagedSdkRoot, filename),
|
|
15
|
+
path.resolve(packagedSdkRoot, "..", filename),
|
|
16
|
+
];
|
|
17
|
+
for (const candidate of candidates) {
|
|
18
|
+
if (existsSync(candidate)) return candidate;
|
|
19
|
+
}
|
|
20
|
+
throw new Error(`Missing required license notice file for packaging: ${filename}`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function sanitizeDesktopField(value: string): string {
|
|
24
|
+
return value.replace(/[\r\n]+/g, " ").trim();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function buildLauncherScript(app: string, { pureShell = false }: { pureShell?: boolean }): string {
|
|
28
|
+
const extraFlags = pureShell ? " --pure-shell" : "";
|
|
29
|
+
return `#!/usr/bin/env sh
|
|
30
|
+
set -eu
|
|
31
|
+
DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
|
32
|
+
exec "$DIR/bin/zgui-host" --bundle "$DIR/bundle"${extraFlags} "$@"
|
|
33
|
+
`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function buildDesktopEntry({
|
|
37
|
+
name,
|
|
38
|
+
id,
|
|
39
|
+
execPath,
|
|
40
|
+
}: {
|
|
41
|
+
name: string;
|
|
42
|
+
id: string;
|
|
43
|
+
execPath: string;
|
|
44
|
+
}): string {
|
|
45
|
+
return `[Desktop Entry]
|
|
46
|
+
Type=Application
|
|
47
|
+
Version=1.0
|
|
48
|
+
Name=${sanitizeDesktopField(name)}
|
|
49
|
+
Exec=${sanitizeDesktopField(execPath)}
|
|
50
|
+
Terminal=false
|
|
51
|
+
Categories=Utility;
|
|
52
|
+
StartupWMClass=${sanitizeDesktopField(id)}
|
|
53
|
+
`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function packageApplication(
|
|
57
|
+
app: string,
|
|
58
|
+
{
|
|
59
|
+
root = workspaceRoot,
|
|
60
|
+
appDirectory = path.join(root, "apps/examples", app),
|
|
61
|
+
out = path.join(appDirectory, "dist-package"),
|
|
62
|
+
bytecode = false,
|
|
63
|
+
hostRoot,
|
|
64
|
+
hostExecutable,
|
|
65
|
+
hostSearchFrom,
|
|
66
|
+
extraCapabilities,
|
|
67
|
+
capabilitiesFile,
|
|
68
|
+
desktopEntry = false,
|
|
69
|
+
pureShell = false,
|
|
70
|
+
signal,
|
|
71
|
+
}: PackageApplicationOptions = {},
|
|
72
|
+
): Promise<PackagedApplication> {
|
|
73
|
+
signal?.throwIfAborted();
|
|
74
|
+
const resolvedAppDir = path.resolve(appDirectory);
|
|
75
|
+
const outputDir = path.resolve(out);
|
|
76
|
+
const { dist, manifest } = await buildApplication(app, {
|
|
77
|
+
root,
|
|
78
|
+
appDirectory: resolvedAppDir,
|
|
79
|
+
development: false,
|
|
80
|
+
bytecode,
|
|
81
|
+
hostRoot,
|
|
82
|
+
hostExecutable,
|
|
83
|
+
hostSearchFrom,
|
|
84
|
+
extraCapabilities,
|
|
85
|
+
capabilitiesFile,
|
|
86
|
+
});
|
|
87
|
+
signal?.throwIfAborted();
|
|
88
|
+
|
|
89
|
+
if (pureShell && !manifest.capabilities?.shellSurfaces && !manifest.capabilities?.sessionLock) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
"--pure-shell requires capabilities.shellSurfaces or capabilities.sessionLock in manifest",
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const host = await resolveNativeHost({
|
|
96
|
+
hostExecutable,
|
|
97
|
+
hostRoot,
|
|
98
|
+
root,
|
|
99
|
+
cwd: resolvedAppDir,
|
|
100
|
+
searchFrom: hostSearchFrom,
|
|
101
|
+
signal,
|
|
102
|
+
});
|
|
103
|
+
signal?.throwIfAborted();
|
|
104
|
+
|
|
105
|
+
const bundleSource = await realpath(dist);
|
|
106
|
+
const stagingDir = `${outputDir}.staging-${process.pid}-${Date.now()}`;
|
|
107
|
+
const binDir = path.join(stagingDir, "bin");
|
|
108
|
+
const bundleDir = path.join(stagingDir, "bundle");
|
|
109
|
+
const executablePath = path.join(binDir, "zgui-host");
|
|
110
|
+
const launcherPath = path.join(stagingDir, app);
|
|
111
|
+
const desktopFileName = `${app}.desktop`;
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
await mkdir(binDir, { recursive: true });
|
|
115
|
+
await copyFile(host.executable, executablePath);
|
|
116
|
+
await chmod(executablePath, 0o755);
|
|
117
|
+
await cp(bundleSource, bundleDir, { recursive: true, dereference: true });
|
|
118
|
+
const licensesDir = path.join(stagingDir, "licenses");
|
|
119
|
+
await mkdir(licensesDir, { recursive: true });
|
|
120
|
+
const licensePaths: string[] = [];
|
|
121
|
+
for (const filename of DISTRIBUTED_LICENSE_FILES) {
|
|
122
|
+
const source = resolveLicenseFile(filename, path.resolve(root));
|
|
123
|
+
await copyFile(source, path.join(licensesDir, filename));
|
|
124
|
+
licensePaths.push(`licenses/${filename}`);
|
|
125
|
+
}
|
|
126
|
+
await writeFile(launcherPath, buildLauncherScript(app, { pureShell }), "utf8");
|
|
127
|
+
await chmod(launcherPath, 0o755);
|
|
128
|
+
|
|
129
|
+
if (desktopEntry) {
|
|
130
|
+
const displayName = manifest.window?.title ?? app;
|
|
131
|
+
await writeFile(
|
|
132
|
+
path.join(stagingDir, desktopFileName),
|
|
133
|
+
buildDesktopEntry({
|
|
134
|
+
name: displayName,
|
|
135
|
+
id: manifest.id,
|
|
136
|
+
execPath: path.join(outputDir, app),
|
|
137
|
+
}),
|
|
138
|
+
"utf8",
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const packageManifest = {
|
|
143
|
+
schemaVersion: 1 as const,
|
|
144
|
+
app,
|
|
145
|
+
id: manifest.id,
|
|
146
|
+
buildId: manifest.buildId,
|
|
147
|
+
hostApiVersion: host.abi?.hostApiVersion ?? EXPECTED_HOST_API_VERSION,
|
|
148
|
+
hostVersion: host.abi?.version ?? "local",
|
|
149
|
+
hostSource: host.source,
|
|
150
|
+
executable: "bin/zgui-host",
|
|
151
|
+
bundle: "bundle",
|
|
152
|
+
launcher: app,
|
|
153
|
+
licenses: licensePaths,
|
|
154
|
+
...(desktopEntry ? { desktopEntry: desktopFileName } : {}),
|
|
155
|
+
};
|
|
156
|
+
await writeFile(
|
|
157
|
+
path.join(stagingDir, "package-manifest.json"),
|
|
158
|
+
`${JSON.stringify(packageManifest, null, 2)}\n`,
|
|
159
|
+
"utf8",
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
await rm(outputDir, { recursive: true, force: true });
|
|
163
|
+
await mkdir(path.dirname(outputDir), { recursive: true });
|
|
164
|
+
await cp(stagingDir, outputDir, { recursive: true });
|
|
165
|
+
} finally {
|
|
166
|
+
await rm(stagingDir, { recursive: true, force: true });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
out: outputDir,
|
|
171
|
+
launcher: path.join(outputDir, app),
|
|
172
|
+
executable: path.join(outputDir, "bin/zgui-host"),
|
|
173
|
+
bundle: path.join(outputDir, "bundle"),
|
|
174
|
+
...(desktopEntry ? { desktopEntry: path.join(outputDir, desktopFileName) } : {}),
|
|
175
|
+
host,
|
|
176
|
+
manifest,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { mkdir, rename, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
const workspaceRoot = fileURLToPath(new URL("../../../", import.meta.url));
|
|
6
|
+
const nativeHost = path.join(workspaceRoot, "target/debug/zgui-host");
|
|
7
|
+
const maxSummaryBytes = 8 * 1024;
|
|
8
|
+
const exitCodes = new Set([0, 2, 3, 4, 5, 6, 7, 8, 9, 130]);
|
|
9
|
+
const usage =
|
|
10
|
+
"Usage: bun packages/build/src/preview-check.ts --out NEW_ROOT [--baseline DIR] [--policy FILE]";
|
|
11
|
+
|
|
12
|
+
export interface PreviewError {
|
|
13
|
+
code: string;
|
|
14
|
+
message: string;
|
|
15
|
+
stage?: string;
|
|
16
|
+
details?: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type PreviewStage = "setup" | "run" | "compare";
|
|
20
|
+
|
|
21
|
+
interface PreviewStageResult {
|
|
22
|
+
status: "passed" | "failed";
|
|
23
|
+
exitCode: number;
|
|
24
|
+
result: string;
|
|
25
|
+
stdout: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface PreviewCheckResult {
|
|
29
|
+
schemaVersion: 1;
|
|
30
|
+
command: "preview-check";
|
|
31
|
+
status: "passed" | "failed";
|
|
32
|
+
exitCode: number;
|
|
33
|
+
stage: PreviewStage;
|
|
34
|
+
run: PreviewStageResult | null;
|
|
35
|
+
comparison: PreviewStageResult | null;
|
|
36
|
+
firstError: PreviewError | null;
|
|
37
|
+
cause?: PreviewError;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function properties(value: unknown): Record<string, unknown> {
|
|
41
|
+
return value !== null && (typeof value === "object" || typeof value === "function")
|
|
42
|
+
? (value as Record<string, unknown>)
|
|
43
|
+
: {};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function message(error: unknown): string {
|
|
47
|
+
return error instanceof Error ? error.message : String(error);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function failure(code: string, exitCode: number, message: string) {
|
|
51
|
+
return Object.assign(new Error(message), { code, exitCode });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function options(args: string[]) {
|
|
55
|
+
const values = new Map<string, string>();
|
|
56
|
+
for (let index = 0; index < args.length; index += 2) {
|
|
57
|
+
const flag = args[index],
|
|
58
|
+
value = args[index + 1];
|
|
59
|
+
if (
|
|
60
|
+
!["--out", "--baseline", "--policy"].includes(flag) ||
|
|
61
|
+
values.has(flag) ||
|
|
62
|
+
!value ||
|
|
63
|
+
value.startsWith("--") ||
|
|
64
|
+
value.includes("\0")
|
|
65
|
+
) {
|
|
66
|
+
throw failure("E_SCHEMA", 2, usage);
|
|
67
|
+
}
|
|
68
|
+
values.set(flag, value);
|
|
69
|
+
}
|
|
70
|
+
const out = values.get("--out");
|
|
71
|
+
if (out === undefined) throw failure("E_SCHEMA", 2, usage);
|
|
72
|
+
return {
|
|
73
|
+
out: path.resolve(out),
|
|
74
|
+
baseline: path.resolve(
|
|
75
|
+
values.get("--baseline") ?? path.join(workspaceRoot, "preview/baselines/counter"),
|
|
76
|
+
),
|
|
77
|
+
policy: path.resolve(
|
|
78
|
+
values.get("--policy") ?? path.join(workspaceRoot, "preview/comparison/strict.json"),
|
|
79
|
+
),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function execute(
|
|
84
|
+
command: "run" | "compare",
|
|
85
|
+
args: string[],
|
|
86
|
+
): {
|
|
87
|
+
result: Pick<PreviewStageResult, "status" | "exitCode"> & { firstError: unknown };
|
|
88
|
+
stdout: string;
|
|
89
|
+
} {
|
|
90
|
+
// Native stdout is a bounded protocol, never streamed alongside the gate result.
|
|
91
|
+
// Native stderr remains available live, including graphics setup diagnostics.
|
|
92
|
+
let child: Bun.SyncSubprocess<"pipe", "inherit">;
|
|
93
|
+
try {
|
|
94
|
+
child = Bun.spawnSync([nativeHost, "preview", command, ...args], {
|
|
95
|
+
cwd: workspaceRoot,
|
|
96
|
+
stdin: "ignore",
|
|
97
|
+
stdout: "pipe",
|
|
98
|
+
stderr: "inherit",
|
|
99
|
+
});
|
|
100
|
+
} catch (error) {
|
|
101
|
+
throw failure(
|
|
102
|
+
"E_WORKER_CRASH",
|
|
103
|
+
9,
|
|
104
|
+
`Cannot complete native preview ${command}: ${message(error)}`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
if (child.signalCode) {
|
|
108
|
+
throw failure(
|
|
109
|
+
child.signalCode === "SIGINT" ? "E_CANCELED" : "E_WORKER_CRASH",
|
|
110
|
+
child.signalCode === "SIGINT" ? 130 : 9,
|
|
111
|
+
`Native preview ${command} terminated by ${child.signalCode}`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
if (child.stdout.byteLength > maxSummaryBytes) {
|
|
115
|
+
throw failure(
|
|
116
|
+
"E_HOST_INVARIANT",
|
|
117
|
+
9,
|
|
118
|
+
`Native preview ${command} stdout exceeded ${maxSummaryBytes} bytes`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
const stdout = child.stdout.toString("utf8");
|
|
122
|
+
let parsed: unknown;
|
|
123
|
+
try {
|
|
124
|
+
parsed = JSON.parse(stdout);
|
|
125
|
+
} catch {
|
|
126
|
+
throw failure(
|
|
127
|
+
"E_HOST_INVARIANT",
|
|
128
|
+
9,
|
|
129
|
+
`Native preview ${command} did not return one JSON result`,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
const result = properties(parsed);
|
|
133
|
+
const status = child.exitCode === 0 ? "passed" : "failed";
|
|
134
|
+
if (
|
|
135
|
+
result.schemaVersion !== 1 ||
|
|
136
|
+
result.command !== command ||
|
|
137
|
+
!exitCodes.has(child.exitCode) ||
|
|
138
|
+
result.exitCode !== child.exitCode ||
|
|
139
|
+
result.status !== status ||
|
|
140
|
+
(child.exitCode !== 0 && typeof properties(result.firstError).code !== "string")
|
|
141
|
+
) {
|
|
142
|
+
throw failure(
|
|
143
|
+
"E_HOST_INVARIANT",
|
|
144
|
+
9,
|
|
145
|
+
`Native preview ${command} returned an inconsistent result`,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
result: { status, exitCode: child.exitCode, firstError: result.firstError },
|
|
150
|
+
stdout,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function firstError(error: unknown): PreviewError | null {
|
|
155
|
+
if (!error) return null;
|
|
156
|
+
const data = properties(error);
|
|
157
|
+
const details =
|
|
158
|
+
data.details && typeof data.details === "object" && !Array.isArray(data.details)
|
|
159
|
+
? (data.details as Record<string, unknown>)
|
|
160
|
+
: undefined;
|
|
161
|
+
return {
|
|
162
|
+
code: String(data.code).slice(0, 64),
|
|
163
|
+
message: String(data.message ?? data.code).slice(0, 512),
|
|
164
|
+
...(typeof data.stage === "string" ? { stage: data.stage.slice(0, 64) } : {}),
|
|
165
|
+
...(details ? { details } : {}),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const result: PreviewCheckResult = {
|
|
170
|
+
schemaVersion: 1,
|
|
171
|
+
command: "preview-check",
|
|
172
|
+
status: "failed",
|
|
173
|
+
exitCode: 9,
|
|
174
|
+
stage: "setup",
|
|
175
|
+
run: null,
|
|
176
|
+
comparison: null,
|
|
177
|
+
firstError: null,
|
|
178
|
+
};
|
|
179
|
+
let root: string | undefined;
|
|
180
|
+
try {
|
|
181
|
+
const selected = options(process.argv.slice(2));
|
|
182
|
+
try {
|
|
183
|
+
await mkdir(path.dirname(selected.out), { recursive: true });
|
|
184
|
+
// A new root is an exclusive claim. Never reuse even an empty directory or symlink.
|
|
185
|
+
await mkdir(selected.out);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
throw failure("E_OUTPUT_IO", 8, `Cannot claim new preview output root: ${message(error)}`);
|
|
188
|
+
}
|
|
189
|
+
root = selected.out;
|
|
190
|
+
result.stage = "run";
|
|
191
|
+
const run = execute("run", [
|
|
192
|
+
"--bundle",
|
|
193
|
+
path.join(workspaceRoot, "apps/examples/counter/dist"),
|
|
194
|
+
"--profile",
|
|
195
|
+
path.join(workspaceRoot, "preview/profiles/default.json"),
|
|
196
|
+
"--fixture",
|
|
197
|
+
path.join(workspaceRoot, "preview/fixtures/empty.json"),
|
|
198
|
+
"--scenario",
|
|
199
|
+
path.join(workspaceRoot, "preview/scenarios/counter-virtual.json"),
|
|
200
|
+
"--out",
|
|
201
|
+
path.join(root, "actual"),
|
|
202
|
+
]);
|
|
203
|
+
result.run = {
|
|
204
|
+
status: run.result.status,
|
|
205
|
+
exitCode: run.result.exitCode,
|
|
206
|
+
result: "actual/result.json",
|
|
207
|
+
stdout: "run.stdout.json",
|
|
208
|
+
};
|
|
209
|
+
result.exitCode = run.result.exitCode;
|
|
210
|
+
result.firstError = firstError(run.result.firstError);
|
|
211
|
+
await writeFile(path.join(root, "run.stdout.json"), run.stdout, { flag: "wx" });
|
|
212
|
+
if (run.result.exitCode === 0) {
|
|
213
|
+
result.stage = "compare";
|
|
214
|
+
const comparison = execute("compare", [
|
|
215
|
+
"--baseline",
|
|
216
|
+
selected.baseline,
|
|
217
|
+
"--actual",
|
|
218
|
+
path.join(root, "actual"),
|
|
219
|
+
"--policy",
|
|
220
|
+
selected.policy,
|
|
221
|
+
"--out",
|
|
222
|
+
path.join(root, "comparison"),
|
|
223
|
+
]);
|
|
224
|
+
result.comparison = {
|
|
225
|
+
status: comparison.result.status,
|
|
226
|
+
exitCode: comparison.result.exitCode,
|
|
227
|
+
result: "comparison/result.json",
|
|
228
|
+
stdout: "comparison.stdout.json",
|
|
229
|
+
};
|
|
230
|
+
result.exitCode = comparison.result.exitCode;
|
|
231
|
+
result.firstError = firstError(comparison.result.firstError);
|
|
232
|
+
await writeFile(path.join(root, "comparison.stdout.json"), comparison.stdout, { flag: "wx" });
|
|
233
|
+
}
|
|
234
|
+
result.status = result.exitCode === 0 ? "passed" : "failed";
|
|
235
|
+
} catch (error) {
|
|
236
|
+
if (result.firstError) result.cause = result.firstError;
|
|
237
|
+
result.status = "failed";
|
|
238
|
+
const data = properties(error);
|
|
239
|
+
result.exitCode = typeof data.exitCode === "number" ? data.exitCode : 8;
|
|
240
|
+
result.firstError = firstError(
|
|
241
|
+
data.code && data.exitCode ? error : failure("E_OUTPUT_IO", 8, message(error)),
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function serialize() {
|
|
246
|
+
const bytes = `${JSON.stringify(result)}\n`;
|
|
247
|
+
if (Buffer.byteLength(bytes) > maxSummaryBytes)
|
|
248
|
+
throw new Error("Preview gate summary exceeds 8 KiB");
|
|
249
|
+
return bytes;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (root) {
|
|
253
|
+
try {
|
|
254
|
+
await writeFile(path.join(root, ".result.tmp"), serialize(), { flag: "wx" });
|
|
255
|
+
await rename(path.join(root, ".result.tmp"), path.join(root, "result.json"));
|
|
256
|
+
} catch (error) {
|
|
257
|
+
const cause = result.firstError;
|
|
258
|
+
result.status = "failed";
|
|
259
|
+
result.exitCode = 8;
|
|
260
|
+
result.firstError = firstError(
|
|
261
|
+
failure("E_OUTPUT_IO", 8, `Cannot publish preview gate result: ${message(error)}`),
|
|
262
|
+
);
|
|
263
|
+
if (cause) result.cause = cause;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
process.stdout.write(serialize());
|
|
267
|
+
process.exitCode = result.exitCode;
|
package/src/preview.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { PreviewApplicationOptions } from "./index.ts";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { buildApplication } from "./build.ts";
|
|
6
|
+
import { devApplication } from "./dev.ts";
|
|
7
|
+
import { resolveNativeHost } from "./host.ts";
|
|
8
|
+
import { runNativeHost } from "./native-process.ts";
|
|
9
|
+
import { packagedSdkRoot, workspaceRoot } from "./sdk.ts";
|
|
10
|
+
|
|
11
|
+
export async function previewApplication(
|
|
12
|
+
app: string,
|
|
13
|
+
{
|
|
14
|
+
root = workspaceRoot,
|
|
15
|
+
hostRoot,
|
|
16
|
+
hostExecutable,
|
|
17
|
+
watch = false,
|
|
18
|
+
profile = path.join(root, "preview/profiles/default.json"),
|
|
19
|
+
fixture = path.join(root, "preview/fixtures/empty.json"),
|
|
20
|
+
scenario = path.join(root, "preview/scenarios/counter-capture.json"),
|
|
21
|
+
out = path.join(
|
|
22
|
+
path.resolve(root) === packagedSdkRoot ? tmpdir() : path.join(root, "target"),
|
|
23
|
+
`preview-${app}-${randomUUID()}`,
|
|
24
|
+
),
|
|
25
|
+
signal,
|
|
26
|
+
onOutput,
|
|
27
|
+
...buildOptions
|
|
28
|
+
}: PreviewApplicationOptions = {},
|
|
29
|
+
): Promise<number> {
|
|
30
|
+
signal?.throwIfAborted();
|
|
31
|
+
const explicitHostRoot = hostRoot === undefined ? undefined : path.resolve(hostRoot);
|
|
32
|
+
const output = path.resolve(out);
|
|
33
|
+
const args = [
|
|
34
|
+
"--profile",
|
|
35
|
+
path.resolve(profile),
|
|
36
|
+
"--fixture",
|
|
37
|
+
path.resolve(fixture),
|
|
38
|
+
"--scenario",
|
|
39
|
+
path.resolve(scenario),
|
|
40
|
+
"--out",
|
|
41
|
+
output,
|
|
42
|
+
];
|
|
43
|
+
onOutput?.(output);
|
|
44
|
+
if (watch)
|
|
45
|
+
return devApplication(app, {
|
|
46
|
+
...buildOptions,
|
|
47
|
+
root,
|
|
48
|
+
...(explicitHostRoot === undefined ? {} : { hostRoot: explicitHostRoot }),
|
|
49
|
+
hostExecutable,
|
|
50
|
+
mode: "preview",
|
|
51
|
+
hostArgs: args,
|
|
52
|
+
signal,
|
|
53
|
+
});
|
|
54
|
+
const { dist } = await buildApplication(app, { ...buildOptions, root });
|
|
55
|
+
signal?.throwIfAborted();
|
|
56
|
+
const resolvedHost = await resolveNativeHost({
|
|
57
|
+
hostExecutable,
|
|
58
|
+
hostRoot: explicitHostRoot,
|
|
59
|
+
root,
|
|
60
|
+
cwd: buildOptions.appDirectory ?? process.cwd(),
|
|
61
|
+
searchFrom: buildOptions.hostSearchFrom,
|
|
62
|
+
signal,
|
|
63
|
+
});
|
|
64
|
+
const runRoot = explicitHostRoot ?? resolvedHost.cargoRoot ?? buildOptions.appDirectory ?? root;
|
|
65
|
+
return runNativeHost(resolvedHost.executable, ["preview", "run", "--bundle", dist, ...args], {
|
|
66
|
+
root: runRoot,
|
|
67
|
+
signal,
|
|
68
|
+
});
|
|
69
|
+
}
|