fix(launcher): use original-fs in portable updater and drop default_app.asar

Electron patches node:fs to treat .asar archives as virtual directories, so portable update verification failed on staged resources/default_app.asar and recursive cleanup could leave staging folders behind. Switch staging lstat and cleanup rm in portable-update.ts to original-fs (falling back to node:fs outside Electron), strip unused resources/default_app.asar during packaging so already-released 0.1.2/0.1.3 portable builds can update cleanly to future releases, and include portable-update.log in exported diagnostics.
This commit is contained in:
N0VAN0M4LY committed 2026-12-03 07:18:14 +08:00
1 parent f6b00bc787
commit 214f9d7f4b
3 files changed
+32 -4

No files matched your search

@@ -9,19 +9,33 @@
*/
import { execFile, spawn } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import * as nodeFs from 'node:fs';
import { existsSync } from 'node:fs';
import { lstat, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
import { promisify } from 'node:util';
import { z } from 'zod';
const execute = promisify(execFile);
const require = createRequire(import.meta.url);
export const PORTABLE_MARKER_FILE = 'portable-mode';
export const PORTABLE_FILES_MANIFEST = 'portable-files.json';
export const PORTABLE_UPDATE_LOG_FILENAME = 'portable-update.log';
const LAUNCHER_EXECUTABLE = 'WTOffline Launcher.exe';
const UNINSTALLER_EXECUTABLE = 'Uninstall WTOffline Launcher.exe';
// Electron patches node:fs to expose .asar archives as virtual directories, which makes
// lstat().isFile() return false and recursive rm() fail on staged .asar files.
function rawFsPromises(): typeof nodeFs.promises {
try {
return (require('original-fs') as typeof nodeFs).promises;
} catch {
return nodeFs.promises;
}
}
const PortableInventorySchema = z.object({
format: z.literal(1),
version: z.string().min(1),
@@ -216,6 +230,7 @@ export async function preparePortableInstall(
): Promise<PreparedPortableUpdate> {
await assertWritableFolder(installDir);
await mkdir(updatesRoot, { recursive: true });
const rawFs = rawFsPromises();
const stagingDir = join(updatesRoot, `portable-staging-${randomUUID()}`);
await mkdir(stagingDir, { recursive: true });
try {
@@ -238,7 +253,7 @@ export async function preparePortableInstall(
}
for (const rel of newFiles) {
const stagedPath = resolveInside(stagingDir, rel);
const stats = await lstat(stagedPath);
const stats = await rawFs.lstat(stagedPath);
if (!stats.isFile()) throw new Error(`Staged portable entry is not a file: ${rel}`);
}
@@ -254,7 +269,7 @@ export async function preparePortableInstall(
installDir: resolve(installDir),
stagingDir,
archivePath,
logPath: join(updatesRoot, 'portable-update.log'),
logPath: join(updatesRoot, PORTABLE_UPDATE_LOG_FILENAME),
executable: LAUNCHER_EXECUTABLE,
newFiles,
staleFiles,
@@ -263,7 +278,7 @@ export async function preparePortableInstall(
await writeFile(scriptPath, `\uFEFF${buildPortableApplyScript()}\r\n`, 'utf8');
return { scriptPath, planPath, stagingDir };
} catch (error) {
await rm(stagingDir, { recursive: true, force: true }).catch(() => undefined);
await rawFs.rm(stagingDir, { recursive: true, force: true }).catch(() => undefined);
throw error;
}
}
@@ -7,6 +7,7 @@ import { zipSync } from 'fflate';
import { GAME_FILES } from '@wto/patcher';
import { AppError } from '@shared/errors.ts';
import type { InstallRecord } from '@shared/schemas/install.ts';
import { PORTABLE_UPDATE_LOG_FILENAME } from '../app-update/portable-update.ts';
import { getSettings } from '../config/settings-store.ts';
import { COPIES_FILENAME, readInstallRecord } from '../install/install-record.ts';
import { LAUNCHER_LOG_FILES, launcherLogFolder } from './launcher-log.ts';
@@ -58,6 +59,11 @@ export async function exportDiagnostics(window: BrowserWindow | null): Promise<{
await addFile(files, `launcher-logs/${name}`, join(launcherLogs, name));
}
}
await addFile(
files,
`launcher-logs/${PORTABLE_UPDATE_LOG_FILENAME}`,
join(userData, 'launcher-updates', PORTABLE_UPDATE_LOG_FILENAME),
);
const [record, settings] = await Promise.all([readInstallRecord(), getSettings()]);
const dataPath = (settings.developerMode ? settings.labDataPath : null) ?? record?.dataPath;
if (dataPath) await addServerLogs(files, dataPath);
+7
View File
@@ -163,6 +163,12 @@ async function main(): Promise<void> {
uninstallerIcon: join(stage, 'build', 'icon.ico'),
installerHeaderIcon: join(stage, 'build', 'icon.ico'),
},
// Drop Electron's unused fallback welcome archive so portable builds never ship it and
// already-released portable launchers (0.1.2 / 0.1.3) can update without tripping over
// Electron's .asar virtual-directory check.
afterPack: async (context) => {
await rm(join(context.appOutDir, 'resources', 'default_app.asar'), { force: true });
},
},
});
for (const artifact of artifacts) console.info(artifact);
@@ -200,6 +206,7 @@ async function packagePortableArchive(outputFolder: string, version: string): Pr
const inventoryPath = join(resources, 'portable-files.json');
const portable = join(outputFolder, portableLauncherReleaseFiles(version).archive);
try {
await rm(join(resources, 'default_app.asar'), { force: true });
await writeFile(markerPath, 'portable\n', 'utf8');
await writeFile(inventoryPath, '', 'utf8');
const ownedFiles = await listRelativeFiles(unpacked);