#!/usr/bin/env node

const version = require('./package').version

const fs = require('fs')
const os = require('os')
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)) {
  process.exit(0)
}

// 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((zipPath) => extractFile(zipPath)).catch((err) => onerror(err))

// 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)
    })
  })
}

function onerror (err) {
  throw err
}

function getPlatformPath () {
  const platform = process.env.npm_config_platform || os.platform()

  switch (platform) {
    case 'darwin':
      return 'Electron.app/Contents/MacOS/Electron'
    case 'freebsd':
    case 'openbsd':
    case 'linux':
      return 'electron'
    case 'win32':
      return 'electron.exe'
    default:
      throw new Error('Electron builds are not available on platform: ' + platform)
  }
}

@@ -237,186 +237,8 @@
 ')%0A%0A
-let installedVersion = null%0Atry %7B%0A  installedVersion = fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/%5Ev/, '')%0A%7D catch (ignored) %7B%0A  // do nothing%0A%7D%0A%0A
 if (
@@ -347,129 +347,23 @@
 ()%0A%0A
-const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH %7C%7C path.join(__dirname, 'dist', platformPath)%0A%0A
 if (i
+sI
 nstalled
 Vers
@@ -362,57 +362,9 @@
 lled
-Version === version && fs.existsSync(electronPath
+(
 )) %7B
@@ -695,71 +695,560 @@
 hen(
-(zipPath) =%3E extractFile(zipPath)).catch((err) =%3E onerror(err))
+extractFile).catch(err =%3E %7B%0A  console.error(err.stack)%0A  process.exit(1)%0A%7D)%0A%0Afunction isInstalled () %7B%0A  try %7B%0A    if (fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/%5Ev/, '') !== version) %7B%0A      return false%0A    %7D%0A%0A    if (fs.readFileSync(path.join(__dirname, 'path.txt'), 'utf-8') !== platformPath) %7B%0A      return false%0A    %7D%0A  %7D catch (ignored) %7B%0A    return false%0A  %7D%0A  %0A  const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH %7C%7C path.join(__dirname, 'dist', platformPath)%0A  %0A  return fs.existsSync(electronPath)%0A%7D
 %0A%0A//
@@ -1339,16 +1339,62 @@
 Path) %7B%0A
+  return new Promise((resolve, reject) =%3E %7B%0A  
   extrac
@@ -1443,33 +1443,27 @@
 st') %7D, 
-function (err)
+err =%3E
  %7B%0A
+  
     if (
@@ -1470,37 +1470,39 @@
 err) return 
-onerror
+reject
 (err)%0A
+%0A  
     fs.write
@@ -1558,25 +1558,19 @@
 th, 
-function (err)
+err =%3E
  %7B%0A
+  
     
@@ -1591,72 +1591,59 @@
 urn 
-onerror
+reject
 (err)%0A
+%0A
     
-%7D)%0A  %7D)%0A%7D%0A%0Afunction onerror (err) %7B%0A  throw err
+    resolve()%0A      %7D)%0A    %7D)%0A  %7D)
 %0A%7D%0A%0A

improve error handling in npm install (#21589)