const fs = require('fs')
const
  appPath = require('../app-paths'),
  packagerVersion = '13.1.0',
  getPackageJson = require('../helpers/get-package-json'),
  getPackage = require('../helpers/get-package'),
  log = require('../helpers/logger')('app:electron-bundle')

function isValidName (bundlerName) {
  return ['packager', 'builder'].includes(bundlerName)
}

function installBundler (bundlerName) {
  const
    { spawnSync } = require('../helpers/spawn'),
    nodePackager = require('../helpers/node-packager'),
    version = bundlerName === 'packager' ? `^${packagerVersion}` : 'latest',
    cmdParam = nodePackager === 'npm'
      ? ['install', '--save-dev']
      : ['add', '--dev']

  log(`Installing required Electron bundler (electron-${bundlerName})...`)
  spawnSync(
    nodePackager,
    cmdParam.concat([`electron-${bundlerName}@${version}`]),
    appPath.appDir,
    () => warn(`Failed to install electron-${bundlerName}`)
  )
}

function isInstalled (bundlerName) {
  return getPackageJson(`electron-${bundlerName}`) !== void 0
}

module.exports.ensureInstall = function (bundlerName) {
  if (!isValidName(bundlerName)) {
    warn(`⚠️  Unknown bundler "${ bundlerName }" for Electron`)
    warn()
    process.exit(1)
  }

  if (bundlerName === 'packager') {
    if (isInstalled('packager')) {
      const
        semver = require('semver'),
        pkg = getPackageJson(`electron-${bundlerName}`)

      if (semver.satisfies(pkg.version, `>= ${packagerVersion}`)) {
        return
      }
    }
  }
  else if (isInstalled('builder')) {
    return
  }

  installBundler(bundlerName)
}

module.exports.getDefaultName = function () {
  if (isInstalled('packager')) {
    return 'packager'
  }

  if (isInstalled('builder')) {
    return 'builder'
  }

  return 'packager'
}

module.exports.getBundler = function (bundlerName) {
  return getPackage(`electron-${bundlerName}`)
}

module.exports.ensureBuilderCompatibility = function () {
  if (fs.existsSync(appPath.resolve.electron('icons/linux-256x256.png'))) {
    console.log()
    console.log(`\n⚠️  electron-builder requires a change to your src-electron/icons folder:
  * replace linux-256x256.png with a 512x512 px png file named "linux-512x512.png"
  * make sure to delete the old linux-256x256.png file
`)
    console.log()
    process.exit(1)
  }
}

@@ -65,38 +65,8 @@
 '),%0A
-  packagerVersion = '13.1.0',%0A
   ge
@@ -231,16 +231,80 @@
 ndle')%0A%0A
+const versions = %7B%0A  packager: '13.1.0',%0A  builder: '21.0.0'%0A%7D%0A%0A
 function
@@ -547,85 +547,8 @@
 '),%0A
-    version = bundlerName === 'packager' ? %60%5E$%7BpackagerVersion%7D%60 : 'latest',%0A
     
@@ -795,23 +795,43 @@
 Name%7D@$%7B
+'%5E' + 
 version
+s%5BbundlerName%5D
 %7D%60%5D),%0A  
@@ -1021,282 +1021,53 @@
 %0A%7D%0A%0A
-module.exports.ensureInstall = function (bundlerName) %7B%0A  if (!isValidName(bundlerName)) %7B%0A    warn(%60%E2%9A%A0%EF%B8%8F  Unknown bundler %22$%7B bundlerName %7D%22 for Electron%60)%0A    warn()%0A    process.exit(1)%0A  %7D%0A%0A  if (bundlerName === 'packager') %7B%0A    if (isInstalled('packager')) %7B%0A    
+function versionIsOk (bundlerName) %7B%0A
   const%0A
     
@@ -1062,20 +1062,16 @@
   const%0A
-    
     semv
@@ -1094,20 +1094,16 @@
 mver'),%0A
-    
     pkg 
@@ -1147,20 +1147,16 @@
 ame%7D%60)%0A%0A
-    
   if (se
@@ -1193,74 +1193,257 @@
 = $%7B
-packagerVersion%7D%60)) %7B%0A        return%0A      %7D%0A    %7D%0A  %7D%0A  else
+versions%5BbundlerName%5D%7D%60)) %7B%0A    return true%0A  %7D%0A%7D%0A%0Amodule.exports.ensureInstall = function (bundlerName) %7B%0A  if (!isValidName(bundlerName)) %7B%0A    warn(%60%E2%9A%A0%EF%B8%8F  Unknown bundler %22$%7B bundlerName %7D%22 for Electron%60)%0A    warn()%0A    process.exit(1)%0A  %7D%0A%0A 
  if (
+!
 isIn
@@ -1454,38 +1454,55 @@
 led(
-'
 bu
-ilder')) %7B%0A    return%0A  %7D%0A%0A
+ndlerName) %7C%7C !versionIsOk(bundlerName)) %7B%0A  
   in
@@ -1527,16 +1527,20 @@
 erName)%0A
+  %7D%0A
 %7D%0A%0Amodul

Install required electron-builder version if necessary #4703