1 | #!/usr/bin/env node
|
2 |
|
3 | const { downloadArtifact } = require('@electron/get');
|
4 |
|
5 | const extract = require('extract-zip');
|
6 |
|
7 | const childProcess = require('child_process');
|
8 | const fs = require('fs');
|
9 | const os = require('os');
|
10 | const path = require('path');
|
11 |
|
12 | const { version } = require('./package');
|
13 |
|
14 | if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) {
|
15 | process.exit(0);
|
16 | }
|
17 |
|
18 | const platformPath = getPlatformPath();
|
19 |
|
20 | if (isInstalled()) {
|
21 | process.exit(0);
|
22 | }
|
23 |
|
24 | const platform = process.env.npm_config_platform || process.platform;
|
25 | let arch = process.env.npm_config_arch || process.arch;
|
26 |
|
27 | if (platform === 'darwin' && process.platform === 'darwin' && arch === 'x64' &&
|
28 | process.env.npm_config_arch === undefined) {
|
29 |
|
30 |
|
31 | try {
|
32 | const output = childProcess.execSync('sysctl -in sysctl.proc_translated');
|
33 | if (output.toString().trim() === '1') {
|
34 | arch = 'arm64';
|
35 | }
|
36 | } catch {
|
37 |
|
38 | }
|
39 | }
|
40 |
|
41 |
|
42 | downloadArtifact({
|
43 | version,
|
44 | artifactName: 'electron',
|
45 | force: process.env.force_no_cache === 'true',
|
46 | cacheRoot: process.env.electron_config_cache,
|
47 | checksums: process.env.electron_use_remote_checksums ?? process.env.npm_config_electron_use_remote_checksums ? undefined : require('./checksums.json'),
|
48 | platform,
|
49 | arch
|
50 | }).then(extractFile).catch(err => {
|
51 | console.error(err.stack);
|
52 | process.exit(1);
|
53 | });
|
54 |
|
55 | function isInstalled () {
|
56 | try {
|
57 | if (fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '') !== version) {
|
58 | return false;
|
59 | }
|
60 |
|
61 | if (fs.readFileSync(path.join(__dirname, 'path.txt'), 'utf-8') !== platformPath) {
|
62 | return false;
|
63 | }
|
64 | } catch {
|
65 | return false;
|
66 | }
|
67 |
|
68 | const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(__dirname, 'dist', platformPath);
|
69 |
|
70 | return fs.existsSync(electronPath);
|
71 | }
|
72 |
|
73 |
|
74 | function extractFile (zipPath) {
|
75 | const distPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(__dirname, 'dist');
|
76 |
|
77 | return extract(zipPath, { dir: path.join(__dirname, 'dist') }).then(() => {
|
78 |
|
79 |
|
80 | const srcTypeDefPath = path.join(distPath, 'electron.d.ts');
|
81 | const targetTypeDefPath = path.join(__dirname, 'electron.d.ts');
|
82 | const hasTypeDefinitions = fs.existsSync(srcTypeDefPath);
|
83 |
|
84 | if (hasTypeDefinitions) {
|
85 | fs.renameSync(srcTypeDefPath, targetTypeDefPath);
|
86 | }
|
87 |
|
88 |
|
89 | return fs.promises.writeFile(path.join(__dirname, 'path.txt'), platformPath);
|
90 | });
|
91 | }
|
92 |
|
93 | function getPlatformPath () {
|
94 | const platform = process.env.npm_config_platform || os.platform();
|
95 |
|
96 | switch (platform) {
|
97 | case 'mas':
|
98 | case 'darwin':
|
99 | return 'Electron.app/Contents/MacOS/Electron';
|
100 | case 'freebsd':
|
101 | case 'openbsd':
|
102 | case 'linux':
|
103 | return 'electron';
|
104 | case 'win32':
|
105 | return 'electron.exe';
|
106 | default:
|
107 | throw new Error('Electron builds are not available on platform: ' + platform);
|
108 | }
|
109 | }
|