diff --git a/scripts/build-windows-installer.ts b/scripts/build-windows-installer.ts index 3b4a1dc..fd06f06 100644 --- a/scripts/build-windows-installer.ts +++ b/scripts/build-windows-installer.ts @@ -1,4 +1,5 @@ -import { cp, mkdir, mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises'; +import { execFileSync } from 'node:child_process'; +import { cp, lstat, 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'; @@ -7,7 +8,8 @@ import { launcherReleaseFiles } from '../apps/launcher/src/shared/schemas/app-up 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 buildFolder = join(repository, 'build'); +const output = join(buildFolder, 'windows-installer'); const workspacePackages = ['apps/server', 'packages/blk', 'packages/exepatch', 'packages/jsonc', 'packages/patcher', 'packages/vromfs']; interface LauncherManifest { @@ -47,10 +49,13 @@ async function stageWorkspacePackage(folder: string, resources: string, runtimeM await writeFile(join(bridge, 'index.js'), 'export * from ' + JSON.stringify(entry) + ';\n'); } -async function stageInstaller(stage: string): Promise { - const manifest = JSON.parse(await readFile(join(launcher, 'package.json'), 'utf8')) as LauncherManifest; +async function stageInstaller(stage: string, manifest: LauncherManifest): Promise { const { productName, version, description, main, type } = manifest; - const artifactName = launcherReleaseFiles(version).installer; + const native = join(stage, 'built-native'); + execFileSync('powershell.exe', [ + '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', + '-File', join(repository, 'native', 'wto-net', 'build.ps1'), '-Out', native, + ], { cwd: repository, stdio: 'inherit', windowsHide: true }); await writeFile(join(stage, 'package.json'), JSON.stringify({ name: 'wtoffline-launcher', productName, version, description, main, type, license: 'AGPL-3.0-or-later', })); @@ -59,7 +64,7 @@ async function stageInstaller(stage: string): Promise { 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')); + await cp(join(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 }); } @@ -68,7 +73,33 @@ async function stageInstaller(stage: string): Promise { for (const name of ['fflate', 'zod']) { await cp(join(modules, name), join(runtimeModules, name), { recursive: true }); } - return artifactName; +} + +async function cleanInstallerOutput(version: string): Promise { + await mkdir(buildFolder, { recursive: true }); + if (relative(buildFolder, await realpath(buildFolder)) !== '') throw new Error('The build directory is redirected.'); + await mkdir(output, { recursive: true }); + const root = await realpath(output); + if (relative(output, root) !== '') throw new Error('The installer output directory is redirected.'); + + const { installer, manifest, signature } = launcherReleaseFiles(version); + const legacy = `WTOffline Launcher Setup ${version}.exe`; + const names = [installer, installer + '.blockmap', legacy, legacy + '.blockmap', manifest, signature, 'win-unpacked']; + const artifacts = []; + for (const name of names) { + const path = join(root, name); + const entry = await lstat(path).catch((error: NodeJS.ErrnoException) => { + if (error.code === 'ENOENT') return null; + throw error; + }); + if (!entry) continue; + const directory = name === 'win-unpacked'; + if (relative(path, await realpath(path)) !== '' || !(directory ? entry.isDirectory() : entry.isFile())) { + throw new Error(`Unexpected installer output: ${path}`); + } + artifacts.push({ path, directory }); + } + for (const { path, directory } of artifacts) await rm(path, { recursive: directory, force: true }); } async function main(): Promise { @@ -78,7 +109,10 @@ async function main(): Promise { 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 manifest = JSON.parse(await readFile(join(launcher, 'package.json'), 'utf8')) as LauncherManifest; + const artifactName = launcherReleaseFiles(manifest.version).installer; + await stageInstaller(stage, manifest); + await cleanInstallerOutput(manifest.version); const artifacts = await build({ projectDir: stage, targets: Platform.WINDOWS.createTarget('nsis', Arch.x64),