diff options
Diffstat (limited to 'scripts/update-electron-vendors.js')
-rw-r--r-- | scripts/update-electron-vendors.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/scripts/update-electron-vendors.js b/scripts/update-electron-vendors.js new file mode 100644 index 0000000..650d394 --- /dev/null +++ b/scripts/update-electron-vendors.js | |||
@@ -0,0 +1,55 @@ | |||
1 | #!/usr/bin/env node | ||
2 | |||
3 | // @ts-check | ||
4 | |||
5 | const { execSync } = require('child_process'); | ||
6 | const electronPath = require('electron'); | ||
7 | const { writeFile } = require('fs/promises'); | ||
8 | const path = require('path'); | ||
9 | |||
10 | /** | ||
11 | * Returns versions of electron vendors | ||
12 | * The performance of this feature is very poor and can be improved | ||
13 | * @see https://github.com/electron/electron/issues/28006 | ||
14 | * | ||
15 | * @returns {NodeJS.ProcessVersions} | ||
16 | */ | ||
17 | function getVendors() { | ||
18 | const output = execSync(`${electronPath} -p "JSON.stringify(process.versions)"`, { | ||
19 | env: {'ELECTRON_RUN_AS_NODE': '1'}, | ||
20 | encoding: 'utf-8', | ||
21 | }); | ||
22 | |||
23 | return JSON.parse(output); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Generates the `.browserlistrc` and `.electron-vendors.cache.json` files. | ||
28 | * | ||
29 | * @returns Promise<void> | ||
30 | */ | ||
31 | function updateVendors() { | ||
32 | const electronRelease = getVendors(); | ||
33 | |||
34 | const nodeMajorVersion = electronRelease.node.split('.')[0]; | ||
35 | const chromeMajorVersion = electronRelease.v8.split('.')[0] + electronRelease.v8.split('.')[1]; | ||
36 | |||
37 | const browserslistrcPath = path.resolve(process.cwd(), '.browserslistrc'); | ||
38 | |||
39 | return Promise.all([ | ||
40 | writeFile( | ||
41 | './.electron-vendors.cache.json', | ||
42 | JSON.stringify({ | ||
43 | chrome: chromeMajorVersion, | ||
44 | node: nodeMajorVersion, | ||
45 | }, null, 2) + '\n', | ||
46 | ), | ||
47 | |||
48 | writeFile(browserslistrcPath, `Chrome ${chromeMajorVersion}\n`, 'utf8'), | ||
49 | ]); | ||
50 | } | ||
51 | |||
52 | updateVendors().catch((err) => { | ||
53 | console.error(err); | ||
54 | process.exit(1); | ||
55 | }); | ||