1 | #!/usr/bin/env node
|
2 |
|
3 | const {version} = require('./package')
|
4 |
|
5 | const fs = require('fs')
|
6 | const os = require('os')
|
7 | const path = require('path')
|
8 | const extract = require('extract-zip')
|
9 | const { downloadArtifact } = require('@electron/get')
|
10 |
|
11 | if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) {
|
12 | process.exit(0)
|
13 | }
|
14 |
|
15 | const platformPath = getPlatformPath()
|
16 |
|
17 | if (isInstalled()) {
|
18 | process.exit(0)
|
19 | }
|
20 |
|
21 |
|
22 | downloadArtifact({
|
23 | version,
|
24 | artifactName: 'electron',
|
25 | force: process.env.force_no_cache === 'true',
|
26 | cacheRoot: process.env.electron_config_cache,
|
27 | platform: process.env.npm_config_platform || process.platform,
|
28 | arch: process.env.npm_config_arch || process.arch
|
29 | }).then(extractFile).catch(err => {
|
30 | console.error(err.stack)
|
31 | process.exit(1)
|
32 | })
|
33 |
|
34 | function isInstalled () {
|
35 | try {
|
36 | if (fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '') !== version) {
|
37 | return false
|
38 | }
|
39 |
|
40 | if (fs.readFileSync(path.join(__dirname, 'path.txt'), 'utf-8') !== platformPath) {
|
41 | return false
|
42 | }
|
43 | } catch (ignored) {
|
44 | return false
|
45 | }
|
46 |
|
47 | const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(__dirname, 'dist', platformPath)
|
48 |
|
49 | return fs.existsSync(electronPath)
|
50 | }
|
51 |
|
52 |
|
53 | function extractFile (zipPath) {
|
54 | return new Promise((resolve, reject) => {
|
55 | extract(zipPath, { dir: path.join(__dirname, 'dist') }, err => {
|
56 | if (err) return reject(err)
|
57 |
|
58 | fs.writeFile(path.join(__dirname, 'path.txt'), platformPath, err => {
|
59 | if (err) return reject(err)
|
60 |
|
61 | resolve()
|
62 | })
|
63 | })
|
64 | })
|
65 | }
|
66 |
|
67 | function getPlatformPath () {
|
68 | const platform = process.env.npm_config_platform || os.platform()
|
69 |
|
70 | switch (platform) {
|
71 | case 'mas':
|
72 | case 'darwin':
|
73 | return 'Electron.app/Contents/MacOS/Electron'
|
74 | case 'freebsd':
|
75 | case 'openbsd':
|
76 | case 'linux':
|
77 | return 'electron'
|
78 | case 'win32':
|
79 | return 'electron.exe'
|
80 | default:
|
81 | throw new Error('Electron builds are not available on platform: ' + platform)
|
82 | }
|
83 | }
|