feat(launcher): add the launcher application skeleton
The installer and launcher is an Electron app in apps/launcher: main process, a sandboxed preload that only forwards allowed channels, and a React window translated into Chinese and English. Every renderer -> main call is declared once in the IPC contract (shared/ipc/contract.ts) with zod schemas for its input and output; the main side validates both when it registers a handler. The main process throws error codes only, and the window translates them. The launcher's own settings (for now the window language) are kept in the user data folder and written through a temporary file and a rename. Development runs can point the user data folder elsewhere with WTO_LAUNCHER_USER_DATA. The workspace packages (@wto/patcher, @wto/server) stay external to the bundle: they ship as TypeScript sources and will run in utility processes, where Electron's Node strips the types and they find their web pages, tuning and keys next to their sources as they do today. The root typecheck leaves out the launcher, which has its own tsconfig files for the main process and the window. Checked with the launcher typecheck, electron-vite build, the root typecheck, and a start on a scratch user data folder.
This commit is contained in:
1 parent
930ea07630
commit
70d59d7bf7
30 files changed
+3484
-1
No files matched your search
@@ -0,0 +1,20 @@
|
||||
import { app } from 'electron';
|
||||
import { getSettings, updateSettings } from '../services/config/settings-store.ts';
|
||||
import { broadcast, handle } from './register.ts';
|
||||
|
||||
/** Registers the handler of every contract channel. One line per channel; the work is in services/. */
|
||||
export function registerIpcHandlers(): void {
|
||||
handle('app:getInfo', () => ({
|
||||
version: app.getVersion(),
|
||||
electron: process.versions.electron,
|
||||
systemLocale: app.getLocale(),
|
||||
userDataPath: app.getPath('userData'),
|
||||
}));
|
||||
|
||||
handle('settings:get', () => getSettings());
|
||||
handle('settings:update', async (patch) => {
|
||||
const settings = await updateSettings(patch);
|
||||
broadcast('event:settings-changed', settings);
|
||||
return settings;
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { BrowserWindow, ipcMain, type WebContents } from 'electron';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import {
|
||||
ipcContract,
|
||||
ipcEvents,
|
||||
type IpcChannel,
|
||||
type IpcEventChannel,
|
||||
type IpcEventPayload,
|
||||
type IpcOutput,
|
||||
type IpcParsedInput,
|
||||
} from '@shared/ipc/contract.ts';
|
||||
|
||||
/**
|
||||
* Typed IPC on the main side.
|
||||
* handle(): registers the handler of a contract channel; its input and its output are validated
|
||||
* against the contract.
|
||||
* broadcast(): sends a contract event to every window, validated the same way.
|
||||
*/
|
||||
|
||||
export function handle<C extends IpcChannel>(
|
||||
channel: C,
|
||||
handler: (input: IpcParsedInput<C>, sender: WebContents) => Promise<IpcOutput<C>> | IpcOutput<C>,
|
||||
): void {
|
||||
ipcMain.handle(channel, async (event, rawInput: unknown) => {
|
||||
const schema = ipcContract[channel];
|
||||
const input = schema.input.parse(rawInput) as IpcParsedInput<C>;
|
||||
const result = await handler(input, event.sender);
|
||||
// An output that breaks the contract is our bug. Keep the details in the log and give the
|
||||
// window a code it can show.
|
||||
const checked = schema.output.safeParse(result);
|
||||
if (!checked.success) {
|
||||
console.error(`[ipc] ${channel} returned an unexpected shape:`, checked.error.issues);
|
||||
throw new AppError('internal_error', { detail: channel });
|
||||
}
|
||||
return checked.data;
|
||||
});
|
||||
}
|
||||
|
||||
export function broadcast<C extends IpcEventChannel>(channel: C, payload: IpcEventPayload<C>): void {
|
||||
const checked = ipcEvents[channel].parse(payload);
|
||||
for (const window of BrowserWindow.getAllWindows()) window.webContents.send(channel, checked);
|
||||
}
|
||||
Reference in new issue
Block a user