summaryrefslogtreecommitdiffstats
path: root/packaging/win/bump-electron.mjs
blob: 78189bfa91683a149b1deb580bc4cc3852bab962 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Bump the pinned Electron to the latest release, in package.json and
// package-lock.json, and keep the `allowScripts` key matching.
//
// Chromium CVEs are fixed in Electron releases; a client built against an old
// one ships those holes. This is the same policy packaging/build/build-client.sh
// applies on Linux -- factored out to a file so build-win.ps1 does not have to
// carry a PowerShell here-string (whose terminator rules make it fragile).
//
// Run from packages/meshbay-client. Writes both manifests and exits 0 whether
// or not a bump happened; exits non-zero only on an actual error. Prints the
// new version to stdout when it changed, nothing when it did not.

import { readFileSync, writeFileSync } from 'node:fs';
import { execFileSync } from 'node:child_process';

const pinned = JSON.parse(readFileSync('package-lock.json', 'utf8'))
  .packages['node_modules/electron'].version;

let latest;
try {
  latest = execFileSync('npm', ['view', 'electron', 'version'], { encoding: 'utf8' }).trim();
} catch {
  process.stderr.write('npm registry unreachable -- keeping Electron ' + pinned + '\n');
  process.exit(0);
}

if (latest === pinned) process.exit(0);

execFileSync('npm', ['install', '--save-dev', '--ignore-scripts', `electron@${latest}`],
  { stdio: 'inherit' });

const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
if (pkg.allowScripts) {
  for (const k of Object.keys(pkg.allowScripts)) {
    if (k.startsWith('electron@')) {
      delete pkg.allowScripts[k];
      pkg.allowScripts[`electron@${latest}`] = true;
    }
  }
  writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
}

process.stdout.write(latest + '\n');