index.js 4.23 KB
Newer Older
huahua committed
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
const fs = require('fs')
const { warn } = require('@vue/cli-shared-utils')

module.exports = (api, options = {}) => {
  if (!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'))

  if (!hasBackground) {
    // If user does not have a background file it should be created
    api.render('./templates/base', {
      spectronSupport: options.electronBuilder.addTests,
      vue3: /^(\^?)3/.test(((pkg || {}).dependencies || {}).vue)
    })
  }
  // Add tests
  let testFramework
  if (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 if (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(
        'process.env.ELECTRON_NODE_INTEGRATION',
        '(process.env\n          .ELECTRON_NODE_INTEGRATION as unknown) as boolean'
      )
      fs.writeFileSync(api.resolve('./src/background.ts'), background)
    }
    if (api.hasPlugin('router')) {
      console.log('\n')
      warn(
        'It is detected that you are using Vue Router. It must function in hash mode to work in Electron. 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 = {
    'electron-devtools-installer': '^3.1.0'
  }
  if (electronVersion) {
    // Use provided electron version
    devDependencies.electron = electronVersion
  }
  if (usesTS) {
    devDependencies['@types/electron-devtools-installer'] = '^2.2.0'
  }
  const dependencies = {}
  if (testFramework) {
    // Spectron version should be electron version + 2
    devDependencies.spectron =
      parseInt(
        (electronVersion || pkg.devDependencies.electron).match(/^\^(\d*)\./)[1]
      ) +
      2 +
      '.0.0'
  }
  if (testFramework === 'mocha') {
    devDependencies['chai-as-promised'] = '^7.1.1'
  }
  api.extendPackage({
    scripts,
    dependencies,
    devDependencies,
    main: 'background.js'
  })
}