askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
launch.mjs · 3.3 KB · 65 lines
| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * Launch the built app (dist/main.js) with proper branding. |
| 4 | * |
| 5 | * Why this exists: on macOS the Dock label, the app-menu title and the Dock icon come |
| 6 | * from the .app BUNDLE, not from our code — so running plain `electron dist/main.js` |
| 7 | * always shows "Electron" and the Electron logo. In dev we therefore make a branded copy |
| 8 | * of Electron.app (name, bundle id, icon), re-sign it ad hoc, and run the app inside it. |
| 9 | * The copy is cached in .bundle/ (gitignored) and rebuilt only when Electron or the icon |
| 10 | * changes. A packaged release would use electron-builder instead; this is the dev path. |
| 11 | * |
| 12 | * Other platforms: window/taskbar icon is set in main.ts, so we just run electron. |
| 13 | */ |
| 14 | import { execFileSync, spawn } from "node:child_process"; |
| 15 | import { cpSync, existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; |
| 16 | import { createRequire } from "node:module"; |
| 17 | import { dirname, join } from "node:path"; |
| 18 | import { fileURLToPath } from "node:url"; |
| 19 | |
| 20 | const here = dirname(fileURLToPath(import.meta.url)); |
| 21 | const root = join(here, ".."); |
| 22 | const NAME = "dbbasic-browser"; |
| 23 | const BUNDLE_ID = "com.askrobots.dbbasic-browser"; |
| 24 | const electronPath = createRequire(import.meta.url)("electron"); // path to the electron binary |
| 25 | |
| 26 | let exe = electronPath; |
| 27 | |
| 28 | if (process.platform === "darwin") { |
| 29 | const srcApp = join(dirname(electronPath), "..", ".."); // …/Electron.app |
| 30 | const icon = join(root, "assets", "icon.icns"); |
| 31 | const outDir = join(root, ".bundle"); |
| 32 | const app = join(outDir, `${NAME}.app`); |
| 33 | const stampFile = join(outDir, "stamp"); |
| 34 | const electronVersion = readFileSync(join(srcApp, "..", "version"), "utf8").trim(); |
| 35 | const stamp = `${electronVersion}|${existsSync(icon) ? statSync(icon).mtimeMs : "noicon"}|${NAME}|${BUNDLE_ID}`; |
| 36 | |
| 37 | const upToDate = existsSync(app) && existsSync(stampFile) && readFileSync(stampFile, "utf8") === stamp; |
| 38 | if (!upToDate) { |
| 39 | console.error(`[launch] building branded ${NAME}.app …`); |
| 40 | rmSync(outDir, { recursive: true, force: true }); |
| 41 | mkdirSync(outDir, { recursive: true }); |
| 42 | try { |
| 43 | execFileSync("cp", ["-cR", srcApp, app]); // APFS clone: near-instant, no extra disk |
| 44 | } catch { |
| 45 | cpSync(srcApp, app, { recursive: true, verbatimSymlinks: true }); |
| 46 | } |
| 47 | const plist = join(app, "Contents", "Info.plist"); |
| 48 | const pb = (cmd) => { try { execFileSync("/usr/libexec/PlistBuddy", ["-c", cmd, plist]); } catch { /* key may not exist */ } }; |
| 49 | for (const [k, v] of [["CFBundleName", NAME], ["CFBundleDisplayName", NAME], ["CFBundleIdentifier", BUNDLE_ID]]) { |
| 50 | pb(`Delete :${k}`); |
| 51 | pb(`Add :${k} string ${v}`); |
| 52 | } |
| 53 | if (existsSync(icon)) { |
| 54 | execFileSync("cp", [icon, join(app, "Contents", "Resources", "electron.icns")]); // CFBundleIconFile points here |
| 55 | } |
| 56 | // Editing Info.plist breaks the bundle's signature; macOS (esp. Apple Silicon) wants a valid one. |
| 57 | execFileSync("codesign", ["--force", "--deep", "--sign", "-", app], { stdio: "ignore" }); |
| 58 | writeFileSync(stampFile, stamp); |
| 59 | } |
| 60 | exe = join(app, "Contents", "MacOS", "Electron"); |
| 61 | } |
| 62 | |
| 63 | const child = spawn(exe, [join(root, "dist", "main.js"), ...process.argv.slice(2)], { stdio: "inherit" }); |
| 64 | for (const sig of ["SIGINT", "SIGTERM"]) process.on(sig, () => child.kill(sig)); |
| 65 | child.on("exit", (code, signal) => process.exit(code ?? (signal ? 1 : 0))); |