electron/npm/install.js

84 lines
2.1 KiB
JavaScript
Raw Normal View History

2014-10-20 04:17:38 +00:00
#!/usr/bin/env node
2020-10-21 22:43:52 +00:00
const { version } = require('./package');
2015-05-10 20:54:06 +00:00
2020-10-21 22:43:52 +00:00
const fs = require('fs');
const os = require('os');
const path = require('path');
const extract = require('extract-zip');
const { downloadArtifact } = require('@electron/get');
2015-05-10 20:54:06 +00:00
if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) {
2020-10-21 22:43:52 +00:00
process.exit(0);
}
2020-10-21 22:43:52 +00:00
const platformPath = getPlatformPath();
2014-10-20 04:56:49 +00:00
if (isInstalled()) {
2020-10-21 22:43:52 +00:00
process.exit(0);
2015-12-09 15:16:56 +00:00
}
// downloads if not cached
downloadArtifact({
version,
artifactName: 'electron',
force: process.env.force_no_cache === 'true',
cacheRoot: process.env.electron_config_cache,
platform: process.env.npm_config_platform || process.platform,
arch: process.env.npm_config_arch || process.arch
}).then(extractFile).catch(err => {
2020-10-21 22:43:52 +00:00
console.error(err.stack);
process.exit(1);
});
function isInstalled () {
try {
if (fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '') !== version) {
2020-10-21 22:43:52 +00:00
return false;
}
if (fs.readFileSync(path.join(__dirname, 'path.txt'), 'utf-8') !== platformPath) {
2020-10-21 22:43:52 +00:00
return false;
}
} catch (ignored) {
2020-10-21 22:43:52 +00:00
return false;
}
2020-10-21 22:43:52 +00:00
const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(__dirname, 'dist', platformPath);
return fs.existsSync(electronPath);
}
2015-05-07 04:44:10 +00:00
2015-05-10 20:54:06 +00:00
// unzips and makes path.txt point at the correct executable
function extractFile (zipPath) {
return new Promise((resolve, reject) => {
extract(zipPath, { dir: path.join(__dirname, 'dist') }, err => {
2020-10-21 22:43:52 +00:00
if (err) return reject(err);
fs.writeFile(path.join(__dirname, 'path.txt'), platformPath, err => {
2020-10-21 22:43:52 +00:00
if (err) return reject(err);
2020-10-21 22:43:52 +00:00
resolve();
});
});
});
2015-05-07 04:44:10 +00:00
}
2016-12-27 19:48:14 +00:00
function getPlatformPath () {
2020-10-21 22:43:52 +00:00
const platform = process.env.npm_config_platform || os.platform();
2016-12-27 19:48:14 +00:00
switch (platform) {
case 'mas':
2016-12-27 19:48:14 +00:00
case 'darwin':
2020-10-21 22:43:52 +00:00
return 'Electron.app/Contents/MacOS/Electron';
2016-12-27 19:48:14 +00:00
case 'freebsd':
case 'openbsd':
2016-12-27 19:48:14 +00:00
case 'linux':
2020-10-21 22:43:52 +00:00
return 'electron';
2016-12-27 19:48:14 +00:00
case 'win32':
2020-10-21 22:43:52 +00:00
return 'electron.exe';
2016-12-27 19:48:14 +00:00
default:
2020-10-21 22:43:52 +00:00
throw new Error('Electron builds are not available on platform: ' + platform);
2016-12-27 19:48:14 +00:00
}
}