@zenbujs/core 0.0.1

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.
Files changed (94) hide show
  1. package/LICENSE +11 -0
  2. package/dist/advice-config-CjgkEf2E.mjs +135 -0
  3. package/dist/advice-config-Cy133IQP.mjs +2 -0
  4. package/dist/advice-runtime.d.mts +35 -0
  5. package/dist/advice-runtime.mjs +131 -0
  6. package/dist/advice.d.mts +36 -0
  7. package/dist/advice.mjs +2 -0
  8. package/dist/base-window-BUt8pwbw.mjs +94 -0
  9. package/dist/base-window-DEIAk618.mjs +2 -0
  10. package/dist/build-config-pbv0w4oN.mjs +17 -0
  11. package/dist/build-electron-B4Gd0Gi4.mjs +516 -0
  12. package/dist/build-source-_q1n1zTV.mjs +162 -0
  13. package/dist/chunk-Dm34NbLt.mjs +6 -0
  14. package/dist/cli/bin.d.mts +1 -0
  15. package/dist/cli/bin.mjs +88 -0
  16. package/dist/cli/build.d.mts +53 -0
  17. package/dist/cli/build.mjs +48 -0
  18. package/dist/cli-BLbQQIVB.mjs +8054 -0
  19. package/dist/config-CdVrW85P.mjs +59 -0
  20. package/dist/config-LK73dJmO.mjs +2 -0
  21. package/dist/db-ByKPbnP6.mjs +2 -0
  22. package/dist/db-DhuAJrye.mjs +531 -0
  23. package/dist/db.d.mts +16 -0
  24. package/dist/db.mjs +16 -0
  25. package/dist/dev-BuqklM0k.mjs +85 -0
  26. package/dist/env-bootstrap-BtVME-CU.d.mts +16 -0
  27. package/dist/env-bootstrap-rj7I-59x.mjs +53 -0
  28. package/dist/env-bootstrap.d.mts +2 -0
  29. package/dist/env-bootstrap.mjs +2 -0
  30. package/dist/http-IBcLzbYu.mjs +2 -0
  31. package/dist/index-Bhlbyrn7.d.mts +63 -0
  32. package/dist/index-CPZ5d6Hl.d.mts +442 -0
  33. package/dist/index-FtE8MXJ_.d.mts +1 -0
  34. package/dist/index.d.mts +6 -0
  35. package/dist/index.mjs +5 -0
  36. package/dist/launcher.mjs +173 -0
  37. package/dist/link-6roQ7Cn6.mjs +580 -0
  38. package/dist/loaders/zenbu.d.mts +22 -0
  39. package/dist/loaders/zenbu.mjs +267 -0
  40. package/dist/log-CyKv8hQg.mjs +20 -0
  41. package/dist/mirror-sync-CodOnwkD.mjs +332 -0
  42. package/dist/monorepo-CmGPHsVm.mjs +119 -0
  43. package/dist/node-D4M19_mV.mjs +5 -0
  44. package/dist/node-loader.d.mts +17 -0
  45. package/dist/node-loader.mjs +33 -0
  46. package/dist/pause-DvAUNmKn.mjs +52 -0
  47. package/dist/publish-source-BVgB62Zj.mjs +131 -0
  48. package/dist/react.d.mts +76 -0
  49. package/dist/react.mjs +291 -0
  50. package/dist/registry-Dh_e7HU1.d.mts +61 -0
  51. package/dist/registry.d.mts +2 -0
  52. package/dist/registry.mjs +1 -0
  53. package/dist/reloader-BCkLjDhS.mjs +2 -0
  54. package/dist/reloader-lLAJ3lqg.mjs +164 -0
  55. package/dist/renderer-host-Bg8QdeeH.mjs +1508 -0
  56. package/dist/renderer-host-DpvBPTHJ.mjs +2 -0
  57. package/dist/rpc-BwwQK6hD.mjs +71 -0
  58. package/dist/rpc-CqitnyR4.mjs +2 -0
  59. package/dist/rpc.d.mts +2 -0
  60. package/dist/rpc.mjs +2 -0
  61. package/dist/runtime-CjqDr8Yf.d.mts +109 -0
  62. package/dist/runtime-DUFKDIe4.mjs +409 -0
  63. package/dist/runtime.d.mts +2 -0
  64. package/dist/runtime.mjs +2 -0
  65. package/dist/schema-CIg4GzHQ.mjs +100 -0
  66. package/dist/schema-DMoSkwUx.d.mts +62 -0
  67. package/dist/schema-dGK6qkfR.mjs +28 -0
  68. package/dist/schema.d.mts +2 -0
  69. package/dist/schema.mjs +2 -0
  70. package/dist/server-BXwZEQ-n.mjs +66 -0
  71. package/dist/server-DjrZUbbu.mjs +2 -0
  72. package/dist/services/default.d.mts +11 -0
  73. package/dist/services/default.mjs +22 -0
  74. package/dist/services/index.d.mts +276 -0
  75. package/dist/services/index.mjs +7 -0
  76. package/dist/setup-gate-BeD6WS6d.mjs +110 -0
  77. package/dist/setup-gate-BqOzm7zp.d.mts +4 -0
  78. package/dist/setup-gate.d.mts +2 -0
  79. package/dist/setup-gate.mjs +2 -0
  80. package/dist/src-pELM4_iH.mjs +376 -0
  81. package/dist/trace-DCB7qFzT.mjs +10 -0
  82. package/dist/transform-DJH3vN4b.mjs +84041 -0
  83. package/dist/transport-BMSzG2-F.mjs +1045 -0
  84. package/dist/view-registry-BualWgAf.mjs +2 -0
  85. package/dist/vite-plugins-Bh3SCOw-.mjs +331 -0
  86. package/dist/vite.d.mts +68 -0
  87. package/dist/vite.mjs +2 -0
  88. package/dist/window-CM2a9Kyc.mjs +2 -0
  89. package/dist/window-CmmpCVX6.mjs +156 -0
  90. package/dist/write-9dRFczGJ.mjs +1248 -0
  91. package/migrations/0000_migration.ts +34 -0
  92. package/migrations/meta/0000_snapshot.json +18 -0
  93. package/migrations/meta/_journal.json +10 -0
  94. package/package.json +124 -0
@@ -0,0 +1,173 @@
1
+ import { app } from "electron";
2
+ import crypto from "node:crypto";
3
+ import { spawn } from "node:child_process";
4
+ import fs, { existsSync } from "node:fs";
5
+ import fsp from "node:fs/promises";
6
+ import os from "node:os";
7
+ import path from "node:path";
8
+ //#region src/launcher.ts
9
+ /**
10
+ * Zenbu launcher shim.
11
+ *
12
+ * This file is bundled by tsdown (as a second entry alongside `cli/bin.mjs`)
13
+ * and shipped inside `@zenbujs/core`'s `dist/` as `launcher.mjs`.
14
+ * `zen build:electron` copies it into the .app bundle as `<app>/launcher.mjs`,
15
+ * and the bundle's `package.json#main` points at it.
16
+ *
17
+ * Its job — and ONLY its job — is to make `~/.zenbu/apps/<name>/` runnable,
18
+ * then dynamic-`import()` the apps-dir's `@zenbujs/core/dist/setup-gate.mjs`.
19
+ *
20
+ * It must NOT import `@zenbujs/core`. The whole point of the shim is to
21
+ * preserve a single framework module identity — the apps-dir copy that pnpm
22
+ * installs on first launch is the one true `@zenbujs/core`. If we imported
23
+ * `@zenbujs/core` here, we'd have two instances and the runtime singleton,
24
+ * `instanceof` checks, and dynohot HMR would all split-brain.
25
+ *
26
+ * Allowed deps: node built-ins + the `electron` main-process API (provided at
27
+ * runtime by the running Electron, marked `external` in tsdown.config.ts).
28
+ */
29
+ const _logDir = path.join(os.homedir(), ".zenbu", ".internal");
30
+ fs.mkdirSync(_logDir, { recursive: true });
31
+ const _logStream = fs.createWriteStream(path.join(_logDir, "launcher.log"), { flags: "a" });
32
+ _logStream.write(`\n=== launcher ${(/* @__PURE__ */ new Date()).toISOString()} pid=${process.pid} ===\n`);
33
+ function silenceStream(stream, prefix) {
34
+ if (!stream) return;
35
+ const original = stream.write.bind(stream);
36
+ const safeWrite = ((chunk, encodingOrCb, cb) => {
37
+ try {
38
+ const text = typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8");
39
+ _logStream.write(prefix + text);
40
+ } catch {}
41
+ try {
42
+ return original(chunk, encodingOrCb, cb);
43
+ } catch {
44
+ return true;
45
+ }
46
+ });
47
+ stream.write = safeWrite;
48
+ stream.on("error", () => {});
49
+ }
50
+ silenceStream(process.stdout, "");
51
+ silenceStream(process.stderr, "[ERR] ");
52
+ process.on("uncaughtException", (err) => {
53
+ try {
54
+ _logStream.write("[UNCAUGHT] " + (err.stack ?? err.message ?? String(err)) + "\n");
55
+ } catch {}
56
+ if (err.code === "EPIPE") return;
57
+ });
58
+ process.on("unhandledRejection", (reason) => {
59
+ try {
60
+ const err = reason;
61
+ _logStream.write("[UNHANDLED] " + (err?.stack ?? err?.message ?? String(reason)) + "\n");
62
+ } catch {}
63
+ });
64
+ const APP_PATH = app.getAppPath();
65
+ const RESOURCES_PATH = path.dirname(APP_PATH);
66
+ function readAppConfig() {
67
+ const configPath = path.join(APP_PATH, "app-config.json");
68
+ return JSON.parse(fs.readFileSync(configPath, "utf8"));
69
+ }
70
+ function appsDirFor(name) {
71
+ if (process.env.ZENBU_APPS_DIR) return path.resolve(process.env.ZENBU_APPS_DIR);
72
+ return path.join(os.homedir(), ".zenbu", "apps", name);
73
+ }
74
+ async function copySeed(seedDir, dest) {
75
+ await fsp.mkdir(path.dirname(dest), { recursive: true });
76
+ await fsp.cp(seedDir, dest, { recursive: true });
77
+ }
78
+ async function fileHash(hash, filePath) {
79
+ hash.update(filePath);
80
+ hash.update("\0");
81
+ try {
82
+ hash.update(await fsp.readFile(filePath));
83
+ } catch {}
84
+ hash.update("\0");
85
+ }
86
+ async function depsSignature(appsDir) {
87
+ const hash = crypto.createHash("sha256");
88
+ await fileHash(hash, path.join(appsDir, "package.json"));
89
+ await fileHash(hash, path.join(appsDir, "pnpm-lock.yaml"));
90
+ hash.update(process.versions.electron ?? "no-electron");
91
+ hash.update("\0");
92
+ hash.update(process.platform);
93
+ hash.update("\0");
94
+ hash.update(process.arch);
95
+ return hash.digest("hex");
96
+ }
97
+ function bundledToolPath(name) {
98
+ const candidates = [path.join(RESOURCES_PATH, "toolchain", "bin", name), path.join(RESOURCES_PATH, "toolchain", name)];
99
+ for (const candidate of candidates) if (existsSync(candidate)) return candidate;
100
+ return null;
101
+ }
102
+ function electronTargetVersion(appsDir) {
103
+ if (process.versions.electron) return process.versions.electron;
104
+ try {
105
+ const pkg = JSON.parse(fs.readFileSync(path.join(appsDir, "package.json"), "utf8"));
106
+ return (pkg.devDependencies?.electron ?? pkg.dependencies?.electron ?? "").replace(/^[^\d]*/, "") || "42.0.0";
107
+ } catch {
108
+ return "42.0.0";
109
+ }
110
+ }
111
+ async function runPnpmInstall(appsDir) {
112
+ const pnpm = bundledToolPath("pnpm");
113
+ if (!pnpm) throw new Error(`bundled pnpm not found in ${RESOURCES_PATH}/toolchain. The .app is missing required toolchain binaries.`);
114
+ const target = electronTargetVersion(appsDir);
115
+ await new Promise((resolve, reject) => {
116
+ const child = spawn(pnpm, ["install", "--no-frozen-lockfile"], {
117
+ cwd: appsDir,
118
+ stdio: "inherit",
119
+ env: {
120
+ ...process.env,
121
+ CI: "true",
122
+ HOME: path.join(appsDir, ".zenbu", ".node-gyp"),
123
+ npm_config_runtime: "electron",
124
+ npm_config_target: target,
125
+ npm_config_disturl: "https://electronjs.org/headers",
126
+ npm_config_arch: process.arch
127
+ }
128
+ });
129
+ child.on("error", reject);
130
+ child.on("close", (code) => {
131
+ if (code === 0) resolve();
132
+ else reject(/* @__PURE__ */ new Error(`pnpm install exited with code ${code}`));
133
+ });
134
+ });
135
+ }
136
+ async function ensureAppsDir(seedDir, appsDir) {
137
+ if (existsSync(appsDir)) return;
138
+ if (!existsSync(seedDir)) throw new Error(`apps dir ${appsDir} is missing and no seed/ directory found in the .app at ${seedDir}.`);
139
+ console.log(`[launcher] seeding ${appsDir} from bundled seed`);
140
+ await copySeed(seedDir, appsDir);
141
+ }
142
+ async function ensureDepsInstalled(appsDir) {
143
+ const sigPath = path.join(appsDir, ".zenbu", "deps-sig");
144
+ const nodeModules = path.join(appsDir, "node_modules");
145
+ const nextSig = await depsSignature(appsDir);
146
+ if (existsSync(nodeModules)) try {
147
+ if (await fsp.readFile(sigPath, "utf8") === nextSig) return;
148
+ } catch {}
149
+ console.log(`[launcher] installing deps in ${appsDir}`);
150
+ await runPnpmInstall(appsDir);
151
+ await fsp.mkdir(path.dirname(sigPath), { recursive: true });
152
+ await fsp.writeFile(sigPath, nextSig);
153
+ }
154
+ async function handoff(appsDir) {
155
+ const entry = path.join(appsDir, "node_modules", "@zenbujs", "core", "dist", "setup-gate.mjs");
156
+ if (!existsSync(entry)) throw new Error(`[launcher] expected entry not found: ${entry}. The seeded app may be missing @zenbujs/core in its dependencies.`);
157
+ if (!process.argv.some((arg) => arg.startsWith("--project="))) process.argv.push(`--project=${appsDir}`);
158
+ process.env.ZENBU_LAUNCHED_FROM_BUNDLE = "1";
159
+ await import(entry);
160
+ }
161
+ async function main() {
162
+ await app.whenReady();
163
+ const appsDir = appsDirFor(readAppConfig().name);
164
+ await ensureAppsDir(path.join(APP_PATH, "seed"), appsDir);
165
+ await ensureDepsInstalled(appsDir);
166
+ await handoff(appsDir);
167
+ }
168
+ main().catch((err) => {
169
+ console.error("[launcher] fatal:", err);
170
+ app.exit(1);
171
+ });
172
+ //#endregion
173
+ export {};