UNPKG

1.89 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const version = require('./package').version
4
5const fs = require('fs')
6const os = require('os')
7const path = require('path')
8const extract = require('extract-zip')
9const { downloadArtifact } = require('@electron/get')
10
11let installedVersion = null
12try {
13 installedVersion = fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '')
14} catch (ignored) {
15 // do nothing
16}
17
18if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) {
19 process.exit(0)
20}
21
22const platformPath = getPlatformPath()
23
24const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(__dirname, 'dist', platformPath)
25
26if (installedVersion === version && fs.existsSync(electronPath)) {
27 process.exit(0)
28}
29
30// downloads if not cached
31downloadArtifact({
32 version,
33 artifactName: 'electron',
34 force: process.env.force_no_cache === 'true',
35 cacheRoot: process.env.electron_config_cache,
36 platform: process.env.npm_config_platform || process.platform,
37 arch: process.env.npm_config_arch || process.arch
38}).then((zipPath) => extractFile(zipPath)).catch((err) => onerror(err))
39
40// unzips and makes path.txt point at the correct executable
41function extractFile (zipPath) {
42 extract(zipPath, { dir: path.join(__dirname, 'dist') }, function (err) {
43 if (err) return onerror(err)
44 fs.writeFile(path.join(__dirname, 'path.txt'), platformPath, function (err) {
45 if (err) return onerror(err)
46 })
47 })
48}
49
50function onerror (err) {
51 throw err
52}
53
54function getPlatformPath () {
55 const platform = process.env.npm_config_platform || os.platform()
56
57 switch (platform) {
58 case 'darwin':
59 return 'Electron.app/Contents/MacOS/Electron'
60 case 'freebsd':
61 case 'openbsd':
62 case 'linux':
63 return 'electron'
64 case 'win32':
65 return 'electron.exe'
66 default:
67 throw new Error('Electron builds are not available on platform: ' + platform)
68 }
69}