From 38a70d668ebba7802d9f052cc1090317b1331efd Mon Sep 17 00:00:00 2001 From: Emirhan Gueler <38109072+Murbal@users.noreply.github.com> Date: Thu, 26 Nov 2026 21:00:48 +0100 Subject: [PATCH] Add Windows packaging script that works from WSL - Replace the `package` script with `package:win`, which runs scripts/package-win.mjs - Under WSL, stage the built app in the Windows temp dir and run electron-builder on the host, since Wine is needed for the NSIS uninstaller on Linux - Copy the resulting installer back into release// - Lint config covers scripts/**/*.mjs; README documents the new command --- README.md | 8 ++-- eslint.config.mjs | 2 +- package.json | 2 +- scripts/package-win.mjs | 91 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 scripts/package-win.mjs diff --git a/README.md b/README.md index 6d13a68..a1bbcc1 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,10 @@ Requires Node.js 22.12+ (CI uses Node 24). ```bash npm install -npm run dev # electron-vite dev server with hot reload -npm run build # production bundles in out/ -npm start # run the production build -npm run package # Windows x64 NSIS installer in release/ (package:linux for an AppImage, package:mac for an Apple Silicon DMG) +npm run dev # electron-vite dev server with hot reload +npm run build # production bundles in out/ +npm start # run the production build +npm run package:win # Windows x64 NSIS installer in release/ (works from WSL too; package:linux for an AppImage, package:mac for an Apple Silicon DMG) ``` The Electron binary is downloaded on the first run. diff --git a/eslint.config.mjs b/eslint.config.mjs index 4e5032f..e3d8bd3 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -17,7 +17,7 @@ export default tseslint.config( } }, { - files: ['src/main/**/*.ts', 'src/preload/**/*.ts', '*.config.{ts,mjs}', 'tests/**/*.ts'], + files: ['src/main/**/*.ts', 'src/preload/**/*.ts', '*.config.{ts,mjs}', 'scripts/**/*.mjs', 'tests/**/*.ts'], languageOptions: { globals: globals.node } }, { diff --git a/package.json b/package.json index 3f16f86..f635b96 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "lint": "eslint .", "test": "vitest run", "test:watch": "vitest", - "package": "npm run build && electron-builder --win --x64", + "package:win": "npm run build && node scripts/package-win.mjs", "package:linux": "npm run build && electron-builder --linux", "package:mac": "npm run build && electron-builder --mac --arm64", "build:test": "cross-env VITE_PHOTOEARTH_TEST=1 electron-vite build" diff --git a/scripts/package-win.mjs b/scripts/package-win.mjs new file mode 100644 index 0000000..90e8041 --- /dev/null +++ b/scripts/package-win.mjs @@ -0,0 +1,91 @@ +// Builds the Windows x64 NSIS installer. Run after `electron-vite build` (see `npm run package:win`). +// +// On Windows, macOS or plain Linux this just runs electron-builder. Under WSL it doesn't +// work in place: building the NSIS installer means running the generated uninstaller, which +// electron-builder does through Wine on Linux. So under WSL the built app is copied to the +// Windows host's temp dir, electron-builder runs there with the host's Node, and the +// installer is copied back into release//. +import { execFileSync, spawnSync } from 'node:child_process' +import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync } from 'node:fs' +import { join, resolve } from 'node:path' + +const root = resolve(import.meta.dirname, '..') +const builderArgs = ['--win', '--x64'] + +function isWsl() { + if (process.platform !== 'linux') return false + if (process.env.WSL_DISTRO_NAME) return true + try { + return /microsoft/i.test(readFileSync('/proc/version', 'utf8')) + } catch { + return false + } +} + +function run(cmd, args, opts = {}) { + const result = spawnSync(cmd, args, { stdio: 'inherit', ...opts }) + if (result.error) throw result.error + if (result.status !== 0) process.exit(result.status ?? 1) +} + +function versionOf(pkg) { + return JSON.parse(readFileSync(join(root, 'node_modules', pkg, 'package.json'), 'utf8')).version +} + +if (!isWsl()) { + run(process.platform === 'win32' ? 'npx.cmd' : 'npx', ['electron-builder', ...builderArgs], { + cwd: root, + shell: process.platform === 'win32' + }) + process.exit(0) +} + +if (!existsSync(join(root, 'out', 'main', 'index.js'))) { + console.error('out/ is missing; run `npm run build` first.') + process.exit(1) +} + +// cmd.exe complains when started from a \\wsl.localhost cwd, so run it from the Windows temp dir. +const winTemp = execFileSync('cmd.exe', ['/d', '/c', 'echo %TEMP%'], { cwd: '/mnt/c', encoding: 'utf8' }).trim() +const stage = join(execFileSync('wslpath', ['-u', winTemp], { encoding: 'utf8' }).trim(), 'photoearth-package') + +console.log(`Staging the app on the Windows host: ${stage}`) +rmSync(stage, { recursive: true, force: true }) +mkdirSync(stage, { recursive: true }) +for (const entry of ['out', 'build', 'electron-builder.yml', 'package.json']) { + cpSync(join(root, entry), join(stage, entry), { + recursive: true, + filter: (src) => !src.endsWith('.map') + }) +} + +// No node_modules on the host: pin electron-builder and Electron to the versions installed here. +run( + 'cmd.exe', + [ + '/d', + '/c', + 'npx.cmd', + '-y', + `electron-builder@${versionOf('electron-builder')}`, + ...builderArgs, + `-c.electronVersion=${versionOf('electron')}` + ], + { cwd: stage } +) + +const version = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')).version +const from = join(stage, 'release', version) +const to = join(root, 'release', version) +mkdirSync(to, { recursive: true }) +// A failed Linux build leaves a stub installer plus a loose .7z payload; drop them. +for (const name of readdirSync(to)) { + if (name.endsWith('.nsis.7z') || /^PhotoEarth Setup .*\.exe(\.blockmap)?$/.test(name)) rmSync(join(to, name)) +} +for (const name of readdirSync(from)) { + if (name.endsWith('.exe') || name.endsWith('.blockmap')) { + cpSync(join(from, name), join(to, name)) + console.log(`Copied ${join('release', version, name)}`) + } +} +rmSync(stage, { recursive: true, force: true })