@bake-native/core 0.0.0 → 0.1.0-rc.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 +21 -0
- package/THIRD_PARTY_NOTICES.txt +548 -0
- package/bake-prelude.js +189 -0
- package/batch-contract.json +21 -0
- package/batch-wire.js +674 -0
- package/compat.json +64 -0
- package/controls.css +108 -0
- package/determinism.js +175 -0
- package/devtools.d.ts +13 -0
- package/devtools.js +73 -0
- package/dialogs.js +59 -0
- package/direct-style.js +105 -0
- package/direct-table.js +261 -0
- package/dist/bake.mjs +2211 -0
- package/dist/build-CMsvCcZ0.mjs +1 -0
- package/dist/build-CkLSZw06.mjs +5514 -0
- package/dist/build-tools-Dxyl6XAf.mjs +80 -0
- package/dist/client-Cm7o6dQd.mjs +17544 -0
- package/dist/compiler.mjs +4 -0
- package/dist/config-B1W8gfZY.mjs +12 -0
- package/dist/dev-Cskd8Hl4.mjs +986 -0
- package/dist/direct-report-Bdda-k_Y.mjs +153 -0
- package/dist/direct-report-Du44nNWl.mjs +1 -0
- package/dist/index.d.mts +176 -0
- package/dist/index.mjs +1 -0
- package/dist/intl-ROnAp2qO.mjs +75 -0
- package/dist/jiti-stub-CKcaShwE.mjs +5 -0
- package/dist/native-views-BuzB9x5H.mjs +7849 -0
- package/dist/octane/package.json +5 -0
- package/dist/package-KkUSMoji.mjs +2784 -0
- package/dist/react/package.json +13 -0
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/dist/run-D2aPm54a.mjs +6632 -0
- package/dist/scope-8W2aIuQ9.mjs +262 -0
- package/dist/screens/BakeNativeScreen.native.ts +9 -0
- package/dist/screens/package.json +5 -0
- package/dom-exception.js +31 -0
- package/element-api.js +158 -0
- package/first-screen.js +30 -0
- package/fonts/Geist-Italic-Variable.ttf +0 -0
- package/fonts/Geist-Variable.ttf +0 -0
- package/fonts/GeistMono-Italic-Variable.ttf +0 -0
- package/fonts/GeistMono-Variable.ttf +0 -0
- package/fonts/OFL.txt +93 -0
- package/fonts/README.md +27 -0
- package/fonts/geist-italic.css +8 -0
- package/fonts/geist-mono-italic.css +8 -0
- package/fonts/geist-mono.css +8 -0
- package/fonts/geist.css +8 -0
- package/header-icons.json +53 -0
- package/host-runtime.js +508 -0
- package/image-props.js +17 -0
- package/index.d.ts +8 -0
- package/index.js +7 -0
- package/licenses/Apache-2.0.txt +73 -0
- package/locale-entry.d.ts +1 -0
- package/locale-entry.js +1 -0
- package/locale.d.ts +28 -0
- package/locale.js +124 -0
- package/markup-support.js +87 -0
- package/media-element.js +115 -0
- package/native-args-entry.js +2 -0
- package/native-args.d.ts +7 -0
- package/native-args.js +155 -0
- package/native-entry.js +1 -0
- package/native-globals.d.ts +156 -0
- package/native-scenarios.d.ts +17 -0
- package/native-scenarios.js +46 -0
- package/native-support.json +349 -0
- package/native.d.ts +52 -0
- package/native.js +237 -0
- package/package.json +142 -4
- package/page-elements.js +123 -0
- package/runtime-provides.json +412 -0
- package/safe-area.d.ts +1 -0
- package/safe-area.js +44 -0
- package/swift-ui/modifiers.js +164 -0
- package/tab-icons.js +62 -0
- package/templates/create/tabs/AGENTS.md +22 -0
- package/templates/create/tabs/README.md +43 -0
- package/templates/create/tabs/_gitignore +13 -0
- package/templates/create/tabs/android/app/build.gradle.kts +79 -0
- package/templates/create/tabs/android/app/src/main/AndroidManifest.xml +39 -0
- package/templates/create/tabs/android/app/src/main/kotlin/MainActivity.kt +35 -0
- package/templates/create/tabs/android/app/src/main/res/values/themes.xml +8 -0
- package/templates/create/tabs/android/build.gradle.kts +5 -0
- package/templates/create/tabs/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/templates/create/tabs/android/gradle/wrapper/gradle-wrapper.properties +9 -0
- package/templates/create/tabs/android/gradle.properties +3 -0
- package/templates/create/tabs/android/gradlew +248 -0
- package/templates/create/tabs/android/settings.gradle.kts +47 -0
- package/templates/create/tabs/bake.config.ts +10 -0
- package/templates/create/tabs/ios/App/App.swift +30 -0
- package/templates/create/tabs/ios/build.sh +23 -0
- package/templates/create/tabs/ios/project.yml +57 -0
- package/templates/create/tabs/macos/Package.swift +24 -0
- package/templates/create/tabs/macos/Sources/__SWIFT_NAME__/App.swift +146 -0
- package/templates/create/tabs/macos/build.sh +4 -0
- package/templates/create/tabs/package.json +23 -0
- package/templates/create/tabs/pnpm-workspace.yaml +6 -0
- package/templates/create/tabs/src/App.tsrx +92 -0
- package/templates/create/tabs/src/App.tsx +67 -0
- package/templates/create/tabs/src/styles.css +32 -0
- package/templates/create/tabs/tsconfig.json +5 -0
- package/templates/create/tabs/windows/Cargo.toml +29 -0
- package/templates/create/tabs/windows/build.rs +9 -0
- package/templates/create/tabs/windows/src/main.rs +52 -0
- package/templates/create/tabs/windows/src/shell.rs +316 -0
- package/templates/module/__NAME__.kt +20 -0
- package/templates/module/__NAME__.native.ts +24 -0
- package/templates/module/__NAME__.swift +25 -0
- package/templates/module/__NAME__.web.ts +13 -0
- package/templates/module/__NAME__Test.kt +26 -0
- package/templates/module/__NAME__Tests.swift +25 -0
- package/templates/module/__SNAKE__.rs +43 -0
- package/text.d.ts +20 -0
- package/text.js +100 -0
- package/transform-entry.js +1 -0
- package/transform.d.ts +15 -0
- package/transform.js +98 -0
- package/tsconfig.native.json +16 -0
- package/ui/scope.js +103 -0
- package/ui/swift.js +106 -0
- package/ui/ui.css +272 -0
- package/view-ref-writer.js +268 -0
- package/view-ref.d.ts +26 -0
- package/view-ref.js +85 -0
- package/views/Video.native.ts +68 -0
- package/views/generated/VideoViewAndroid.h +68 -0
- package/views/generated/VideoViewJSI.h +94 -0
- package/views/generated/VideoViewSpec.kt +113 -0
- package/views/generated/apple/VideoViewJSI/VideoViewJSIApple.cpp +40 -0
- package/views/generated/apple/VideoViewJSI/include/VideoViewJSIApple.h +28 -0
- package/views/generated/apple/VideoViewSpec/VideoViewInterop.swift +39 -0
- package/views/generated/apple/VideoViewSpec/VideoViewSpec.swift +186 -0
- package/views/generated/video_view_spec.rs +189 -0
- package/views.d.ts +24 -0
- package/views.js +45 -0
- package/virtual-window.js +35 -0
- package/visibility.js +75 -0
- package/web-crypto.js +84 -0
- package/web-fetch.js +1071 -0
- package/README.md +0 -3
|
@@ -0,0 +1,986 @@
|
|
|
1
|
+
import { execFile, spawn } from "node:child_process";
|
|
2
|
+
import { copyFileSync, createReadStream, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, watch } from "node:fs";
|
|
3
|
+
import { createServer } from "node:http";
|
|
4
|
+
import { basename, extname, join, resolve, sep } from "node:path";
|
|
5
|
+
import { performance } from "node:perf_hooks";
|
|
6
|
+
import { promisify } from "node:util";
|
|
7
|
+
import { o as buildTargets } from "./build-CkLSZw06.mjs";
|
|
8
|
+
import { I as repo, U as packagedTool } from "./package-KkUSMoji.mjs";
|
|
9
|
+
// src/devices.mjs
|
|
10
|
+
/** The booted simulator bake dev uses: `udid` when given (it must be booted), else the only booted one. */
|
|
11
|
+
function pickSimulator(devices, udid) {
|
|
12
|
+
const booted = [];
|
|
13
|
+
for (const [runtime, list] of Object.entries(devices)) for (const d of list) if (d.state === "Booted") booted.push({
|
|
14
|
+
...d,
|
|
15
|
+
runtime: runtime.replace(/^.*SimRuntime\./, "")
|
|
16
|
+
});
|
|
17
|
+
if (udid) {
|
|
18
|
+
const d = booted.find((b) => b.udid === udid);
|
|
19
|
+
if (!d) throw new Error(`simulator ${udid} is not booted (bake dev never boots one): \`xcrun simctl boot ${udid}\`, then run bake dev again`);
|
|
20
|
+
return d;
|
|
21
|
+
}
|
|
22
|
+
if (booted.length === 1) return booted[0];
|
|
23
|
+
if (!booted.length) throw new Error("no booted iOS simulator: boot one (bake dev never boots one), or pass --udid");
|
|
24
|
+
throw new Error(`several simulators are booted; pass --udid:\n ${booted.map((b) => `${b.udid} ${b.name} (${b.runtime})`).join("\n ")}`);
|
|
25
|
+
}
|
|
26
|
+
/** `adb devices` output as [{ serial, state }]. */
|
|
27
|
+
function adbDevices(text) {
|
|
28
|
+
return text.split("\n").slice(1).map((l) => l.trim().split(/\s+/)).filter((p) => p.length >= 2 && p[0]).map(([serial, state]) => ({
|
|
29
|
+
serial,
|
|
30
|
+
state
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
/** The device bake dev uses: `serial` (--serial, else $ANDROID_SERIAL) when given, else the only one adb lists. */
|
|
34
|
+
function pickAndroid(devices, serial) {
|
|
35
|
+
if (serial) {
|
|
36
|
+
const d = devices.find((x) => x.serial === serial);
|
|
37
|
+
if (!d) throw new Error(`adb does not list ${serial} (bake dev never starts an emulator)`);
|
|
38
|
+
if (d.state !== "device") throw new Error(`${serial} is ${d.state}`);
|
|
39
|
+
return d.serial;
|
|
40
|
+
}
|
|
41
|
+
const ready = devices.filter((d) => d.state === "device");
|
|
42
|
+
if (ready.length === 1) return ready[0].serial;
|
|
43
|
+
if (!ready.length) throw new Error("no Android device or emulator: start one (bake dev never starts one), or pass --serial");
|
|
44
|
+
throw new Error(`several Android devices are attached; pass --serial (or set ANDROID_SERIAL):\n ${ready.map((d) => d.serial).join("\n ")}`);
|
|
45
|
+
}
|
|
46
|
+
// src/dev.mjs
|
|
47
|
+
const IGNORED_DIRS = /* @__PURE__ */ new Set([
|
|
48
|
+
"node_modules",
|
|
49
|
+
"dist",
|
|
50
|
+
".bake",
|
|
51
|
+
".git"
|
|
52
|
+
]);
|
|
53
|
+
const NATIVE_SOURCES = /* @__PURE__ */ new Set([
|
|
54
|
+
".swift",
|
|
55
|
+
".m",
|
|
56
|
+
".mm",
|
|
57
|
+
".h",
|
|
58
|
+
".kt",
|
|
59
|
+
".java",
|
|
60
|
+
".rs",
|
|
61
|
+
".c",
|
|
62
|
+
".cc",
|
|
63
|
+
".cpp"
|
|
64
|
+
]);
|
|
65
|
+
/** Whether a changed path (relative to the root) can change the bundle. */
|
|
66
|
+
function watchedChange(rel) {
|
|
67
|
+
if (!rel) return false;
|
|
68
|
+
const parts = rel.split(/[\\/]/);
|
|
69
|
+
if (parts.some((p) => IGNORED_DIRS.has(p) || p.startsWith("."))) return false;
|
|
70
|
+
const name = parts[parts.length - 1];
|
|
71
|
+
return !(name.endsWith("~") || name.endsWith(".swp") || name.endsWith(".tmp"));
|
|
72
|
+
}
|
|
73
|
+
/** @param {import('../cli/dev.js').DevOptions} opts */
|
|
74
|
+
async function devCommand(opts) {
|
|
75
|
+
const ms = (v) => `${Math.round(v)} ms`;
|
|
76
|
+
const emit = (event) => {
|
|
77
|
+
if (opts.json) {
|
|
78
|
+
process.stdout.write(JSON.stringify(event) + "\n");
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
switch (event.event) {
|
|
82
|
+
case "ready":
|
|
83
|
+
console.log(`bake dev ${event.targets.join(", ")} ${event.root}`);
|
|
84
|
+
break;
|
|
85
|
+
case "serve":
|
|
86
|
+
console.log(` web ${event.url}`);
|
|
87
|
+
break;
|
|
88
|
+
case "change":
|
|
89
|
+
console.log(`change ${event.files.join(", ")}`);
|
|
90
|
+
break;
|
|
91
|
+
case "build": {
|
|
92
|
+
const parts = Object.entries(event.targets).map(([t, r]) => `${r.ok ? "" : "✗ "}${t} ${ms(r.ms)}`);
|
|
93
|
+
console.log(`${event.ok ? "✓" : "✗"} build ${event.build} ${parts.join(" ")}`);
|
|
94
|
+
for (const d of event.diagnostics) console.log(" " + d);
|
|
95
|
+
for (const [t, r] of Object.entries(event.targets)) for (const d of [...r.diagnostics ?? [], ...r.warnings ?? []]) console.log(` ${t}: ${d}`);
|
|
96
|
+
if (!event.ok) console.log(` the running app stays on its last good build; save a fix to reload`);
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
case "shown": {
|
|
100
|
+
const from = event.reason === "start" ? "start → screen" : "save → screen";
|
|
101
|
+
const extra = event.target === "windows" ? ` (build ${ms(event.buildMs)}, launch → first JS commit ${ms(event.launchMs)}, pid ${event.pid})` : event.launchMs != null ? ` (build ${ms(event.buildMs)}, ${event.restart ?? "restart"} → screen ${ms(event.launchMs)}${event.appMs != null ? `, of which process start → first JS commit ${ms(event.appMs)}` : ""})` : ` (build ${ms(event.buildMs)})`;
|
|
102
|
+
console.log(` ${event.target.padEnd(8)} ${from} ${ms(event.ms)}${extra}`);
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case "log":
|
|
106
|
+
console.log(` ${event.target}│ ${event.line}`);
|
|
107
|
+
break;
|
|
108
|
+
case "notice":
|
|
109
|
+
console.log(` ${event.message}`);
|
|
110
|
+
break;
|
|
111
|
+
case "exit": console.log(` ${event.target.padEnd(8)} exited (${event.code})`);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const targets = opts.targets;
|
|
115
|
+
emit({
|
|
116
|
+
event: "ready",
|
|
117
|
+
targets,
|
|
118
|
+
root: opts.root
|
|
119
|
+
});
|
|
120
|
+
rmSync(opts.out, {
|
|
121
|
+
recursive: true,
|
|
122
|
+
force: true
|
|
123
|
+
});
|
|
124
|
+
mkdirSync(opts.out, { recursive: true });
|
|
125
|
+
const runners = {};
|
|
126
|
+
try {
|
|
127
|
+
if (targets.includes("web")) runners.web = await webRunner(opts, emit);
|
|
128
|
+
if (targets.includes("windows")) runners.windows = windowsRunner(opts, emit);
|
|
129
|
+
if (targets.includes("mac")) runners.mac = macRunner(opts, emit);
|
|
130
|
+
if (targets.includes("ios")) runners.ios = await iosRunner(opts, emit);
|
|
131
|
+
if (targets.includes("android")) runners.android = await androidRunner(opts, emit);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
await Promise.allSettled(Object.values(runners).map((runner) => runner.close()));
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
/** @type {Map<string, string>} target -> the build directory its running app uses */
|
|
137
|
+
const live = /* @__PURE__ */ new Map();
|
|
138
|
+
let build = 0;
|
|
139
|
+
let pending = null;
|
|
140
|
+
let building = false;
|
|
141
|
+
let timer = null;
|
|
142
|
+
const settled = [];
|
|
143
|
+
async function round(change) {
|
|
144
|
+
const n = ++build;
|
|
145
|
+
const out = join(opts.out, String(n));
|
|
146
|
+
const t0 = performance.now();
|
|
147
|
+
const report = await buildTargets({
|
|
148
|
+
...opts,
|
|
149
|
+
out,
|
|
150
|
+
targets,
|
|
151
|
+
bake: false,
|
|
152
|
+
viewport: "800x600",
|
|
153
|
+
json: true
|
|
154
|
+
});
|
|
155
|
+
const done = performance.now();
|
|
156
|
+
const summary = {};
|
|
157
|
+
for (const [t, r] of Object.entries(report.targets)) summary[t] = {
|
|
158
|
+
ok: r.ok,
|
|
159
|
+
ms: r.ms,
|
|
160
|
+
diagnostics: r.diagnostics,
|
|
161
|
+
warnings: r.warnings,
|
|
162
|
+
errors: r.errors,
|
|
163
|
+
dir: r.dir
|
|
164
|
+
};
|
|
165
|
+
emit({
|
|
166
|
+
event: "build",
|
|
167
|
+
build: n,
|
|
168
|
+
ok: report.ok,
|
|
169
|
+
ms: done - t0,
|
|
170
|
+
targets: summary,
|
|
171
|
+
diagnostics: report.diagnostics,
|
|
172
|
+
files: change.files
|
|
173
|
+
});
|
|
174
|
+
for (const [t, r] of Object.entries(report.targets)) {
|
|
175
|
+
if (!r.ok) {
|
|
176
|
+
runners[t]?.failed?.(n, [...r.diagnostics]);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const timing = {
|
|
180
|
+
build: n,
|
|
181
|
+
saveAt: change.at,
|
|
182
|
+
reason: change.reason,
|
|
183
|
+
buildMs: done - change.at
|
|
184
|
+
};
|
|
185
|
+
live.set(t, out);
|
|
186
|
+
settled.push(runners[t].load(r.dir, timing));
|
|
187
|
+
}
|
|
188
|
+
const keep = new Set([...live.values(), out].map((d) => basename(d)));
|
|
189
|
+
for (const d of readdirSync(opts.out)) if (/^\d+$/.test(d) && !keep.has(d)) try {
|
|
190
|
+
rmSync(join(opts.out, d), {
|
|
191
|
+
recursive: true,
|
|
192
|
+
force: true
|
|
193
|
+
});
|
|
194
|
+
} catch {}
|
|
195
|
+
return report.ok;
|
|
196
|
+
}
|
|
197
|
+
async function kick() {
|
|
198
|
+
if (building || !pending) return;
|
|
199
|
+
building = true;
|
|
200
|
+
const change = pending;
|
|
201
|
+
pending = null;
|
|
202
|
+
let ok = false;
|
|
203
|
+
try {
|
|
204
|
+
ok = await round(change);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
emit({
|
|
207
|
+
event: "build",
|
|
208
|
+
build,
|
|
209
|
+
ok: false,
|
|
210
|
+
ms: 0,
|
|
211
|
+
targets: {},
|
|
212
|
+
diagnostics: [e?.message ?? String(e)],
|
|
213
|
+
files: change.files
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
building = false;
|
|
217
|
+
if (opts.once) {
|
|
218
|
+
const shown = ok ? await Promise.all(settled) : [false];
|
|
219
|
+
for (const r of Object.values(runners)) await r.close();
|
|
220
|
+
process.exit(ok && shown.every(Boolean) ? 0 : 1);
|
|
221
|
+
}
|
|
222
|
+
if (pending) schedule();
|
|
223
|
+
}
|
|
224
|
+
function schedule() {
|
|
225
|
+
clearTimeout(timer);
|
|
226
|
+
timer = setTimeout(kick, opts.debounce);
|
|
227
|
+
}
|
|
228
|
+
function changed(files) {
|
|
229
|
+
const at = performance.now();
|
|
230
|
+
if (!pending) pending = {
|
|
231
|
+
at,
|
|
232
|
+
reason: "save",
|
|
233
|
+
files: []
|
|
234
|
+
};
|
|
235
|
+
for (const f of files) if (!pending.files.includes(f)) pending.files.push(f);
|
|
236
|
+
schedule();
|
|
237
|
+
}
|
|
238
|
+
const watcher = opts.once ? null : watch(opts.root, { recursive: true }, (_type, filename) => {
|
|
239
|
+
const rel = filename ? String(filename) : "";
|
|
240
|
+
if (!watchedChange(rel)) return;
|
|
241
|
+
const file = rel.replaceAll("\\", "/");
|
|
242
|
+
try {
|
|
243
|
+
if (statSync(join(opts.root, rel)).isDirectory()) return;
|
|
244
|
+
} catch {}
|
|
245
|
+
if (NATIVE_SOURCES.has(extname(file).toLowerCase())) {
|
|
246
|
+
emit({
|
|
247
|
+
event: "notice",
|
|
248
|
+
message: `${file} is native code: rebuild the host app (bake dev reloads JS, CSS and assets)`
|
|
249
|
+
});
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (!pending || !pending.files.includes(file)) emit({
|
|
253
|
+
event: "change",
|
|
254
|
+
files: [file]
|
|
255
|
+
});
|
|
256
|
+
changed([file]);
|
|
257
|
+
});
|
|
258
|
+
const stop = async () => {
|
|
259
|
+
watcher?.close();
|
|
260
|
+
for (const r of Object.values(runners)) await r.close();
|
|
261
|
+
process.exit(0);
|
|
262
|
+
};
|
|
263
|
+
process.on("SIGINT", stop);
|
|
264
|
+
process.on("SIGTERM", stop);
|
|
265
|
+
pending = {
|
|
266
|
+
at: performance.now(),
|
|
267
|
+
reason: "start",
|
|
268
|
+
files: []
|
|
269
|
+
};
|
|
270
|
+
await kick();
|
|
271
|
+
}
|
|
272
|
+
const TYPES = {
|
|
273
|
+
".html": "text/html; charset=utf-8",
|
|
274
|
+
".js": "text/javascript",
|
|
275
|
+
".mjs": "text/javascript",
|
|
276
|
+
".css": "text/css",
|
|
277
|
+
".json": "application/json",
|
|
278
|
+
".svg": "image/svg+xml",
|
|
279
|
+
".png": "image/png",
|
|
280
|
+
".jpg": "image/jpeg",
|
|
281
|
+
".jpeg": "image/jpeg",
|
|
282
|
+
".gif": "image/gif",
|
|
283
|
+
".webp": "image/webp",
|
|
284
|
+
".avif": "image/avif",
|
|
285
|
+
".woff2": "font/woff2",
|
|
286
|
+
".woff": "font/woff",
|
|
287
|
+
".ttf": "font/ttf",
|
|
288
|
+
".otf": "font/otf"
|
|
289
|
+
};
|
|
290
|
+
/** The page side: reload after a good build, show a failed build, report the drawn screen. */
|
|
291
|
+
function client(build) {
|
|
292
|
+
return `<script>(() => {
|
|
293
|
+
const build = ${build};
|
|
294
|
+
// Opened after load: an event stream opened while the page loads keeps it loading.
|
|
295
|
+
addEventListener('load', () => {
|
|
296
|
+
const events = new EventSource('/__bake/events?build=' + build);
|
|
297
|
+
events.addEventListener('reload', () => location.reload());
|
|
298
|
+
events.addEventListener('failed', (e) => {
|
|
299
|
+
let box = document.getElementById('__bake_error');
|
|
300
|
+
if (!box) {
|
|
301
|
+
box = document.createElement('pre');
|
|
302
|
+
box.id = '__bake_error';
|
|
303
|
+
box.style.cssText = 'position:fixed;inset:auto 0 0 0;max-height:50vh;overflow:auto;margin:0;padding:12px;background:#3b0d0c;color:#fff;font:12px/1.4 ui-monospace,monospace;z-index:2147483647;white-space:pre-wrap';
|
|
304
|
+
document.body.appendChild(box);
|
|
305
|
+
}
|
|
306
|
+
box.textContent = 'bake dev: build failed, this page shows the last good build\\n\\n' + JSON.parse(e.data).join('\\n');
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
addEventListener('load', () => requestAnimationFrame(() => requestAnimationFrame(() => {
|
|
310
|
+
const root = document.getElementById('root') || document.body;
|
|
311
|
+
fetch('/__bake/shown', { method: 'POST', body: JSON.stringify({ build, text: root.textContent.slice(0, 4000) }) });
|
|
312
|
+
})));
|
|
313
|
+
})();<\/script>`;
|
|
314
|
+
}
|
|
315
|
+
async function webRunner(opts, emit) {
|
|
316
|
+
let dir = null;
|
|
317
|
+
let build = 0;
|
|
318
|
+
const waiting = /* @__PURE__ */ new Map();
|
|
319
|
+
const clients = /* @__PURE__ */ new Set();
|
|
320
|
+
const send = (event, data) => {
|
|
321
|
+
for (const res of clients) res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
|
322
|
+
};
|
|
323
|
+
const server = createServer((req, res) => {
|
|
324
|
+
const url = new URL(req.url, "http://localhost");
|
|
325
|
+
if (url.pathname === "/__bake/events") {
|
|
326
|
+
res.writeHead(200, {
|
|
327
|
+
"content-type": "text/event-stream",
|
|
328
|
+
"cache-control": "no-store",
|
|
329
|
+
connection: "keep-alive"
|
|
330
|
+
});
|
|
331
|
+
res.write(": bake dev\n\n");
|
|
332
|
+
clients.add(res);
|
|
333
|
+
if (Number(url.searchParams.get("build")) < build) res.write("event: reload\ndata: {}\n\n");
|
|
334
|
+
req.on("close", () => clients.delete(res));
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
if (url.pathname === "/__bake/shown" && req.method === "POST") {
|
|
338
|
+
let body = "";
|
|
339
|
+
req.on("data", (c) => body += c);
|
|
340
|
+
req.on("end", () => {
|
|
341
|
+
res.writeHead(204).end();
|
|
342
|
+
let msg;
|
|
343
|
+
try {
|
|
344
|
+
msg = JSON.parse(body);
|
|
345
|
+
} catch {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const w = waiting.get(msg.build);
|
|
349
|
+
if (!w) return;
|
|
350
|
+
waiting.delete(msg.build);
|
|
351
|
+
emit({
|
|
352
|
+
event: "shown",
|
|
353
|
+
target: "web",
|
|
354
|
+
build: msg.build,
|
|
355
|
+
ms: performance.now() - w.timing.saveAt,
|
|
356
|
+
buildMs: w.timing.buildMs,
|
|
357
|
+
reason: w.timing.reason,
|
|
358
|
+
text: msg.text
|
|
359
|
+
});
|
|
360
|
+
w.resolve(true);
|
|
361
|
+
});
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
const rel = decodeURIComponent(url.pathname).replace(/^\/+/, "") || "index.html";
|
|
365
|
+
if (!dir) {
|
|
366
|
+
if (rel !== "index.html") {
|
|
367
|
+
res.writeHead(503).end();
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
res.writeHead(200, {
|
|
371
|
+
"content-type": TYPES[".html"],
|
|
372
|
+
"cache-control": "no-store"
|
|
373
|
+
}).end(`<!doctype html><html><head><meta charset="utf-8"><title>bake dev</title>${client(0)}</head><body>Building…</body></html>`);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
const file = resolve(dir, rel);
|
|
377
|
+
if (file !== dir && !file.startsWith(dir + sep)) {
|
|
378
|
+
res.writeHead(403).end();
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (!existsSync(file) || !statSync(file).isFile()) {
|
|
382
|
+
res.writeHead(404).end();
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
const headers = {
|
|
386
|
+
"content-type": TYPES[extname(file).toLowerCase()] ?? "application/octet-stream",
|
|
387
|
+
"cache-control": "no-store"
|
|
388
|
+
};
|
|
389
|
+
if (basename(file) === "index.html") {
|
|
390
|
+
const html = readFileSync(file, "utf8");
|
|
391
|
+
const at = html.indexOf("</head>");
|
|
392
|
+
res.writeHead(200, headers).end(at < 0 ? client(build) + html : html.slice(0, at) + client(build) + html.slice(at));
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
res.writeHead(200, headers);
|
|
396
|
+
createReadStream(file).pipe(res);
|
|
397
|
+
});
|
|
398
|
+
const port = await new Promise((done, fail) => {
|
|
399
|
+
server.once("error", (e) => {
|
|
400
|
+
if (e.code !== "EADDRINUSE" || opts.port === 0) return fail(e);
|
|
401
|
+
server.listen(0, "127.0.0.1", () => done(server.address().port));
|
|
402
|
+
});
|
|
403
|
+
server.listen(opts.port, "127.0.0.1", () => done(server.address().port));
|
|
404
|
+
});
|
|
405
|
+
const url = `http://localhost:${port}/`;
|
|
406
|
+
emit({
|
|
407
|
+
event: "serve",
|
|
408
|
+
target: "web",
|
|
409
|
+
url,
|
|
410
|
+
port
|
|
411
|
+
});
|
|
412
|
+
let opened = false;
|
|
413
|
+
return {
|
|
414
|
+
url,
|
|
415
|
+
load(next, timing) {
|
|
416
|
+
dir = resolve(next);
|
|
417
|
+
build = timing.build;
|
|
418
|
+
for (const [b, w] of waiting) {
|
|
419
|
+
waiting.delete(b);
|
|
420
|
+
w.resolve(false);
|
|
421
|
+
}
|
|
422
|
+
const shown = new Promise((resolve) => waiting.set(build, {
|
|
423
|
+
timing,
|
|
424
|
+
resolve
|
|
425
|
+
}));
|
|
426
|
+
if (clients.size) send("reload", { build });
|
|
427
|
+
else if (opts.open && !opened) {
|
|
428
|
+
opened = true;
|
|
429
|
+
openBrowser(url);
|
|
430
|
+
}
|
|
431
|
+
return shown;
|
|
432
|
+
},
|
|
433
|
+
failed(_build, lines) {
|
|
434
|
+
send("failed", lines);
|
|
435
|
+
},
|
|
436
|
+
async close() {
|
|
437
|
+
for (const res of clients) res.end();
|
|
438
|
+
server.closeAllConnections?.();
|
|
439
|
+
await new Promise((done) => server.close(done));
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
function openBrowser(url) {
|
|
444
|
+
const [cmd, args] = process.platform === "win32" ? ["cmd", [
|
|
445
|
+
"/c",
|
|
446
|
+
"start",
|
|
447
|
+
"\"\"",
|
|
448
|
+
url
|
|
449
|
+
]] : process.platform === "darwin" ? ["open", [url]] : ["xdg-open", [url]];
|
|
450
|
+
spawn(cmd, args, {
|
|
451
|
+
stdio: "ignore",
|
|
452
|
+
detached: true
|
|
453
|
+
}).unref();
|
|
454
|
+
}
|
|
455
|
+
/** The host exe and the Hermes DLLs it loads, copied under .bake/dev/host (cargo can rebuild the original while it runs). */
|
|
456
|
+
function windowsHost(opts) {
|
|
457
|
+
const checkout = existsSync(join(repo, "native/rust/Cargo.toml"));
|
|
458
|
+
const staged = checkout ? [join(repo, "examples/windows/dist/BakeNative/bakenative-app.exe"), join(repo, "native/rust/target/release/bakenative-app.exe")] : [];
|
|
459
|
+
const candidates = [
|
|
460
|
+
opts.host,
|
|
461
|
+
...staged,
|
|
462
|
+
packagedTool("bakenative-app")
|
|
463
|
+
].filter(Boolean);
|
|
464
|
+
const exe = candidates.find((p) => existsSync(p));
|
|
465
|
+
if (!exe) throw new Error(checkout ? `no Windows host exe: pass --host <bakenative-app.exe> (or set BAKE_WINDOWS_HOST), or stage one with \`pnpm windows -- --no-run\`. Looked at:\n ${candidates.join("\n ")}` : "no Windows host exe: @bake-native/toolchain-win32-x64 (an optional dependency of @bake-native/core) is not installed. Reinstall @bake-native/core with optional dependencies, or pass --host <bakenative-app.exe> (or set BAKE_WINDOWS_HOST).");
|
|
466
|
+
const artifacts = join(process.env.BAKE_HERMES_ARTIFACTS ?? join(repo, "native/hermes/artifacts"), "windows/x64");
|
|
467
|
+
const stage = join(opts.root, ".bake/dev-host");
|
|
468
|
+
mkdirSync(stage, { recursive: true });
|
|
469
|
+
for (const dll of [
|
|
470
|
+
"msvcp140.dll",
|
|
471
|
+
"vcruntime140.dll",
|
|
472
|
+
"vcruntime140_1.dll"
|
|
473
|
+
]) {
|
|
474
|
+
const from = join(exe, "..", dll);
|
|
475
|
+
if (existsSync(from) && (!existsSync(join(stage, dll)) || statSync(join(stage, dll)).size !== statSync(from).size)) copyFileSync(from, join(stage, dll));
|
|
476
|
+
}
|
|
477
|
+
const st = statSync(exe);
|
|
478
|
+
const copy = join(stage, `${basename(exe, ".exe")}-${st.size}-${Math.floor(st.mtimeMs)}.exe`);
|
|
479
|
+
if (!existsSync(copy)) copyFileSync(exe, copy);
|
|
480
|
+
for (const dll of ["bakenative_hermes.dll", "hermesvm.dll"]) {
|
|
481
|
+
const from = [join(exe, "..", dll), join(artifacts, dll)].find((p) => existsSync(p));
|
|
482
|
+
if (!from) throw new Error(`${dll} is not next to ${exe} nor in ${artifacts}: run \`pnpm hermes\` first`);
|
|
483
|
+
if (!existsSync(join(stage, dll)) || statSync(join(stage, dll)).size !== statSync(from).size) copyFileSync(from, join(stage, dll));
|
|
484
|
+
}
|
|
485
|
+
return copy;
|
|
486
|
+
}
|
|
487
|
+
function windowsRunner(opts, emit) {
|
|
488
|
+
if (process.platform !== "win32") throw new Error("bake dev --target windows runs on Windows");
|
|
489
|
+
const exe = windowsHost(opts);
|
|
490
|
+
emit({
|
|
491
|
+
event: "notice",
|
|
492
|
+
message: `windows host ${exe}`
|
|
493
|
+
});
|
|
494
|
+
let child = null;
|
|
495
|
+
let generation = 0;
|
|
496
|
+
function stopChild() {
|
|
497
|
+
const c = child;
|
|
498
|
+
child = null;
|
|
499
|
+
if (!c || c.exitCode !== null) return Promise.resolve();
|
|
500
|
+
return new Promise((done) => {
|
|
501
|
+
c.once("exit", done);
|
|
502
|
+
c.kill();
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
return {
|
|
506
|
+
async load(dir, timing) {
|
|
507
|
+
const gen = ++generation;
|
|
508
|
+
await stopChild();
|
|
509
|
+
const report = join(dir, "launch-report.json");
|
|
510
|
+
rmSync(report, { force: true });
|
|
511
|
+
const spawnAt = performance.now();
|
|
512
|
+
const c = spawn(exe, ["--launch-report", report], {
|
|
513
|
+
cwd: dir,
|
|
514
|
+
env: {
|
|
515
|
+
...process.env,
|
|
516
|
+
BAKE_BUNDLE: dir,
|
|
517
|
+
BAKE_NO_DIALOGS: "1"
|
|
518
|
+
},
|
|
519
|
+
stdio: [
|
|
520
|
+
"ignore",
|
|
521
|
+
"pipe",
|
|
522
|
+
"pipe"
|
|
523
|
+
],
|
|
524
|
+
windowsHide: false
|
|
525
|
+
});
|
|
526
|
+
child = c;
|
|
527
|
+
emit({
|
|
528
|
+
event: "launch",
|
|
529
|
+
target: "windows",
|
|
530
|
+
build: timing.build,
|
|
531
|
+
pid: c.pid
|
|
532
|
+
});
|
|
533
|
+
for (const stream of [c.stdout, c.stderr]) {
|
|
534
|
+
let rest = "";
|
|
535
|
+
stream.setEncoding("utf8");
|
|
536
|
+
stream.on("data", (chunk) => {
|
|
537
|
+
const lines = (rest + chunk).split(/\r?\n/);
|
|
538
|
+
rest = lines.pop();
|
|
539
|
+
for (const line of lines) if (line) emit({
|
|
540
|
+
event: "log",
|
|
541
|
+
target: "windows",
|
|
542
|
+
line
|
|
543
|
+
});
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
c.on("exit", (code) => {
|
|
547
|
+
if (child === c) {
|
|
548
|
+
child = null;
|
|
549
|
+
emit({
|
|
550
|
+
event: "exit",
|
|
551
|
+
target: "windows",
|
|
552
|
+
code
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
return new Promise((resolve) => {
|
|
557
|
+
const poll = setInterval(() => {
|
|
558
|
+
if (gen !== generation || c.exitCode !== null) {
|
|
559
|
+
clearInterval(poll);
|
|
560
|
+
resolve(false);
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
let r;
|
|
564
|
+
try {
|
|
565
|
+
r = JSON.parse(readFileSync(report, "utf8"));
|
|
566
|
+
} catch {
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
clearInterval(poll);
|
|
570
|
+
const launchMs = r.hydrated_ms ?? r.first_frame_ms;
|
|
571
|
+
if (r.hydrated_ms == null) {
|
|
572
|
+
emit({
|
|
573
|
+
event: "notice",
|
|
574
|
+
message: `windows build ${timing.build} started but JS did not commit a screen (see the log above)`
|
|
575
|
+
});
|
|
576
|
+
resolve(false);
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
emit({
|
|
580
|
+
event: "shown",
|
|
581
|
+
target: "windows",
|
|
582
|
+
build: timing.build,
|
|
583
|
+
pid: c.pid,
|
|
584
|
+
ms: spawnAt - timing.saveAt + launchMs,
|
|
585
|
+
buildMs: timing.buildMs,
|
|
586
|
+
launchMs,
|
|
587
|
+
reason: timing.reason,
|
|
588
|
+
hbc: join(dir, "app.hbc"),
|
|
589
|
+
report: r
|
|
590
|
+
});
|
|
591
|
+
resolve(true);
|
|
592
|
+
}, 5);
|
|
593
|
+
});
|
|
594
|
+
},
|
|
595
|
+
failed() {},
|
|
596
|
+
close: stopChild
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
const SHOWN_TIMEOUT_MS = 3e4;
|
|
600
|
+
const run = promisify(execFile);
|
|
601
|
+
/**
|
|
602
|
+
* Reads a host's report lines (`bake-dev shown|failed|console|error …`, BakeNativeDev) for one build.
|
|
603
|
+
* `line(text)` feeds it (true when the line was a report line); `promise` settles once the build's
|
|
604
|
+
* first screen is shown (true), the launch failed or timed out (false), or `cancel()` is called.
|
|
605
|
+
* A `shown` line for another directory than this build's (`dir`, or `deviceDir` on a device)
|
|
606
|
+
* means the host ignored the bundle bake dev passed: that fails too.
|
|
607
|
+
*/
|
|
608
|
+
function shownWatch(target, dir, timing, startAt, emit, extra = {}) {
|
|
609
|
+
let settle;
|
|
610
|
+
const promise = new Promise((resolve) => settle = resolve);
|
|
611
|
+
let done = false;
|
|
612
|
+
const finish = (ok) => {
|
|
613
|
+
if (done) return;
|
|
614
|
+
done = true;
|
|
615
|
+
clearTimeout(timer);
|
|
616
|
+
settle(ok);
|
|
617
|
+
};
|
|
618
|
+
const timer = setTimeout(() => {
|
|
619
|
+
emit({
|
|
620
|
+
event: "notice",
|
|
621
|
+
message: `${target} build ${timing.build} started but did not report its first screen within ${SHOWN_TIMEOUT_MS / 1e3} s (a debug build whose host calls BakeNativeDev?)`
|
|
622
|
+
});
|
|
623
|
+
finish(false);
|
|
624
|
+
}, SHOWN_TIMEOUT_MS);
|
|
625
|
+
return {
|
|
626
|
+
promise,
|
|
627
|
+
get done() {
|
|
628
|
+
return done;
|
|
629
|
+
},
|
|
630
|
+
cancel: () => finish(false),
|
|
631
|
+
line(text) {
|
|
632
|
+
const m = text.match(/bake-dev (shown|failed|console|error) ?(.*)$/) ?? text.match(/BakeNativeDev\(\s*\d+\): (shown|failed|console|error) ?(.*)$/);
|
|
633
|
+
if (!m) return false;
|
|
634
|
+
const [, kind, rest] = m;
|
|
635
|
+
if (kind === "console" || kind === "error") {
|
|
636
|
+
emit({
|
|
637
|
+
event: "log",
|
|
638
|
+
target,
|
|
639
|
+
line: kind === "error" ? `error: ${rest}` : rest
|
|
640
|
+
});
|
|
641
|
+
return true;
|
|
642
|
+
}
|
|
643
|
+
if (done) return true;
|
|
644
|
+
if (kind === "shown") {
|
|
645
|
+
const [, phase, appMs, shownDir] = rest.match(/^(\S+) (-?\d+) (.*)$/) ?? [
|
|
646
|
+
null,
|
|
647
|
+
"",
|
|
648
|
+
null,
|
|
649
|
+
rest
|
|
650
|
+
];
|
|
651
|
+
if (shownDir !== extra.deviceDir && resolve(shownDir) !== resolve(dir)) {
|
|
652
|
+
emit({
|
|
653
|
+
event: "notice",
|
|
654
|
+
message: `${target} the app shows ${shownDir}, not build ${timing.build}: it did not load the bundle bake dev passed`
|
|
655
|
+
});
|
|
656
|
+
finish(false);
|
|
657
|
+
return true;
|
|
658
|
+
}
|
|
659
|
+
const now = performance.now();
|
|
660
|
+
emit({
|
|
661
|
+
event: "shown",
|
|
662
|
+
target,
|
|
663
|
+
build: timing.build,
|
|
664
|
+
ms: now - timing.saveAt,
|
|
665
|
+
buildMs: timing.buildMs,
|
|
666
|
+
launchMs: now - startAt,
|
|
667
|
+
restart: extra.restart,
|
|
668
|
+
reason: timing.reason,
|
|
669
|
+
phase,
|
|
670
|
+
appMs: appMs == null ? void 0 : Number(appMs),
|
|
671
|
+
...extra.event
|
|
672
|
+
});
|
|
673
|
+
finish(true);
|
|
674
|
+
} else {
|
|
675
|
+
emit({
|
|
676
|
+
event: "notice",
|
|
677
|
+
message: `${target} build ${timing.build} failed to launch: ${rest}`
|
|
678
|
+
});
|
|
679
|
+
finish(false);
|
|
680
|
+
}
|
|
681
|
+
return true;
|
|
682
|
+
}
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
/** Calls `fn` with each line of `stream`. */
|
|
686
|
+
function eachLine(stream, fn) {
|
|
687
|
+
let rest = "";
|
|
688
|
+
stream.setEncoding("utf8");
|
|
689
|
+
stream.on("data", (chunk) => {
|
|
690
|
+
const parts = (rest + chunk).split(/\r?\n/);
|
|
691
|
+
rest = parts.pop();
|
|
692
|
+
for (const l of parts) if (l) fn(l);
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
/** The Mac host binary: --host (a binary or a .app), $BAKE_MAC_HOST, or examples/mac's debug build. */
|
|
696
|
+
function macHost(opts) {
|
|
697
|
+
const binary = (p) => {
|
|
698
|
+
if (!p || !p.endsWith(".app")) return p;
|
|
699
|
+
const plist = join(p, "Contents/Info.plist");
|
|
700
|
+
const exe = existsSync(plist) ? readFileSync(plist, "utf8").match(/<key>CFBundleExecutable<\/key>\s*<string>([^<]+)<\/string>/)?.[1] : null;
|
|
701
|
+
return join(p, "Contents/MacOS", exe ?? basename(p, ".app"));
|
|
702
|
+
};
|
|
703
|
+
const candidates = [
|
|
704
|
+
binary(opts.host),
|
|
705
|
+
binary(process.env.BAKE_MAC_HOST),
|
|
706
|
+
join(repo, "examples/mac/.build/debug/MacApp")
|
|
707
|
+
].filter(Boolean);
|
|
708
|
+
const exe = candidates.find((p) => existsSync(p));
|
|
709
|
+
if (!exe) throw new Error(`no Mac host: pass --host <binary or .app> (or set BAKE_MAC_HOST), or build examples/mac in debug (\`swift build --package-path examples/mac --product MacApp\`). Looked at:\n ${candidates.join("\n ")}`);
|
|
710
|
+
return exe;
|
|
711
|
+
}
|
|
712
|
+
function macRunner(opts, emit) {
|
|
713
|
+
if (process.platform !== "darwin") throw new Error("bake dev --target mac runs on macOS");
|
|
714
|
+
const exe = macHost(opts);
|
|
715
|
+
emit({
|
|
716
|
+
event: "notice",
|
|
717
|
+
message: `mac host ${exe}`
|
|
718
|
+
});
|
|
719
|
+
let child = null;
|
|
720
|
+
let watch = null;
|
|
721
|
+
function stopChild() {
|
|
722
|
+
const c = child;
|
|
723
|
+
child = null;
|
|
724
|
+
if (!c || c.exitCode !== null || c.signalCode !== null) return Promise.resolve();
|
|
725
|
+
return new Promise((done) => {
|
|
726
|
+
const kill = setTimeout(() => c.kill("SIGKILL"), 2e3);
|
|
727
|
+
c.once("exit", () => {
|
|
728
|
+
clearTimeout(kill);
|
|
729
|
+
done();
|
|
730
|
+
});
|
|
731
|
+
c.kill("SIGTERM");
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
return {
|
|
735
|
+
async load(dir, timing) {
|
|
736
|
+
watch?.cancel();
|
|
737
|
+
await stopChild();
|
|
738
|
+
const startAt = performance.now();
|
|
739
|
+
const c = spawn(exe, [], {
|
|
740
|
+
cwd: dir,
|
|
741
|
+
env: {
|
|
742
|
+
...process.env,
|
|
743
|
+
BAKE_BUNDLE: resolve(dir)
|
|
744
|
+
},
|
|
745
|
+
stdio: [
|
|
746
|
+
"ignore",
|
|
747
|
+
"pipe",
|
|
748
|
+
"pipe"
|
|
749
|
+
]
|
|
750
|
+
});
|
|
751
|
+
child = c;
|
|
752
|
+
const w = watch = shownWatch("mac", dir, timing, startAt, emit, {
|
|
753
|
+
restart: "restart",
|
|
754
|
+
event: { pid: c.pid }
|
|
755
|
+
});
|
|
756
|
+
emit({
|
|
757
|
+
event: "launch",
|
|
758
|
+
target: "mac",
|
|
759
|
+
build: timing.build,
|
|
760
|
+
pid: c.pid
|
|
761
|
+
});
|
|
762
|
+
for (const stream of [c.stdout, c.stderr]) eachLine(stream, (l) => {
|
|
763
|
+
if (!w.line(l)) emit({
|
|
764
|
+
event: "log",
|
|
765
|
+
target: "mac",
|
|
766
|
+
line: l
|
|
767
|
+
});
|
|
768
|
+
});
|
|
769
|
+
c.on("exit", (code, signal) => {
|
|
770
|
+
if (child !== c) return;
|
|
771
|
+
child = null;
|
|
772
|
+
emit({
|
|
773
|
+
event: "exit",
|
|
774
|
+
target: "mac",
|
|
775
|
+
code: code ?? signal
|
|
776
|
+
});
|
|
777
|
+
w.cancel();
|
|
778
|
+
});
|
|
779
|
+
return w.promise;
|
|
780
|
+
},
|
|
781
|
+
failed() {},
|
|
782
|
+
async close() {
|
|
783
|
+
watch?.cancel();
|
|
784
|
+
await stopChild();
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
async function iosRunner(opts, emit) {
|
|
789
|
+
if (process.platform !== "darwin") throw new Error("bake dev --target ios runs on macOS (the iOS simulator)");
|
|
790
|
+
const simctl = (...args) => run("xcrun", ["simctl", ...args], { maxBuffer: 64 << 20 });
|
|
791
|
+
const sim = pickSimulator(JSON.parse((await simctl("list", "devices", "-j")).stdout).devices, opts.udid);
|
|
792
|
+
const id = opts.bundleId;
|
|
793
|
+
if (opts.iosHost) {
|
|
794
|
+
const t0 = performance.now();
|
|
795
|
+
await simctl("install", sim.udid, opts.iosHost);
|
|
796
|
+
emit({
|
|
797
|
+
event: "notice",
|
|
798
|
+
message: `ios installed ${opts.iosHost} (${Math.round(performance.now() - t0)} ms)`
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
let data;
|
|
802
|
+
try {
|
|
803
|
+
data = (await simctl("get_app_container", sim.udid, id, "data")).stdout.trim();
|
|
804
|
+
} catch {
|
|
805
|
+
throw new Error(`${id} is not installed on ${sim.name} (${sim.udid}): pass --ios-host <App.app> (a debug simulator build whose host calls BakeNativeDev), or --bundle-id <id>`);
|
|
806
|
+
}
|
|
807
|
+
const logs = join(data, "tmp/bake-dev");
|
|
808
|
+
rmSync(logs, {
|
|
809
|
+
recursive: true,
|
|
810
|
+
force: true
|
|
811
|
+
});
|
|
812
|
+
mkdirSync(logs, { recursive: true });
|
|
813
|
+
emit({
|
|
814
|
+
event: "notice",
|
|
815
|
+
message: `ios ${id} on ${sim.name} (${sim.runtime}, ${sim.udid})`
|
|
816
|
+
});
|
|
817
|
+
let gen = 0;
|
|
818
|
+
let watch = null;
|
|
819
|
+
let poll = null;
|
|
820
|
+
return {
|
|
821
|
+
async load(dir, timing) {
|
|
822
|
+
const g = ++gen;
|
|
823
|
+
watch?.cancel();
|
|
824
|
+
clearInterval(poll);
|
|
825
|
+
if (g > 1) rmSync(join(logs, `${g - 1}.log`), { force: true });
|
|
826
|
+
const log = join(logs, `${g}.log`);
|
|
827
|
+
const w = watch = shownWatch("ios", dir, timing, performance.now(), emit, {
|
|
828
|
+
restart: "relaunch",
|
|
829
|
+
event: { udid: sim.udid }
|
|
830
|
+
});
|
|
831
|
+
try {
|
|
832
|
+
await run("xcrun", [
|
|
833
|
+
"simctl",
|
|
834
|
+
"launch",
|
|
835
|
+
"--terminate-running-process",
|
|
836
|
+
`--stderr=${log}`,
|
|
837
|
+
`--stdout=${log}`,
|
|
838
|
+
sim.udid,
|
|
839
|
+
id
|
|
840
|
+
], { env: {
|
|
841
|
+
...process.env,
|
|
842
|
+
SIMCTL_CHILD_BAKE_BUNDLE: resolve(dir)
|
|
843
|
+
} });
|
|
844
|
+
} catch (e) {
|
|
845
|
+
emit({
|
|
846
|
+
event: "notice",
|
|
847
|
+
message: `ios simctl launch failed: ${(e.stderr || e.message).trim()}`
|
|
848
|
+
});
|
|
849
|
+
w.cancel();
|
|
850
|
+
return w.promise;
|
|
851
|
+
}
|
|
852
|
+
let offset = 0;
|
|
853
|
+
let rest = "";
|
|
854
|
+
poll = setInterval(() => {
|
|
855
|
+
if (g !== gen || w.done) {
|
|
856
|
+
clearInterval(poll);
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
let text;
|
|
860
|
+
try {
|
|
861
|
+
text = readFileSync(log, "utf8");
|
|
862
|
+
} catch {
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
if (text.length <= offset) return;
|
|
866
|
+
const parts = (rest + text.slice(offset)).split(/\r?\n/);
|
|
867
|
+
offset = text.length;
|
|
868
|
+
rest = parts.pop();
|
|
869
|
+
for (const l of parts) if (l) w.line(l);
|
|
870
|
+
}, 5);
|
|
871
|
+
return w.promise;
|
|
872
|
+
},
|
|
873
|
+
failed() {},
|
|
874
|
+
async close() {
|
|
875
|
+
watch?.cancel();
|
|
876
|
+
clearInterval(poll);
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
function adbPath() {
|
|
881
|
+
const sdk = process.env.ANDROID_HOME ?? process.env.ANDROID_SDK_ROOT ?? join(process.env.HOME ?? "", "Library/Android/sdk");
|
|
882
|
+
const exe = join(sdk, "platform-tools", process.platform === "win32" ? "adb.exe" : "adb");
|
|
883
|
+
return existsSync(exe) ? exe : "adb";
|
|
884
|
+
}
|
|
885
|
+
async function androidRunner(opts, emit) {
|
|
886
|
+
const adbExe = adbPath();
|
|
887
|
+
const serial = pickAndroid(adbDevices((await run(adbExe, ["devices"])).stdout), opts.serial ?? process.env.ANDROID_SERIAL);
|
|
888
|
+
const adb = (...args) => run(adbExe, [
|
|
889
|
+
"-s",
|
|
890
|
+
serial,
|
|
891
|
+
...args
|
|
892
|
+
], { maxBuffer: 64 << 20 });
|
|
893
|
+
const pkg = opts.package;
|
|
894
|
+
if (!/^[A-Za-z0-9_.]+$/.test(pkg)) throw new Error(`--package ${pkg} is not a package name`);
|
|
895
|
+
let component = "";
|
|
896
|
+
try {
|
|
897
|
+
component = (await adb("shell", "cmd", "package", "resolve-activity", "--brief", "-c", "android.intent.category.LAUNCHER", pkg)).stdout.trim().split("\n").pop().trim();
|
|
898
|
+
} catch {}
|
|
899
|
+
if (!component.includes("/")) throw new Error(`${pkg} is not installed on ${serial} (or has no launcher Activity): install a debug build whose Activity calls BakeNativeDev (\`pnpm android\`), or pass --package <name>`);
|
|
900
|
+
try {
|
|
901
|
+
await adb("shell", "run-as", pkg, "true");
|
|
902
|
+
} catch {
|
|
903
|
+
throw new Error(`${pkg} on ${serial} is not debuggable (run-as fails): bake dev needs a debug build`);
|
|
904
|
+
}
|
|
905
|
+
emit({
|
|
906
|
+
event: "notice",
|
|
907
|
+
message: `android ${component} on ${serial}`
|
|
908
|
+
});
|
|
909
|
+
const logcat = spawn(adbExe, [
|
|
910
|
+
"-s",
|
|
911
|
+
serial,
|
|
912
|
+
"logcat",
|
|
913
|
+
"-T",
|
|
914
|
+
"1",
|
|
915
|
+
"-v",
|
|
916
|
+
"brief",
|
|
917
|
+
"BakeNativeDev:I",
|
|
918
|
+
"AndroidRuntime:E",
|
|
919
|
+
"*:S"
|
|
920
|
+
], { stdio: [
|
|
921
|
+
"ignore",
|
|
922
|
+
"pipe",
|
|
923
|
+
"ignore"
|
|
924
|
+
] });
|
|
925
|
+
let watch = null;
|
|
926
|
+
eachLine(logcat.stdout, (l) => {
|
|
927
|
+
if (watch?.line(l)) return;
|
|
928
|
+
if (/^E\/AndroidRuntime/.test(l)) emit({
|
|
929
|
+
event: "log",
|
|
930
|
+
target: "android",
|
|
931
|
+
line: l.replace(/^E\/AndroidRuntime\(\s*\d+\):\s?/, "")
|
|
932
|
+
});
|
|
933
|
+
});
|
|
934
|
+
const stage = "/data/local/tmp/bake-dev";
|
|
935
|
+
let previous = null;
|
|
936
|
+
let gen = 0;
|
|
937
|
+
return {
|
|
938
|
+
async load(dir, timing) {
|
|
939
|
+
const g = ++gen;
|
|
940
|
+
watch?.cancel();
|
|
941
|
+
const name = String(timing.build);
|
|
942
|
+
const deviceDir = `/data/user/0/${pkg}/files/bake-dev/${name}`;
|
|
943
|
+
const startAt = performance.now();
|
|
944
|
+
try {
|
|
945
|
+
await adb("shell", "mkdir", "-p", stage);
|
|
946
|
+
await adb("push", resolve(dir), `${stage}/${name}`);
|
|
947
|
+
await adb("shell", `run-as ${pkg} mkdir -p files/bake-dev && run-as ${pkg} cp -R ${stage}/${name} files/bake-dev/ ; rm -r ${stage}/${name}`);
|
|
948
|
+
} catch (e) {
|
|
949
|
+
emit({
|
|
950
|
+
event: "notice",
|
|
951
|
+
message: `android could not copy build ${name} to ${serial}: ${(e.stderr || e.message).trim()}`
|
|
952
|
+
});
|
|
953
|
+
return false;
|
|
954
|
+
}
|
|
955
|
+
if (g !== gen) return false;
|
|
956
|
+
const pushMs = performance.now() - startAt;
|
|
957
|
+
const w = watch = shownWatch("android", dir, timing, startAt, emit, {
|
|
958
|
+
restart: "push + restart",
|
|
959
|
+
deviceDir,
|
|
960
|
+
event: {
|
|
961
|
+
serial,
|
|
962
|
+
pushMs
|
|
963
|
+
}
|
|
964
|
+
});
|
|
965
|
+
try {
|
|
966
|
+
await adb("shell", "am", "start", "-S", "-n", component, "--es", "bake_bundle", deviceDir);
|
|
967
|
+
} catch (e) {
|
|
968
|
+
emit({
|
|
969
|
+
event: "notice",
|
|
970
|
+
message: `android am start failed: ${(e.stderr || e.message).trim()}`
|
|
971
|
+
});
|
|
972
|
+
w.cancel();
|
|
973
|
+
}
|
|
974
|
+
const ok = await w.promise;
|
|
975
|
+
if (ok && previous && previous !== name) adb("shell", "run-as", pkg, "rm", "-r", `files/bake-dev/${previous}`).catch(() => {});
|
|
976
|
+
if (ok) previous = name;
|
|
977
|
+
return ok;
|
|
978
|
+
},
|
|
979
|
+
failed() {},
|
|
980
|
+
async close() {
|
|
981
|
+
watch?.cancel();
|
|
982
|
+
logcat.kill();
|
|
983
|
+
}
|
|
984
|
+
};
|
|
985
|
+
}
|
|
986
|
+
export { devCommand };
|