@hewel/zgui-build 0.1.2 → 0.1.3
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/README.md +41 -2
- package/package.json +1 -1
- package/sdk/Cargo.lock +4 -4
- package/sdk/bun.lock +41 -7
- package/sdk/package.json +4 -3
- package/src/build.ts +64 -633
- package/src/css.ts +558 -25
- package/src/dev.ts +4 -3
- package/src/host.ts +253 -75
- package/src/index.ts +8 -2
- package/src/manifest-policy.ts +648 -0
- package/src/package-app.ts +21 -23
- package/src/preview-check.ts +14 -5
- package/src/preview.ts +16 -16
- package/src/public-api.d.ts +11 -0
- package/src/sdk-packages.ts +73 -14
- package/src/typecheck.ts +6 -2
- package/src/validate.ts +8 -4
- package/src/watch.ts +50 -3
- package/src/native-process.ts +0 -178
package/src/native-process.ts
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
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
|
-
}
|