Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/desktop/electron.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/<channel>.
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.
Expand Down
5 changes: 2 additions & 3 deletions apps/desktop/src/main.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 () => {
Expand Down
10 changes: 10 additions & 0 deletions apps/desktop/src/manifest.ts
Original file line number Diff line number Diff line change
@@ -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";
};
4 changes: 3 additions & 1 deletion apps/desktop/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +21,8 @@ export function startServer(): Promise<string> {
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/<channel>; from the repo, `local`.
CODEWORK_CHANNEL: app.isPackaged ? manifest().channel : "local",
},
stdio: ["ignore", "pipe", "inherit"],
});
Expand Down
18 changes: 15 additions & 3 deletions apps/server/src/server.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
/**
* `<CodeWork home>/desktop/<channel>/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),
Expand Down
4 changes: 3 additions & 1 deletion apps/server/src/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down