aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/updateElectronVendors.js
blob: 5e8ab36a11a83307e0e70b555a5db60bd27567b4 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { execSync } from 'node:child_process';
import { writeFile } from 'node:fs/promises';
import path from 'node:path';

import electronPath from 'electron';

import fileUrlToDirname from '../config/fileUrlToDirname.js';

const thisDir = fileUrlToDirname(import.meta.url);

/**
 * Returns versions of electron vendors
 * The performance of this feature is very poor and can be improved
 * @see https://github.com/electron/electron/issues/28006
 *
 * @returns {NodeJS.ProcessVersions}
 */
function getVendors() {
  const output = execSync(
    `${electronPath.toString()} -p "JSON.stringify(process.versions)"`,
    {
      env: { ELECTRON_RUN_AS_NODE: '1' },
      encoding: 'utf-8',
    },
  );

  // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- Read untyped output.
  return JSON.parse(output);
}

/**
 * Generates the `.browserlistrc` and `.electron-vendors.cache.json` files.
 *
 * @returns Promise<void>
 */
function updateVendors() {
  const electronRelease = getVendors();

  const nodeMajorVersion = electronRelease.node.split('.')[0];
  const chromeMajorVersion =
    electronRelease.v8.split('.')[0] + electronRelease.v8.split('.')[1];

  const browserslistrcPath = path.join(thisDir, '../.browserslistrc');

  return Promise.all([
    writeFile(
      path.join(thisDir, '../.electron-vendors.cache.json'),
      `${JSON.stringify(
        {
          chrome: chromeMajorVersion,
          node: nodeMajorVersion,
        },
        undefined,
        2,
      )}\n`,
    ),

    writeFile(browserslistrcPath, `Chrome ${chromeMajorVersion}\n`, 'utf8'),
  ]);
}

updateVendors().catch((error) => {
  console.error(error);
  process.exit(1);
});