fix
This commit is contained in:
7 files changed
+11
-241
No files matched your search
@@ -8,7 +8,7 @@ on:
|
||||
|
||||
jobs:
|
||||
quality:
|
||||
name: Lint, Typecheck, Test
|
||||
name: Lint, Typecheck, Test, Build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
@@ -40,3 +40,11 @@ jobs:
|
||||
|
||||
- name: Test
|
||||
run: bun run test
|
||||
|
||||
- name: Build desktop pipeline
|
||||
run: bun run build:desktop
|
||||
|
||||
- name: Verify preload bundle output
|
||||
run: |
|
||||
test -f apps/desktop/dist-electron/preload.js
|
||||
grep -nE "desktopBridge|getWsUrl|PICK_FOLDER_CHANNEL|wsUrl" apps/desktop/dist-electron/preload.js
|
||||
@@ -0,0 +1 @@
|
||||
dist-electron/
|
||||
@@ -1,226 +0,0 @@
|
||||
"use strict";
|
||||
var __create = Object.create;
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __getProtoOf = Object.getPrototypeOf;
|
||||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||
var __copyProps = (to, from, except, desc) => {
|
||||
if (from && typeof from === "object" || typeof from === "function") {
|
||||
for (let key of __getOwnPropNames(from))
|
||||
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||
}
|
||||
return to;
|
||||
};
|
||||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
||||
// If the importer is in node compatibility mode or this is not an ESM
|
||||
// file that has been converted to a CommonJS file using a Babel-
|
||||
// compatible transform (i.e. "__esModule" has not been set), then set
|
||||
// "default" to the CommonJS "module.exports" for node compatibility.
|
||||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
||||
mod
|
||||
));
|
||||
|
||||
// src/main.ts
|
||||
var import_node_child_process2 = require("child_process");
|
||||
var import_node_crypto = require("crypto");
|
||||
var import_node_fs = __toESM(require("fs"));
|
||||
var import_node_net = __toESM(require("net"));
|
||||
var import_node_os = __toESM(require("os"));
|
||||
var import_node_path = __toESM(require("path"));
|
||||
var import_electron = require("electron");
|
||||
|
||||
// src/fixPath.ts
|
||||
var import_node_child_process = require("child_process");
|
||||
function fixPath() {
|
||||
if (process.platform !== "darwin") return;
|
||||
try {
|
||||
const shell = process.env.SHELL ?? "/bin/zsh";
|
||||
const result = (0, import_node_child_process.execFileSync)(shell, ["-ilc", "echo -n $PATH"], {
|
||||
encoding: "utf8",
|
||||
timeout: 5e3
|
||||
});
|
||||
if (result) {
|
||||
process.env.PATH = result;
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
|
||||
// src/main.ts
|
||||
fixPath();
|
||||
var PICK_FOLDER_CHANNEL = "desktop:pick-folder";
|
||||
var ROOT_DIR = import_node_path.default.resolve(__dirname, "../../..");
|
||||
var BACKEND_ENTRY = import_node_path.default.join(ROOT_DIR, "apps/server/dist/index.js");
|
||||
var RENDERER_ENTRY = import_node_path.default.join(ROOT_DIR, "apps/renderer/dist/index.html");
|
||||
var STATE_DIR = import_node_path.default.join(import_node_os.default.homedir(), ".t3", "userdata");
|
||||
var isDevelopment = Boolean(process.env.VITE_DEV_SERVER_URL);
|
||||
var mainWindow = null;
|
||||
var backendProcess = null;
|
||||
var backendPort = 0;
|
||||
var backendAuthToken = "";
|
||||
var backendWsUrl = "";
|
||||
var restartAttempt = 0;
|
||||
var restartTimer = null;
|
||||
var isQuitting = false;
|
||||
async function reserveLoopbackPort() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const probe = import_node_net.default.createServer();
|
||||
probe.listen(0, "127.0.0.1", () => {
|
||||
const address = probe.address();
|
||||
const port = typeof address === "object" && address !== null ? address.port : 0;
|
||||
probe.close(() => {
|
||||
if (port > 0) {
|
||||
resolve(port);
|
||||
return;
|
||||
}
|
||||
reject(new Error("Failed to reserve backend port"));
|
||||
});
|
||||
});
|
||||
probe.on("error", reject);
|
||||
});
|
||||
}
|
||||
function backendEnv() {
|
||||
return {
|
||||
...process.env,
|
||||
CODETHING_MODE: "desktop",
|
||||
CODETHING_NO_BROWSER: "1",
|
||||
CODETHING_PORT: String(backendPort),
|
||||
CODETHING_STATE_DIR: STATE_DIR,
|
||||
CODETHING_AUTH_TOKEN: backendAuthToken
|
||||
};
|
||||
}
|
||||
function scheduleBackendRestart(reason) {
|
||||
if (isQuitting || restartTimer) return;
|
||||
const delayMs = Math.min(500 * 2 ** restartAttempt, 1e4);
|
||||
restartAttempt += 1;
|
||||
console.error(`[desktop] backend exited unexpectedly (${reason}); restarting in ${delayMs}ms`);
|
||||
restartTimer = setTimeout(() => {
|
||||
restartTimer = null;
|
||||
startBackend();
|
||||
}, delayMs);
|
||||
}
|
||||
function startBackend() {
|
||||
if (isQuitting || backendProcess) return;
|
||||
if (!import_node_fs.default.existsSync(BACKEND_ENTRY)) {
|
||||
scheduleBackendRestart(`missing server entry at ${BACKEND_ENTRY}`);
|
||||
return;
|
||||
}
|
||||
const child = (0, import_node_child_process2.spawn)(process.execPath, [BACKEND_ENTRY], {
|
||||
cwd: ROOT_DIR,
|
||||
env: backendEnv(),
|
||||
stdio: "inherit"
|
||||
});
|
||||
backendProcess = child;
|
||||
child.once("spawn", () => {
|
||||
restartAttempt = 0;
|
||||
});
|
||||
child.on("error", (error) => {
|
||||
if (backendProcess === child) {
|
||||
backendProcess = null;
|
||||
}
|
||||
scheduleBackendRestart(error.message);
|
||||
});
|
||||
child.on("exit", (code, signal) => {
|
||||
if (backendProcess === child) {
|
||||
backendProcess = null;
|
||||
}
|
||||
if (isQuitting) return;
|
||||
const reason = `code=${code ?? "null"} signal=${signal ?? "null"}`;
|
||||
scheduleBackendRestart(reason);
|
||||
});
|
||||
}
|
||||
function stopBackend() {
|
||||
if (restartTimer) {
|
||||
clearTimeout(restartTimer);
|
||||
restartTimer = null;
|
||||
}
|
||||
const child = backendProcess;
|
||||
backendProcess = null;
|
||||
if (!child) return;
|
||||
if (!child.killed) {
|
||||
child.kill("SIGTERM");
|
||||
setTimeout(() => {
|
||||
if (!child.killed) {
|
||||
child.kill("SIGKILL");
|
||||
}
|
||||
}, 2e3).unref();
|
||||
}
|
||||
}
|
||||
function registerIpcHandlers() {
|
||||
import_electron.ipcMain.removeHandler(PICK_FOLDER_CHANNEL);
|
||||
import_electron.ipcMain.handle(PICK_FOLDER_CHANNEL, async () => {
|
||||
const owner = import_electron.BrowserWindow.getFocusedWindow() ?? mainWindow;
|
||||
const result = owner ? await import_electron.dialog.showOpenDialog(owner, {
|
||||
properties: ["openDirectory", "createDirectory"]
|
||||
}) : await import_electron.dialog.showOpenDialog({
|
||||
properties: ["openDirectory", "createDirectory"]
|
||||
});
|
||||
if (result.canceled) return null;
|
||||
return result.filePaths[0] ?? null;
|
||||
});
|
||||
}
|
||||
function createWindow() {
|
||||
const window = new import_electron.BrowserWindow({
|
||||
width: 1100,
|
||||
height: 780,
|
||||
minWidth: 840,
|
||||
minHeight: 620,
|
||||
show: false,
|
||||
autoHideMenuBar: true,
|
||||
titleBarStyle: "hiddenInset",
|
||||
trafficLightPosition: { x: 16, y: 18 },
|
||||
webPreferences: {
|
||||
preload: import_node_path.default.join(__dirname, "preload.js"),
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false,
|
||||
sandbox: true
|
||||
}
|
||||
});
|
||||
window.webContents.setWindowOpenHandler(() => ({ action: "deny" }));
|
||||
window.once("ready-to-show", () => {
|
||||
window.show();
|
||||
});
|
||||
if (isDevelopment) {
|
||||
void window.loadURL(process.env.VITE_DEV_SERVER_URL);
|
||||
} else {
|
||||
if (!import_node_fs.default.existsSync(RENDERER_ENTRY)) {
|
||||
throw new Error(`Renderer bundle missing at ${RENDERER_ENTRY}`);
|
||||
}
|
||||
void window.loadFile(RENDERER_ENTRY);
|
||||
}
|
||||
window.on("closed", () => {
|
||||
if (mainWindow === window) {
|
||||
mainWindow = null;
|
||||
}
|
||||
});
|
||||
return window;
|
||||
}
|
||||
async function bootstrap() {
|
||||
backendPort = await reserveLoopbackPort();
|
||||
backendAuthToken = (0, import_node_crypto.randomBytes)(24).toString("hex");
|
||||
backendWsUrl = `ws://127.0.0.1:${backendPort}/?token=${encodeURIComponent(backendAuthToken)}`;
|
||||
process.env.CODETHING_DESKTOP_WS_URL = backendWsUrl;
|
||||
registerIpcHandlers();
|
||||
startBackend();
|
||||
mainWindow = createWindow();
|
||||
}
|
||||
import_electron.app.on("before-quit", () => {
|
||||
isQuitting = true;
|
||||
stopBackend();
|
||||
});
|
||||
import_electron.app.whenReady().then(() => {
|
||||
void bootstrap();
|
||||
import_electron.app.on("activate", () => {
|
||||
if (import_electron.BrowserWindow.getAllWindows().length === 0) {
|
||||
mainWindow = createWindow();
|
||||
}
|
||||
});
|
||||
});
|
||||
import_electron.app.on("window-all-closed", () => {
|
||||
if (process.platform !== "darwin") {
|
||||
import_electron.app.quit();
|
||||
}
|
||||
});
|
||||
//# sourceMappingURL=main.js.map
|
||||
@@ -1 +0,0 @@
|
||||
{"version":3,"sources":["../src/main.ts","../src/fixPath.ts"],"sourcesContent":["import { spawn, type ChildProcess } from \"node:child_process\";\nimport { randomBytes } from \"node:crypto\";\nimport fs from \"node:fs\";\nimport net from \"node:net\";\nimport os from \"node:os\";\nimport path from \"node:path\";\n\nimport { app, BrowserWindow, dialog, ipcMain } from \"electron\";\n\nimport { fixPath } from \"./fixPath\";\n\nfixPath();\n\nconst PICK_FOLDER_CHANNEL = \"desktop:pick-folder\";\nconst ROOT_DIR = path.resolve(__dirname, \"../../..\");\nconst BACKEND_ENTRY = path.join(ROOT_DIR, \"apps/server/dist/index.js\");\nconst RENDERER_ENTRY = path.join(ROOT_DIR, \"apps/renderer/dist/index.html\");\nconst STATE_DIR = path.join(os.homedir(), \".t3\", \"userdata\");\nconst isDevelopment = Boolean(process.env.VITE_DEV_SERVER_URL);\n\nlet mainWindow: BrowserWindow | null = null;\nlet backendProcess: ChildProcess | null = null;\nlet backendPort = 0;\nlet backendAuthToken = \"\";\nlet backendWsUrl = \"\";\nlet restartAttempt = 0;\nlet restartTimer: ReturnType<typeof setTimeout> | null = null;\nlet isQuitting = false;\n\nasync function reserveLoopbackPort(): Promise<number> {\n return new Promise((resolve, reject) => {\n const probe = net.createServer();\n probe.listen(0, \"127.0.0.1\", () => {\n const address = probe.address();\n const port = typeof address === \"object\" && address !== null ? address.port : 0;\n probe.close(() => {\n if (port > 0) {\n resolve(port);\n return;\n }\n reject(new Error(\"Failed to reserve backend port\"));\n });\n });\n probe.on(\"error\", reject);\n });\n}\n\nfunction backendEnv(): NodeJS.ProcessEnv {\n return {\n ...process.env,\n CODETHING_MODE: \"desktop\",\n CODETHING_NO_BROWSER: \"1\",\n CODETHING_PORT: String(backendPort),\n CODETHING_STATE_DIR: STATE_DIR,\n CODETHING_AUTH_TOKEN: backendAuthToken,\n };\n}\n\nfunction scheduleBackendRestart(reason: string): void {\n if (isQuitting || restartTimer) return;\n\n const delayMs = Math.min(500 * 2 ** restartAttempt, 10_000);\n restartAttempt += 1;\n console.error(`[desktop] backend exited unexpectedly (${reason}); restarting in ${delayMs}ms`);\n\n restartTimer = setTimeout(() => {\n restartTimer = null;\n startBackend();\n }, delayMs);\n}\n\nfunction startBackend(): void {\n if (isQuitting || backendProcess) return;\n\n if (!fs.existsSync(BACKEND_ENTRY)) {\n scheduleBackendRestart(`missing server entry at ${BACKEND_ENTRY}`);\n return;\n }\n\n const child = spawn(process.execPath, [BACKEND_ENTRY], {\n cwd: ROOT_DIR,\n env: backendEnv(),\n stdio: \"inherit\",\n });\n backendProcess = child;\n\n child.once(\"spawn\", () => {\n restartAttempt = 0;\n });\n\n child.on(\"error\", (error) => {\n if (backendProcess === child) {\n backendProcess = null;\n }\n scheduleBackendRestart(error.message);\n });\n\n child.on(\"exit\", (code, signal) => {\n if (backendProcess === child) {\n backendProcess = null;\n }\n if (isQuitting) return;\n const reason = `code=${code ?? \"null\"} signal=${signal ?? \"null\"}`;\n scheduleBackendRestart(reason);\n });\n}\n\nfunction stopBackend(): void {\n if (restartTimer) {\n clearTimeout(restartTimer);\n restartTimer = null;\n }\n\n const child = backendProcess;\n backendProcess = null;\n if (!child) return;\n\n if (!child.killed) {\n child.kill(\"SIGTERM\");\n setTimeout(() => {\n if (!child.killed) {\n child.kill(\"SIGKILL\");\n }\n }, 2_000).unref();\n }\n}\n\nfunction registerIpcHandlers(): void {\n ipcMain.removeHandler(PICK_FOLDER_CHANNEL);\n ipcMain.handle(PICK_FOLDER_CHANNEL, async () => {\n const owner = BrowserWindow.getFocusedWindow() ?? mainWindow;\n const result = owner\n ? await dialog.showOpenDialog(owner, {\n properties: [\"openDirectory\", \"createDirectory\"],\n })\n : await dialog.showOpenDialog({\n properties: [\"openDirectory\", \"createDirectory\"],\n });\n if (result.canceled) return null;\n return result.filePaths[0] ?? null;\n });\n}\n\nfunction createWindow(): BrowserWindow {\n const window = new BrowserWindow({\n width: 1100,\n height: 780,\n minWidth: 840,\n minHeight: 620,\n show: false,\n autoHideMenuBar: true,\n titleBarStyle: \"hiddenInset\",\n trafficLightPosition: { x: 16, y: 18 },\n webPreferences: {\n preload: path.join(__dirname, \"preload.js\"),\n contextIsolation: true,\n nodeIntegration: false,\n sandbox: true,\n },\n });\n\n window.webContents.setWindowOpenHandler(() => ({ action: \"deny\" }));\n window.once(\"ready-to-show\", () => {\n window.show();\n });\n\n if (isDevelopment) {\n void window.loadURL(process.env.VITE_DEV_SERVER_URL as string);\n } else {\n if (!fs.existsSync(RENDERER_ENTRY)) {\n throw new Error(`Renderer bundle missing at $Line truncated
|
||||
@@ -1,11 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
// src/preload.ts
|
||||
var import_electron = require("electron");
|
||||
var PICK_FOLDER_CHANNEL = "desktop:pick-folder";
|
||||
var wsUrl = process.env.CODETHING_DESKTOP_WS_URL ?? null;
|
||||
import_electron.contextBridge.exposeInMainWorld("desktopBridge", {
|
||||
getWsUrl: () => wsUrl,
|
||||
pickFolder: () => import_electron.ipcRenderer.invoke(PICK_FOLDER_CHANNEL)
|
||||
});
|
||||
//# sourceMappingURL=preload.js.map
|
||||
@@ -1 +0,0 @@
|
||||
{"version":3,"sources":["../src/preload.ts"],"sourcesContent":["import { contextBridge, ipcRenderer } from \"electron\";\n\nconst PICK_FOLDER_CHANNEL = \"desktop:pick-folder\";\nconst wsUrl = process.env.CODETHING_DESKTOP_WS_URL ?? null;\n\ncontextBridge.exposeInMainWorld(\"desktopBridge\", {\n getWsUrl: () => wsUrl,\n pickFolder: () => ipcRenderer.invoke(PICK_FOLDER_CHANNEL) as Promise<string | null>,\n});\n"],"mappings":";;;AAAA,sBAA2C;AAE3C,IAAM,sBAAsB;AAC5B,IAAM,QAAQ,QAAQ,IAAI,4BAA4B;AAEtD,8BAAc,kBAAkB,iBAAiB;AAAA,EAC/C,UAAU,MAAM;AAAA,EAChB,YAAY,MAAM,4BAAY,OAAO,mBAAmB;AAC1D,CAAC;","names":[]}
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
"tasks": {
|
||||
"build": {
|
||||
"dependsOn": ["^build"],
|
||||
"outputs": ["dist/**"]
|
||||
"outputs": ["dist/**", "dist-electron/**"]
|
||||
},
|
||||
"dev": {
|
||||
"dependsOn": ["@acme/contracts#build"],
|
||||
|
||||
Reference in new issue
Block a user