fix: improve error handling in npm install (#21589)
* fix: improve error handling in npm install * chore: simplify logic
This commit is contained in:
parent
b41fb2e554
commit
85382d8f1d
1 changed files with 32 additions and 19 deletions
|
@ -8,22 +8,13 @@ const path = require('path')
|
|||
const extract = require('extract-zip')
|
||||
const { downloadArtifact } = require('@electron/get')
|
||||
|
||||
let installedVersion = null
|
||||
try {
|
||||
installedVersion = fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '')
|
||||
} catch (ignored) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
const platformPath = getPlatformPath()
|
||||
|
||||
const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(__dirname, 'dist', platformPath)
|
||||
|
||||
if (installedVersion === version && fs.existsSync(electronPath)) {
|
||||
if (isInstalled()) {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
|
@ -35,22 +26,44 @@ downloadArtifact({
|
|||
cacheRoot: process.env.electron_config_cache,
|
||||
platform: process.env.npm_config_platform || process.platform,
|
||||
arch: process.env.npm_config_arch || process.arch
|
||||
}).then((zipPath) => extractFile(zipPath)).catch((err) => onerror(err))
|
||||
}).then(extractFile).catch(err => {
|
||||
console.error(err.stack)
|
||||
process.exit(1)
|
||||
})
|
||||
|
||||
function isInstalled () {
|
||||
try {
|
||||
if (fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '') !== version) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (fs.readFileSync(path.join(__dirname, 'path.txt'), 'utf-8') !== platformPath) {
|
||||
return false
|
||||
}
|
||||
} catch (ignored) {
|
||||
return false
|
||||
}
|
||||
|
||||
const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(__dirname, 'dist', platformPath)
|
||||
|
||||
return fs.existsSync(electronPath)
|
||||
}
|
||||
|
||||
// unzips and makes path.txt point at the correct executable
|
||||
function extractFile (zipPath) {
|
||||
extract(zipPath, { dir: path.join(__dirname, 'dist') }, function (err) {
|
||||
if (err) return onerror(err)
|
||||
fs.writeFile(path.join(__dirname, 'path.txt'), platformPath, function (err) {
|
||||
if (err) return onerror(err)
|
||||
return new Promise((resolve, reject) => {
|
||||
extract(zipPath, { dir: path.join(__dirname, 'dist') }, err => {
|
||||
if (err) return reject(err)
|
||||
|
||||
fs.writeFile(path.join(__dirname, 'path.txt'), platformPath, err => {
|
||||
if (err) return reject(err)
|
||||
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function onerror (err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
function getPlatformPath () {
|
||||
const platform = process.env.npm_config_platform || os.platform()
|
||||
|
||||
|
|
Loading…
Reference in a new issue