@hewel/zgui-cli 0.0.0-stage → 0.1.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 +202 -0
- package/NOTICE +95 -0
- package/README.md +25 -2
- package/THIRD_PARTY_NOTICES.md +37 -0
- package/inspector-bundle/app.css +138 -0
- package/inspector-bundle/app.mjs +7317 -0
- package/inspector-bundle/app.mjs.map +1 -0
- package/inspector-bundle/build-info.json +345 -0
- package/inspector-bundle/components.css +1546 -0
- package/inspector-bundle/manifest.json +31 -0
- package/package.json +60 -4
- package/src/cli.ts +488 -0
- package/src/doctor.ts +278 -0
- package/src/index.ts +17 -0
- package/src/init.ts +458 -0
- package/src/inspect.ts +160 -0
package/src/init.ts
ADDED
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { mkdir, readdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
function cliPackageVersion(): string {
|
|
6
|
+
const pkg = JSON.parse(
|
|
7
|
+
readFileSync(path.resolve(import.meta.dir, "../package.json"), "utf8"),
|
|
8
|
+
) as { version?: unknown };
|
|
9
|
+
if (typeof pkg.version !== "string" || !pkg.version.trim()) {
|
|
10
|
+
throw new Error("Unable to read @hewel/zgui-cli package version");
|
|
11
|
+
}
|
|
12
|
+
return pkg.version.trim();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function sdkDependencyRange(): string {
|
|
16
|
+
const version = cliPackageVersion();
|
|
17
|
+
return version.includes("-") ? version : `^${version}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type InitTemplate = "desktop-app" | "wayland-shell";
|
|
21
|
+
|
|
22
|
+
export interface InitApplicationOptions {
|
|
23
|
+
directory?: string;
|
|
24
|
+
template?: InitTemplate;
|
|
25
|
+
name?: string;
|
|
26
|
+
force?: boolean;
|
|
27
|
+
cwd?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface InitApplicationResult {
|
|
31
|
+
directory: string;
|
|
32
|
+
name: string;
|
|
33
|
+
template: InitTemplate;
|
|
34
|
+
files: string[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function sanitizeAppName(raw: string): string {
|
|
38
|
+
const cleaned = raw
|
|
39
|
+
.trim()
|
|
40
|
+
.toLowerCase()
|
|
41
|
+
.replace(/^@[^/]+\//, "")
|
|
42
|
+
.replace(/[^a-z0-9-]+/g, "-")
|
|
43
|
+
.replace(/^-+|-+$/g, "");
|
|
44
|
+
if (!cleaned || !/^[a-z]/.test(cleaned)) {
|
|
45
|
+
return `zgui-${cleaned || "app"}`;
|
|
46
|
+
}
|
|
47
|
+
return cleaned;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function titleCase(slug: string): string {
|
|
51
|
+
return slug
|
|
52
|
+
.split("-")
|
|
53
|
+
.filter(Boolean)
|
|
54
|
+
.map(part => `${part[0]!.toUpperCase()}${part.slice(1)}`)
|
|
55
|
+
.join(" ");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function desktopAppFiles(name: string): Record<string, string> {
|
|
59
|
+
const title = titleCase(name);
|
|
60
|
+
const sdkRange = sdkDependencyRange();
|
|
61
|
+
return {
|
|
62
|
+
"package.json": `${JSON.stringify(
|
|
63
|
+
{
|
|
64
|
+
name,
|
|
65
|
+
version: "0.1.0",
|
|
66
|
+
private: true,
|
|
67
|
+
type: "module",
|
|
68
|
+
scripts: {
|
|
69
|
+
dev: "zgui-js dev",
|
|
70
|
+
build: "zgui-js build",
|
|
71
|
+
preview: "zgui-js preview",
|
|
72
|
+
package: "zgui-js package",
|
|
73
|
+
doctor: "zgui-js doctor",
|
|
74
|
+
typecheck: "tsrs --noEmit",
|
|
75
|
+
lint: "oxlint --deny-warnings",
|
|
76
|
+
},
|
|
77
|
+
zgui: {
|
|
78
|
+
id: `dev.example.${name}`,
|
|
79
|
+
window: {
|
|
80
|
+
title,
|
|
81
|
+
width: 960,
|
|
82
|
+
height: 640,
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
dependencies: {
|
|
86
|
+
"@hewel/zgui": sdkRange,
|
|
87
|
+
"@hewel/zgui-services": sdkRange,
|
|
88
|
+
"solid-js": "2.0.1",
|
|
89
|
+
},
|
|
90
|
+
devDependencies: {
|
|
91
|
+
"@hewel/zgui-cli": sdkRange,
|
|
92
|
+
oxlint: "1.94.0",
|
|
93
|
+
tsrs: "7.1.0-beta.tsrs2",
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
null,
|
|
97
|
+
2,
|
|
98
|
+
)}\n`,
|
|
99
|
+
"tsconfig.json": `${JSON.stringify(
|
|
100
|
+
{
|
|
101
|
+
extends: "@hewel/zgui/tsconfig.native.json",
|
|
102
|
+
include: ["src"],
|
|
103
|
+
},
|
|
104
|
+
null,
|
|
105
|
+
2,
|
|
106
|
+
)}\n`,
|
|
107
|
+
".oxlintrc.json": `${JSON.stringify(
|
|
108
|
+
{
|
|
109
|
+
extends: ["./node_modules/@hewel/zgui/oxlint/recommended.json"],
|
|
110
|
+
ignorePatterns: ["dist/**", "dist-package/**", ".zgui-generations/**"],
|
|
111
|
+
},
|
|
112
|
+
null,
|
|
113
|
+
2,
|
|
114
|
+
)}\n`,
|
|
115
|
+
"zgui.capabilities.json": "{}\n",
|
|
116
|
+
".gitignore": `node_modules/
|
|
117
|
+
dist/
|
|
118
|
+
dist-package/
|
|
119
|
+
.zgui-generations/
|
|
120
|
+
.zgui-build-status.json
|
|
121
|
+
.zgui-build-status.json.*.tmp
|
|
122
|
+
.zgui-native-status.json
|
|
123
|
+
.zgui-native-status.json.*.tmp
|
|
124
|
+
*.module.css.d.ts
|
|
125
|
+
`,
|
|
126
|
+
"src/app.css": `:root {
|
|
127
|
+
font-family: sans-serif;
|
|
128
|
+
font-size: 16px;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
box,
|
|
132
|
+
text {
|
|
133
|
+
box-sizing: border-box;
|
|
134
|
+
}
|
|
135
|
+
`,
|
|
136
|
+
"src/App.module.css": `.shell {
|
|
137
|
+
display: flex;
|
|
138
|
+
flex-direction: column;
|
|
139
|
+
gap: 16px;
|
|
140
|
+
padding: 28px;
|
|
141
|
+
width: 100%;
|
|
142
|
+
height: 100%;
|
|
143
|
+
background: var(--zui-color-background);
|
|
144
|
+
color: var(--zui-color-foreground);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.card {
|
|
148
|
+
display: flex;
|
|
149
|
+
flex-direction: column;
|
|
150
|
+
gap: 12px;
|
|
151
|
+
padding: 20px;
|
|
152
|
+
border-radius: 12px;
|
|
153
|
+
border: 1px solid var(--zui-color-border);
|
|
154
|
+
background: var(--zui-color-surface);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.title {
|
|
158
|
+
font-size: 24px;
|
|
159
|
+
font-weight: 700;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.subtitle {
|
|
163
|
+
font-size: 14px;
|
|
164
|
+
color: var(--zui-color-muted-foreground);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
.actions {
|
|
168
|
+
display: flex;
|
|
169
|
+
flex-direction: row;
|
|
170
|
+
gap: 10px;
|
|
171
|
+
}
|
|
172
|
+
`,
|
|
173
|
+
"src/main.tsx": `import { createSignal, renderApp, type NativeNode } from "@hewel/zgui";
|
|
174
|
+
import { Button, ThemeProvider } from "@hewel/zgui/components";
|
|
175
|
+
import styles from "./App.module.css";
|
|
176
|
+
|
|
177
|
+
export const hostApiVersion = 1;
|
|
178
|
+
|
|
179
|
+
function App() {
|
|
180
|
+
const [count, setCount] = createSignal(0);
|
|
181
|
+
|
|
182
|
+
return (
|
|
183
|
+
<ThemeProvider>
|
|
184
|
+
<box class={styles.shell}>
|
|
185
|
+
<box class={styles.card}>
|
|
186
|
+
<text class={styles.title}>${title}</text>
|
|
187
|
+
<text class={styles.subtitle}>Native desktop UI built with zgui-js + CSS Modules.</text>
|
|
188
|
+
<text data-testid="counter-value">Count: {count()}</text>
|
|
189
|
+
<box class={styles.actions}>
|
|
190
|
+
<Button data-testid="btn-inc" onClick={() => setCount(value => value + 1)}>
|
|
191
|
+
Increment
|
|
192
|
+
</Button>
|
|
193
|
+
<Button variant="outline" onClick={() => setCount(0)}>
|
|
194
|
+
Reset
|
|
195
|
+
</Button>
|
|
196
|
+
</box>
|
|
197
|
+
</box>
|
|
198
|
+
</box>
|
|
199
|
+
</ThemeProvider>
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function mount(root: NativeNode): () => void {
|
|
204
|
+
return renderApp(() => <App />, root);
|
|
205
|
+
}
|
|
206
|
+
`,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function waylandShellFiles(name: string): Record<string, string> {
|
|
211
|
+
const title = titleCase(name);
|
|
212
|
+
const sdkRange = sdkDependencyRange();
|
|
213
|
+
return {
|
|
214
|
+
"package.json": `${JSON.stringify(
|
|
215
|
+
{
|
|
216
|
+
name,
|
|
217
|
+
version: "0.1.0",
|
|
218
|
+
private: true,
|
|
219
|
+
type: "module",
|
|
220
|
+
scripts: {
|
|
221
|
+
dev: "zgui-js dev --pure-shell",
|
|
222
|
+
build: "zgui-js build",
|
|
223
|
+
preview: "zgui-js preview",
|
|
224
|
+
package: "zgui-js package --pure-shell",
|
|
225
|
+
doctor: "zgui-js doctor",
|
|
226
|
+
typecheck: "tsrs --noEmit",
|
|
227
|
+
lint: "oxlint --deny-warnings",
|
|
228
|
+
},
|
|
229
|
+
zgui: {
|
|
230
|
+
id: `dev.example.${name}`,
|
|
231
|
+
capabilities: {
|
|
232
|
+
shellSurfaces: true,
|
|
233
|
+
controlChannel: true,
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
dependencies: {
|
|
237
|
+
"@hewel/zgui": sdkRange,
|
|
238
|
+
"@hewel/zgui-services": sdkRange,
|
|
239
|
+
"solid-js": "2.0.1",
|
|
240
|
+
},
|
|
241
|
+
devDependencies: {
|
|
242
|
+
"@hewel/zgui-cli": sdkRange,
|
|
243
|
+
oxlint: "1.94.0",
|
|
244
|
+
tsrs: "7.1.0-beta.tsrs2",
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
null,
|
|
248
|
+
2,
|
|
249
|
+
)}\n`,
|
|
250
|
+
"tsconfig.json": `${JSON.stringify(
|
|
251
|
+
{
|
|
252
|
+
extends: "@hewel/zgui/tsconfig.native.json",
|
|
253
|
+
include: ["src"],
|
|
254
|
+
},
|
|
255
|
+
null,
|
|
256
|
+
2,
|
|
257
|
+
)}\n`,
|
|
258
|
+
".oxlintrc.json": `${JSON.stringify(
|
|
259
|
+
{
|
|
260
|
+
extends: ["./node_modules/@hewel/zgui/oxlint/recommended.json"],
|
|
261
|
+
ignorePatterns: ["dist/**", "dist-package/**", ".zgui-generations/**"],
|
|
262
|
+
},
|
|
263
|
+
null,
|
|
264
|
+
2,
|
|
265
|
+
)}\n`,
|
|
266
|
+
"zgui.capabilities.json": `${JSON.stringify(
|
|
267
|
+
{
|
|
268
|
+
shellSurfaces: true,
|
|
269
|
+
controlChannel: true,
|
|
270
|
+
},
|
|
271
|
+
null,
|
|
272
|
+
2,
|
|
273
|
+
)}\n`,
|
|
274
|
+
".gitignore": `node_modules/
|
|
275
|
+
dist/
|
|
276
|
+
dist-package/
|
|
277
|
+
.zgui-generations/
|
|
278
|
+
.zgui-build-status.json
|
|
279
|
+
.zgui-build-status.json.*.tmp
|
|
280
|
+
.zgui-native-status.json
|
|
281
|
+
.zgui-native-status.json.*.tmp
|
|
282
|
+
*.module.css.d.ts
|
|
283
|
+
`,
|
|
284
|
+
"src/app.css": `:root {
|
|
285
|
+
font-family: sans-serif;
|
|
286
|
+
font-size: 14px;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
box,
|
|
290
|
+
text {
|
|
291
|
+
box-sizing: border-box;
|
|
292
|
+
}
|
|
293
|
+
`,
|
|
294
|
+
"src/App.module.css": `.bar {
|
|
295
|
+
display: flex;
|
|
296
|
+
flex-direction: row;
|
|
297
|
+
align-items: center;
|
|
298
|
+
justify-content: space-between;
|
|
299
|
+
width: 100%;
|
|
300
|
+
height: 36px;
|
|
301
|
+
padding: 0 14px;
|
|
302
|
+
background: rgba(20, 24, 34, 0.92);
|
|
303
|
+
color: #edf2fa;
|
|
304
|
+
border-bottom: 1px solid rgba(255, 255, 255, 0.12);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
.brand {
|
|
308
|
+
font-weight: 700;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
.pill {
|
|
312
|
+
padding: 4px 10px;
|
|
313
|
+
border-radius: 999px;
|
|
314
|
+
background: rgba(120, 160, 255, 0.22);
|
|
315
|
+
|
|
316
|
+
&:hover {
|
|
317
|
+
background: rgba(120, 160, 255, 0.35);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
`,
|
|
321
|
+
"src/main.tsx": `import { createSignal, LayerSurface, renderApp, type NativeNode } from "@hewel/zgui";
|
|
322
|
+
import styles from "./App.module.css";
|
|
323
|
+
|
|
324
|
+
export const hostApiVersion = 1;
|
|
325
|
+
|
|
326
|
+
function ShellBar() {
|
|
327
|
+
const [clicks, setClicks] = createSignal(0);
|
|
328
|
+
|
|
329
|
+
return (
|
|
330
|
+
<LayerSurface
|
|
331
|
+
name="${name}-bar"
|
|
332
|
+
layer="top"
|
|
333
|
+
anchor={["top", "left", "right"]}
|
|
334
|
+
height={36}
|
|
335
|
+
exclusiveZone={36}
|
|
336
|
+
>
|
|
337
|
+
<box class={styles.bar}>
|
|
338
|
+
<text class={styles.brand}>${title}</text>
|
|
339
|
+
<box
|
|
340
|
+
class={styles.pill}
|
|
341
|
+
data-testid="shell-pill"
|
|
342
|
+
onClick={() => setClicks(value => value + 1)}
|
|
343
|
+
>
|
|
344
|
+
<text>Events: {clicks()}</text>
|
|
345
|
+
</box>
|
|
346
|
+
</box>
|
|
347
|
+
</LayerSurface>
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export function mount(root: NativeNode): () => void {
|
|
352
|
+
return renderApp(() => <ShellBar />, root);
|
|
353
|
+
}
|
|
354
|
+
`,
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export async function initApplication({
|
|
359
|
+
directory,
|
|
360
|
+
template = "desktop-app",
|
|
361
|
+
name,
|
|
362
|
+
force = false,
|
|
363
|
+
cwd = process.cwd(),
|
|
364
|
+
}: InitApplicationOptions = {}): Promise<InitApplicationResult> {
|
|
365
|
+
if (template !== "desktop-app" && template !== "wayland-shell") {
|
|
366
|
+
throw new Error(
|
|
367
|
+
`Unknown template "${String(template)}". Expected "desktop-app" or "wayland-shell".`,
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
const targetDir = path.resolve(cwd, directory ?? ".");
|
|
371
|
+
if (existsSync(targetDir) && !force) {
|
|
372
|
+
const existing = await readdir(targetDir);
|
|
373
|
+
if (existing.length > 0) {
|
|
374
|
+
throw new Error(
|
|
375
|
+
`Target directory is not empty: ${targetDir} (pass --force to overwrite scaffold files)`,
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
const appName = sanitizeAppName(name ?? path.basename(targetDir));
|
|
380
|
+
const templates =
|
|
381
|
+
template === "wayland-shell" ? waylandShellFiles(appName) : desktopAppFiles(appName);
|
|
382
|
+
|
|
383
|
+
await mkdir(targetDir, { recursive: true });
|
|
384
|
+
const written: string[] = [];
|
|
385
|
+
for (const [relativePath, content] of Object.entries(templates)) {
|
|
386
|
+
const fullPath = path.join(targetDir, relativePath);
|
|
387
|
+
await mkdir(path.dirname(fullPath), { recursive: true });
|
|
388
|
+
await writeFile(fullPath, content, "utf8");
|
|
389
|
+
written.push(relativePath);
|
|
390
|
+
}
|
|
391
|
+
return {
|
|
392
|
+
directory: targetDir,
|
|
393
|
+
name: appName,
|
|
394
|
+
template,
|
|
395
|
+
files: written.sort(),
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const initUsage = `Usage: zgui-js init [DIRECTORY] [options]
|
|
400
|
+
create-zgui-js [DIRECTORY] [options]
|
|
401
|
+
|
|
402
|
+
Options:
|
|
403
|
+
--template <desktop-app|wayland-shell> Project template (default: desktop-app)
|
|
404
|
+
--name <APP_NAME> Override package/app name
|
|
405
|
+
--force Allow scaffolding into a non-empty directory
|
|
406
|
+
-h, --help Show this help message`;
|
|
407
|
+
|
|
408
|
+
export async function runInitCli(
|
|
409
|
+
args: readonly string[],
|
|
410
|
+
{ cwd = process.cwd(), log = (line: string) => console.log(line) } = {},
|
|
411
|
+
): Promise<number> {
|
|
412
|
+
if (args.includes("-h") || args.includes("--help")) {
|
|
413
|
+
log(initUsage);
|
|
414
|
+
return 0;
|
|
415
|
+
}
|
|
416
|
+
const remaining = [...args];
|
|
417
|
+
let directory: string | undefined;
|
|
418
|
+
let template: InitTemplate = "desktop-app";
|
|
419
|
+
let name: string | undefined;
|
|
420
|
+
let force = false;
|
|
421
|
+
|
|
422
|
+
while (remaining.length > 0) {
|
|
423
|
+
const arg = remaining.shift()!;
|
|
424
|
+
if (arg === "--") continue;
|
|
425
|
+
if (!arg.startsWith("-")) {
|
|
426
|
+
if (directory !== undefined) {
|
|
427
|
+
throw new Error("Specify at most one target directory");
|
|
428
|
+
}
|
|
429
|
+
directory = arg;
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
if (arg === "--force") {
|
|
433
|
+
force = true;
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
if (arg === "--template" || arg === "-t") {
|
|
437
|
+
const val = remaining.shift();
|
|
438
|
+
if (!val || val.startsWith("-")) throw new Error(`${arg} requires a template name`);
|
|
439
|
+
if (val !== "desktop-app" && val !== "wayland-shell") {
|
|
440
|
+
throw new Error(`Unknown template "${val}". Expected "desktop-app" or "wayland-shell".`);
|
|
441
|
+
}
|
|
442
|
+
template = val;
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
if (arg === "--name") {
|
|
446
|
+
const val = remaining.shift();
|
|
447
|
+
if (!val || val.startsWith("-")) throw new Error("--name requires a value");
|
|
448
|
+
name = val;
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
throw new Error(`Unknown option for init: ${arg}`);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const result = await initApplication({ directory, template, name, force, cwd });
|
|
455
|
+
log(`Scaffolded zgui-js ${result.template} "${result.name}" in ${result.directory}`);
|
|
456
|
+
log(`Created ${result.files.length} files (${result.files.join(", ")})`);
|
|
457
|
+
return 0;
|
|
458
|
+
}
|
package/src/inspect.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
buildApplication,
|
|
5
|
+
resolveApplication,
|
|
6
|
+
resolveNativeHost,
|
|
7
|
+
runNativeHost,
|
|
8
|
+
} from "@hewel/zgui-build";
|
|
9
|
+
|
|
10
|
+
export interface RunInspectOptions {
|
|
11
|
+
args: readonly string[];
|
|
12
|
+
cwd?: string;
|
|
13
|
+
hostRoot?: string;
|
|
14
|
+
hostExecutable?: string;
|
|
15
|
+
ui?: boolean;
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type InspectorSource =
|
|
20
|
+
| { readonly mode: "prebuilt"; readonly bundleDir: string }
|
|
21
|
+
| { readonly mode: "checkout"; readonly appDir: string };
|
|
22
|
+
|
|
23
|
+
export function resolveInspectorSource({
|
|
24
|
+
cliDir = import.meta.dir,
|
|
25
|
+
}: {
|
|
26
|
+
cliDir?: string;
|
|
27
|
+
} = {}): InspectorSource {
|
|
28
|
+
const resolvedCliDir = path.resolve(cliDir);
|
|
29
|
+
const packagedBundleDir = path.resolve(resolvedCliDir, "../inspector-bundle");
|
|
30
|
+
if (
|
|
31
|
+
existsSync(path.join(packagedBundleDir, "manifest.json")) &&
|
|
32
|
+
existsSync(path.join(packagedBundleDir, "app.mjs"))
|
|
33
|
+
) {
|
|
34
|
+
return { mode: "prebuilt", bundleDir: packagedBundleDir };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const checkoutRoot = path.resolve(resolvedCliDir, "../../..");
|
|
38
|
+
const checkoutInspectorDir = path.join(checkoutRoot, "apps/devtools/inspector");
|
|
39
|
+
if (
|
|
40
|
+
existsSync(path.join(checkoutRoot, "Cargo.toml")) &&
|
|
41
|
+
existsSync(path.join(checkoutInspectorDir, "package.json"))
|
|
42
|
+
) {
|
|
43
|
+
return { mode: "checkout", appDir: checkoutInspectorDir };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Missing prebuilt Inspector bundle at ${packagedBundleDir}. Reinstall @hewel/zgui-cli or run from a zgui-js checkout.`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function parseUiFlags(
|
|
52
|
+
args: readonly string[],
|
|
53
|
+
cwd: string,
|
|
54
|
+
): { socket: string; exitAfterMs?: string; width?: string; height?: string } {
|
|
55
|
+
let socket: string | undefined;
|
|
56
|
+
let exitAfterMs: string | undefined;
|
|
57
|
+
let width: string | undefined;
|
|
58
|
+
let height: string | undefined;
|
|
59
|
+
const remaining = [...args];
|
|
60
|
+
while (remaining.length > 0) {
|
|
61
|
+
const arg = remaining.shift()!;
|
|
62
|
+
if (arg === "--socket") {
|
|
63
|
+
const val = remaining.shift();
|
|
64
|
+
if (!val || val.startsWith("--")) throw new Error("--socket requires a Unix socket path");
|
|
65
|
+
socket = val;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (arg === "--exit-after-ms") {
|
|
69
|
+
const val = remaining.shift();
|
|
70
|
+
if (!val || !/^[1-9][0-9]*$/.test(val)) {
|
|
71
|
+
throw new Error("--exit-after-ms requires a positive integer");
|
|
72
|
+
}
|
|
73
|
+
exitAfterMs = val;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (arg === "--width") {
|
|
77
|
+
const val = remaining.shift();
|
|
78
|
+
if (!val || val.startsWith("--")) throw new Error("--width requires a value");
|
|
79
|
+
width = val;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (arg === "--height") {
|
|
83
|
+
const val = remaining.shift();
|
|
84
|
+
if (!val || val.startsWith("--")) throw new Error("--height requires a value");
|
|
85
|
+
height = val;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (!arg.startsWith("-") && socket === undefined) {
|
|
89
|
+
socket = arg;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
throw new Error(`Unknown option for inspect --ui: ${arg}`);
|
|
93
|
+
}
|
|
94
|
+
if (!socket) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
"zgui-js inspect --ui requires --socket <PATH> pointing to a --dev-inspect socket",
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
socket: path.resolve(cwd, socket),
|
|
101
|
+
exitAfterMs,
|
|
102
|
+
width,
|
|
103
|
+
height,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function runInspect({
|
|
108
|
+
args,
|
|
109
|
+
cwd = process.cwd(),
|
|
110
|
+
hostRoot,
|
|
111
|
+
hostExecutable,
|
|
112
|
+
ui = false,
|
|
113
|
+
signal,
|
|
114
|
+
}: RunInspectOptions): Promise<number> {
|
|
115
|
+
const resolvedCwd = path.resolve(cwd);
|
|
116
|
+
const host = await resolveNativeHost({
|
|
117
|
+
hostExecutable,
|
|
118
|
+
hostRoot,
|
|
119
|
+
cwd: resolvedCwd,
|
|
120
|
+
searchFrom: [import.meta.url],
|
|
121
|
+
signal,
|
|
122
|
+
});
|
|
123
|
+
const runRoot = hostRoot ? path.resolve(resolvedCwd, hostRoot) : (host.cargoRoot ?? resolvedCwd);
|
|
124
|
+
if (ui) {
|
|
125
|
+
const options = parseUiFlags(args, resolvedCwd);
|
|
126
|
+
const source = resolveInspectorSource();
|
|
127
|
+
let bundleDir: string;
|
|
128
|
+
if (source.mode === "prebuilt") {
|
|
129
|
+
bundleDir = source.bundleDir;
|
|
130
|
+
} else {
|
|
131
|
+
const resolvedApp = await resolveApplication(source.appDir);
|
|
132
|
+
const built = await buildApplication(resolvedApp.app, {
|
|
133
|
+
...resolvedApp,
|
|
134
|
+
development: false,
|
|
135
|
+
});
|
|
136
|
+
bundleDir = built.dist;
|
|
137
|
+
}
|
|
138
|
+
const hostArgs = [
|
|
139
|
+
"--bundle",
|
|
140
|
+
bundleDir,
|
|
141
|
+
"--title",
|
|
142
|
+
"zgui-js Inspector",
|
|
143
|
+
"--width",
|
|
144
|
+
options.width ?? "980",
|
|
145
|
+
"--height",
|
|
146
|
+
options.height ?? "680",
|
|
147
|
+
"--bind-socket",
|
|
148
|
+
`devInspect=${options.socket}`,
|
|
149
|
+
...(options.exitAfterMs ? ["--exit-after-ms", options.exitAfterMs] : []),
|
|
150
|
+
];
|
|
151
|
+
return runNativeHost(host.executable, hostArgs, {
|
|
152
|
+
root: runRoot,
|
|
153
|
+
signal,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return runNativeHost(host.executable, ["inspect", ...args], {
|
|
157
|
+
root: runRoot,
|
|
158
|
+
signal,
|
|
159
|
+
});
|
|
160
|
+
}
|