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

93.94% Statements 31/33
88% Branches 22/25
100% Functions 2/2
93.94% Lines 31/33
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 801x   1x 11x   11x   11x   3x   11x   11x 3x 3x   1x 1x       11x   3x 2x 2x 1x 1x         3x         3x       3x           11x 11x   11x   2x 1x       1x       9x   11x 11x       11x                    
const fs = require('fs')
 
module.exports = (api, options = {}) => {
  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 so it should be created
    api.render('./template')
  }
  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 mainWindow\s*?$/m,
        'let mainWindow: any'
      )
      fs.writeFileSync(api.resolve('./src/background.ts'), background)
    }
  })
 
  // Add electron-builder install-app-deps to postinstall
  let postinstallScript
  let pkg = fs.readFileSync(api.resolve('./package.json'), 'utf8')
  pkg = JSON.parse(pkg)
  // Add on to existing script if it exists
  if (pkg.scripts && pkg.scripts.postinstall) {
    // Don't re-add script
    if (!pkg.scripts.postinstall.match('electron-builder install-app-deps')) {
      postinstallScript =
        pkg.scripts.postinstall + ' && electron-builder install-app-deps'
    } else {
      // electron-builder install-app-deps already exists
      postinstallScript = pkg.scripts.postinstall
    }
  } else {
    // Create new postinstall script
    postinstallScript = 'electron-builder install-app-deps'
  }
  const devDependencies = {}
  Iif (options.electronBuilder && options.electronBuilder.electronVersion) {
    // Use provided electron version
    devDependencies.electron = options.electronBuilder.electronVersion
  }
  api.extendPackage({
    scripts: {
      'electron:build': 'vue-cli-service electron:build',
      'electron:serve': 'vue-cli-service electron:serve',
      postinstall: postinstallScript
    },
    devDependencies,
    main: 'background.js'
  })
}