diff --git a/apps/desktop/electron.config.ts b/apps/desktop/electron.config.ts index 84604f00..33b31e42 100644 --- a/apps/desktop/electron.config.ts +++ b/apps/desktop/electron.config.ts @@ -30,7 +30,8 @@ export default { copyright: "Copyright © CodeWork", directories: { output: "dist", buildResources: "resources" }, // appId too: on Windows the app takes it as its AppUserModelID, the one the installer gives its shortcuts. - extraMetadata: { name: packageName, productName, appId, homepage: "https://www.codework.sh" }, + // channel: the app keeps its data in the CodeWork home under desktop/. + extraMetadata: { name: packageName, productName, appId, channel, homepage: "https://www.codework.sh" }, // The app is its two bundles and its icon; Electron is the only dependency, and it is the runtime. files: ["out/*.cjs", "resources/icon.png", "package.json"], // The app server runs as its own process from Resources, so it sits outside the asar archive. diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts index 5ecea8e7..0eb1b990 100644 --- a/apps/desktop/src/main.ts +++ b/apps/desktop/src/main.ts @@ -1,8 +1,8 @@ import { app, BrowserWindow, dialog, ipcMain, net, protocol, shell } from "electron"; -import { readFileSync } from "node:fs"; import { readFile, stat } from "node:fs/promises"; import path from "node:path"; import { HOME_CHANNEL, PICK_FOLDER_CHANNEL, REVEAL_CHANNEL } from "./ipc.ts"; +import { manifest } from "./manifest.ts"; import { startServer } from "./server.ts"; const SCHEME = app.isPackaged ? "codework" : "codework-dev"; @@ -165,8 +165,7 @@ app.on("second-instance", () => { // The installer's shortcuts carry the app id, and Windows groups the taskbar button with the // shortcut whose id the running app takes. if (process.platform === "win32" && app.isPackaged) { - const { appId } = JSON.parse(readFileSync(path.join(app.getAppPath(), "package.json"), "utf8")) as { appId: string }; - app.setAppUserModelId(appId); + app.setAppUserModelId(manifest().appId); } void app.whenReady().then(async () => { diff --git a/apps/desktop/src/manifest.ts b/apps/desktop/src/manifest.ts new file mode 100644 index 00000000..d15506e6 --- /dev/null +++ b/apps/desktop/src/manifest.ts @@ -0,0 +1,10 @@ +import { app } from "electron"; +import { readFileSync } from "node:fs"; +import path from "node:path"; + +/** A packaged app's package.json, as electron.config.ts writes it. */ +export const manifest = () => + JSON.parse(readFileSync(path.join(app.getAppPath(), "package.json"), "utf8")) as { + readonly appId: string; + readonly channel: "latest" | "dev" | "canary"; + }; diff --git a/apps/desktop/src/server.ts b/apps/desktop/src/server.ts index 406729c6..95f399f6 100644 --- a/apps/desktop/src/server.ts +++ b/apps/desktop/src/server.ts @@ -3,6 +3,7 @@ import { spawn } from "node:child_process"; import path from "node:path"; import { createInterface } from "node:readline"; import { SERVER_CHANNEL } from "./ipc.ts"; +import { manifest } from "./manifest.ts"; // The server bundle: in the app's Resources when packaged, else beside this app in the workspace. const ENTRY = app.isPackaged @@ -20,7 +21,8 @@ export function startServer(): Promise { env: { ...process.env, ELECTRON_RUN_AS_NODE: "1", - CODEWORK_DATABASE: path.join(app.getPath("userData"), "app.db"), + // The app server keeps app.db in the CodeWork home under desktop/; from the repo, `local`. + CODEWORK_CHANNEL: app.isPackaged ? manifest().channel : "local", }, stdio: ["ignore", "pipe", "inherit"], }); diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 16f74b38..e6a981eb 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -1,10 +1,11 @@ import { NodeHttpServer, NodeServices } from "@effect/platform-node"; -import { Config, Effect, Layer, Schedule } from "effect"; +import { Config, Effect, FileSystem, Layer, Option, Schedule } from "effect"; import { HttpRouter, HttpServer } from "effect/http"; import { RpcSerialization, RpcServer } from "effect/rpc"; /* @effect-diagnostics nodeBuiltinImport:off -- the RPC listener needs a raw node server. */ import { createServer } from "node:http"; import type { Socket } from "node:net"; +import path from "node:path"; import { Codework } from "./codework.ts"; import { Contract } from "@codeworksh/contract"; import { Db } from "./db.ts"; @@ -76,8 +77,19 @@ const exitWithParent = Layer.effectDiscard( }), ); -// Electron main sets the path to a file in its userData folder. -const database = Layer.unwrap(Effect.map(Config.String("CODEWORK_DATABASE"), Db.layer)).pipe(Layer.orDie); +/** + * `/desktop//app.db`, beside the CLI's data; Electron main names its channel. + * `CODEWORK_DATABASE` names the file instead (tests). + */ +const database = Layer.unwrap( + Effect.gen(function* () { + const named = yield* Config.option(Config.String("CODEWORK_DATABASE")); + if (Option.isSome(named)) return Db.layer(named.value); + const dir = path.join(yield* Servers.cliHome, "desktop", yield* Config.String("CODEWORK_CHANNEL")); + yield* (yield* FileSystem.FileSystem).makeDirectory(dir, { recursive: true }); + return Db.layer(path.join(dir, "app.db")); + }), +).pipe(Layer.orDie); const rpc = RpcServer.layerHttp({ group: Contract.Api, path: "/rpc", protocol: "websocket" }).pipe( Layer.provide(Handlers.layer), diff --git a/apps/server/src/servers.ts b/apps/server/src/servers.ts index a650c265..a644122e 100644 --- a/apps/server/src/servers.ts +++ b/apps/server/src/servers.ts @@ -6,7 +6,9 @@ import path from "node:path"; import { type CliEnvVar, LOCAL_SERVER, type ServerAdd, ServersError, type ServerUpdate } from "@codeworksh/contract"; /** The same home the CLI resolves: CODEWORK_HOME_DIR, else ~/.codework. Only the built-in server falls back to it. */ -const cliHome = Config.String("CODEWORK_HOME_DIR").pipe(Config.withDefault(path.join(os.homedir(), ".codework"))); +export const cliHome = Config.String("CODEWORK_HOME_DIR").pipe( + Config.withDefault(path.join(os.homedir(), ".codework")), +); // Stored with every value; only the app server reads the sensitive ones. export const StoredEnv = Schema.Array(