All files / vue-cli-plugin-electron-builder/generator index.js

80.33% Statements 49/61
59.57% Branches 28/47
100% Functions 3/3
85.96% Lines 49/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 1331x 1x 1x   1x 14x 14x 14x 14x 14x   14x     14x       14x 14x 14x       14x   3x                     14x         14x   14x 3x 3x   1x 1x       14x   3x 2x 2x 1x 1x         3x         3x       3x   14x                 14x       14x   28x   4x   2x     2x       24x     14x 14x 14x 14x   14x   14x 14x                   14x     14x              
const fs = require('fs')
const semver = require('semver')
const { warn } = require('@vue/cli-shared-utils')
 
module.exports = (api, options = {}) => {
  Iif (!options.electronBuilder) options.electronBuilder = {}
  const electronVersion = options.electronBuilder.electronVersion
  let pkg = fs.readFileSync(api.resolve('./package.json'), 'utf8')
  pkg = JSON.parse(pkg)
  const usesTS = api.hasPlugin('typescript')
  const hasBackground =
    fs.existsSync(api.resolve('./src/background.ts')) ||
    fs.existsSync(api.resolve('./src/background.js'))
 
  const devtoolsExtensionsBroken = semver.gte(
    (electronVersion || pkg.devDependencies.electron).replace('^', ''),
    '6.0.0'
  )
  Eif (devtoolsExtensionsBroken) {
    warn('Devtools extensions are broken in Electron 6.0.0 and greater')
    warn(
      'Vue Devtools have been disabled, see the comments in your background file for more info'
    )
  }
  if (!hasBackground) {
    // If user does not have a background file it should be created
    api.render('./templates/base', {
      // Scheme registration changed in Electron 5.0.0
      newSchemeRegistrationFunction: semver.gte(
        (electronVersion || pkg.devDependencies.electron).replace('^', ''),
        '5.0.0'
      ),
      devtoolsExtensionsBroken
    })
  }
  // Add tests
  let testFramework
  Iif (options.electronBuilder.addTests) {
    if (api.hasPlugin('unit-mocha')) testFramework = 'mocha'
    if (api.hasPlugin('unit-jest')) testFramework = 'jest'
    if (testFramework) api.render(`./templates/tests-${testFramework}`)
  }
  api.onCreateComplete(() => {
    // Update .gitignore if it exists
    if (fs.existsSync(api.resolve('./.gitignore'))) {
      let gitignore = fs.readFileSync(api.resolve('./.gitignore'), 'utf8')
      if (!gitignore.match(/(#Electron-builder output|\/dist_electron)/)) {
        // Add /dist_electron to gitignore if it doesn't exist already
        gitignore = gitignore + '\n#Electron-builder output\n/dist_electron'
        fs.writeFileSync(api.resolve('./.gitignore'), gitignore)
      }
    }
 
    if (usesTS) {
      let background
      if (fs.existsSync(api.resolve('./src/background.js'))) {
        background = fs.readFileSync(api.resolve('./src/background.js'), 'utf8')
        fs.unlinkSync(api.resolve('./src/background.js'))
      } else Eif (fs.existsSync(api.resolve('./src/background.ts'))) {
        background = fs.readFileSync(api.resolve('./src/background.ts'), 'utf8')
      } else {
        // Exit if background file cannot be found
        return
      }
      background = background.replace(
        // Add types if they don't exist
        /process\.env\.WEBPACK_DEV_SERVER_URL\s*?\)$/m,
        'process.env.WEBPACK_DEV_SERVER_URL as string)'
      )
      background = background.replace(
        /let win\s*?$/m,
        'let win: BrowserWindow | null'
      )
      fs.writeFileSync(api.resolve('./src/background.ts'), background)
    }
    Iif (api.hasPlugin('router')) {
      console.log('\n')
      require('@vue/cli-shared-utils/lib/logger').warn(
        'It is detected that you are using Vue Router. If you are using history mode, you must push the default route when the root component is loaded. Learn more at https://goo.gl/GM1xZG .'
      )
    }
  })
 
  // Add electron-builder install-app-deps to postinstall and postuninstall
  const scripts = {
    'electron:build': 'vue-cli-service electron:build',
    'electron:serve': 'vue-cli-service electron:serve'
  }
  const addScript = (name, command) => {
    // Add on to existing script if it exists
    if (pkg.scripts && pkg.scripts[name]) {
      // Don't re-add script
      if (!pkg.scripts[name].match(command)) {
        // add command to existing script
        scripts[name] = pkg.scripts[name] + ` && ${command}`
      } else {
        // command already exists, don't change it
        scripts[name] = pkg.scripts[name]
      }
    } else {
      // Create new postinstall script
      scripts[name] = command
    }
  }
  addScript('postinstall', 'electron-builder install-app-deps')
  addScript('postuninstall', 'electron-builder install-app-deps')
  const devDependencies = {}
  Eif (electronVersion) {
    // Use provided electron version
    devDependencies.electron = electronVersion
  }
  const dependencies = {}
  Iif (testFramework) {
    // Spectron version should be electron version + 2
    devDependencies.spectron =
      '^' +
      (parseInt(
        (electronVersion || pkg.devDependencies.electron).match(/^\^(\d*)\./)[1]
      ) +
        2) +
      '.0.0'
  }
  Iif (testFramework === 'mocha') {
    dependencies['chai-as-promised'] = '^7.1.1'
  }
  api.extendPackage({
    scripts,
    dependencies,
    devDependencies,
    main: 'background.js'
  })
}