Files
WT-Offline/scripts/build-windows-installer.ts
T
N0VAN0M4LY 746b74c564 feat(updates): verify and install launcher releases
Check the versioned release channel, verify the signed manifest and installer digest, and open the installer only after confirmation.

Verified type checks, the Windows package, and an upgrade handoff from a local release fixture, including tampering and interrupted downloads.
2026-11-30 11:12:44 +08:00

113 lines
5.3 KiB
TypeScript

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';
import { launcherReleaseFiles } from '../apps/launcher/src/shared/schemas/app-update.ts';
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<string> {
const manifest = JSON.parse(await readFile(join(launcher, 'package.json'), 'utf8')) as LauncherManifest;
const { productName, version, description, main, type } = manifest;
const artifactName = launcherReleaseFiles(version).installer;
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 });
}
return artifactName;
}
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 };
const artifactName = 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: {
artifactName,
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();