Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
},
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
91 changes: 91 additions & 0 deletions scripts/package-win.mjs
Original file line number Diff line number Diff line change
@@ -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/<version>/.
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 })
Loading