feat(installer): package the Windows launcher and runtime

Ship the Electron workers, local server sources, native library, and bundled patch set in the Windows installer.

Verified type checks, build, and the extracted installer payload with a temporary copy, local save, and server health check.
This commit is contained in:
N0VAN0M4LY committed 2026-11-30 09:49:43 +08:00
1 parent c1dea23955
commit 10839f050f
5 files changed
+3528 -4

No files matched your search

+1
View File
@@ -9,6 +9,7 @@
"scripts": {
"dev": "electron-vite dev",
"build": "electron-vite build",
"package:win": "npm run build && node ../../scripts/build-windows-installer.ts",
"typecheck": "tsc --noEmit -p tsconfig.node.json && tsc --noEmit -p tsconfig.web.json"
},
"dependencies": {
@@ -4,7 +4,7 @@ import { activeDownloadedPack } from '../patch-pack/pack-store.ts';
/**
* Where the launcher finds what it ships besides its own code: the patch set (exe, GUI and native
* definitions and version records, @wto/patcher patch-set.ts) and the network hook library. In development they are this repository's folders.
* definitions and version records, @wto/patcher patch-set.ts) and the network hook library.
*
* The patch set is the active downloaded adaptation pack when there is a usable one
* (services/patch-pack/), otherwise the bundled one. A repository checkout uses its own patches/
@@ -26,8 +26,10 @@ export function resourcePaths(): ResourcePaths {
/** The resources that ship with the launcher, ignoring downloaded packs. */
export function bundledResourcePaths(): ResourcePaths {
if (app.isPackaged) {
// The packaged layout is decided with the installer build; nothing ships it yet.
throw new Error('resource paths of a packaged launcher are not defined yet');
return {
patches: join(process.resourcesPath, 'patches'),
networkHookLibrary: join(process.resourcesPath, 'native', 'wto_ws.dll'),
};
}
// The app path is apps/launcher; the repository is two levels up.
const repository = resolve(app.getAppPath(), '..', '..');
+3414
View File
File diff suppressed because it is too large. Load diff
+2 -1
View File
@@ -16,6 +16,7 @@
},
"devDependencies": {
"@types/node": "^24.19.2",
"electron-builder": "26.15.3",
"typescript": "5.9"
}
}
}
+106
View File
@@ -0,0 +1,106 @@
import { cp, mkdir, mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { dirname, join, relative, resolve, sep } from 'node:path';
import { Arch, build, Platform } from 'electron-builder';
const repository = resolve(import.meta.dirname, '..');
const launcher = join(repository, 'apps', 'launcher');
const modules = join(repository, 'node_modules');
const output = join(repository, 'build', 'windows-installer');
const workspacePackages = ['apps/server', 'packages/blk', 'packages/exepatch', 'packages/jsonc', 'packages/patcher', 'packages/vromfs'];
interface LauncherManifest {
readonly productName: string;
readonly version: string;
readonly description: string;
readonly main: string;
readonly type: string;
}
interface WorkspaceManifest {
readonly name: string;
readonly exports: { readonly '.': string };
}
async function stageWorkspacePackage(folder: string, resources: string, runtimeModules: string): Promise<void> {
const source = join(repository, folder);
const target = join(resources, 'runtime', folder);
const metadata = JSON.parse(await readFile(join(source, 'package.json'), 'utf8')) as WorkspaceManifest;
await mkdir(target, { recursive: true });
await cp(join(source, 'package.json'), join(target, 'package.json'));
await cp(join(source, 'src'), join(target, 'src'), { recursive: true });
if (metadata.name === '@wto/server' || metadata.name === '@wto/patcher') {
await cp(join(source, 'tuning'), join(target, 'tuning'), { recursive: true });
}
if (metadata.name === '@wto/patcher') {
await mkdir(join(target, 'keys'), { recursive: true });
await cp(join(source, 'keys', 'patch-pack-signing.pub.pem'), join(target, 'keys', 'patch-pack-signing.pub.pem'));
}
// Node resolves the package through a JavaScript entry and loads its TypeScript outside node_modules.
const bridge = join(runtimeModules, metadata.name);
await mkdir(bridge, { recursive: true });
const packagedBridge = join(resources, 'node_modules', metadata.name);
const entry = relative(packagedBridge, join(target, metadata.exports['.'])).split(sep).join('/');
await writeFile(join(bridge, 'package.json'), JSON.stringify({ name: metadata.name, type: 'module', exports: './index.js' }));
await writeFile(join(bridge, 'index.js'), 'export * from ' + JSON.stringify(entry) + ';\n');
}
async function stageInstaller(stage: string): Promise<void> {
const manifest = JSON.parse(await readFile(join(launcher, 'package.json'), 'utf8')) as LauncherManifest;
const { productName, version, description, main, type } = manifest;
await writeFile(join(stage, 'package.json'), JSON.stringify({
name: 'wtoffline-launcher', productName, version, description, main, type, license: 'AGPL-3.0-or-later',
}));
await cp(join(launcher, 'out'), join(stage, 'out'), { recursive: true });
const resources = join(stage, 'installer-resources');
await mkdir(join(resources, 'native'), { recursive: true });
await cp(join(repository, 'LICENSE'), join(resources, 'LICENSE-WTOffline'));
await cp(join(repository, 'build', 'native', 'wto_ws.dll'), join(resources, 'native', 'wto_ws.dll'));
for (const folder of ['exe', 'gui', 'native', 'versions']) {
await cp(join(repository, 'patches', folder), join(resources, 'patches', folder), { recursive: true });
}
const runtimeModules = join(stage, 'installer-modules');
for (const folder of workspacePackages) await stageWorkspacePackage(folder, resources, runtimeModules);
for (const name of ['fflate', 'zod']) {
await cp(join(modules, name), join(runtimeModules, name), { recursive: true });
}
}
async function main(): Promise<void> {
if (process.platform !== 'win32' || process.arch !== 'x64') throw new Error('The installer build requires Windows x64.');
const temp = await realpath(tmpdir());
const stage = await mkdtemp(join(temp, 'wto-installer-'));
try {
const electron = join(modules, 'electron');
const { version: electronVersion } = JSON.parse(await readFile(join(electron, 'package.json'), 'utf8')) as { version: string };
await stageInstaller(stage);
const artifacts = await build({
projectDir: stage,
targets: Platform.WINDOWS.createTarget('nsis', Arch.x64),
publish: 'never',
config: {
appId: 'com.wtoffline.launcher',
directories: { output },
electronVersion,
electronDist: join(electron, 'dist'),
asar: false,
npmRebuild: false,
files: ['package.json', 'out/**/*'],
extraResources: [
{ from: 'installer-resources', to: '.', filter: ['**/*'] },
{ from: 'installer-modules', to: 'node_modules', filter: ['**/*'] },
],
win: { signExecutable: false },
nsis: { oneClick: false, perMachine: false, allowToChangeInstallationDirectory: true, runAfterFinish: false },
},
});
for (const artifact of artifacts) console.info(artifact);
} finally {
const actual = await realpath(stage);
if (actual === stage && dirname(actual) === temp) await rm(stage, { recursive: true, force: true });
}
}
await main();