UNPKG

3.17 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const { downloadArtifact } = require('@electron/get');
4
5const extract = require('extract-zip');
6
7const childProcess = require('child_process');
8const fs = require('fs');
9const os = require('os');
10const path = require('path');
11
12const { version } = require('./package');
13
14if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) {
15 process.exit(0);
16}
17
18const platformPath = getPlatformPath();
19
20if (isInstalled()) {
21 process.exit(0);
22}
23
24const platform = process.env.npm_config_platform || process.platform;
25let arch = process.env.npm_config_arch || process.arch;
26
27if (platform === 'darwin' && process.platform === 'darwin' && arch === 'x64' &&
28 process.env.npm_config_arch === undefined) {
29 // When downloading for macOS ON macOS and we think we need x64 we should
30 // check if we're running under rosetta and download the arm64 version if appropriate
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 // Ignore failure
38 }
39}
40
41// downloads if not cached
42downloadArtifact({
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
55function 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// unzips and makes path.txt point at the correct executable
74function 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 // If the zip contains an "electron.d.ts" file,
79 // move that up
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 // Write a "path.txt" file.
89 return fs.promises.writeFile(path.join(__dirname, 'path.txt'), platformPath);
90 });
91}
92
93function 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}