electron.build.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // https://www.electron.build/tutorials/two-package-structure
  2. const fs = require('fs')
  3. const path = require('path')
  4. const axios = require('axios')
  5. const { exec } = require('child_process')
  6. const { build } = require('electron-builder')
  7. const convertPath = (dir) => path.resolve(__dirname, dir)
  8. const outputDir = convertPath('dist_electron') // 打包输出目录
  9. const fileContents = fs.readFileSync(convertPath('dist/config/appconfig.json'), 'utf-8')
  10. const appConfig = JSON.parse(fileContents)
  11. const isBeta = process.argv.includes('demo') // 是否测试版
  12. console.log(isBeta)
  13. // 打包之前清空目录
  14. exec('rd/s/q ' + outputDir)
  15. // 打包自动修改版本号
  16. const pkgContents = fs.readFileSync(convertPath('package.json'), 'utf-8')
  17. const pkg = JSON.parse(pkgContents)
  18. pkg.name = isBeta ? 'trading-beta' : 'trading'
  19. pkg.version = appConfig.version
  20. fs.writeFileSync(convertPath('package.json'), JSON.stringify(pkg, null, 2))
  21. axios.get(appConfig.apiUrl).then((res) => {
  22. const data = res.data.data
  23. console.log(data)
  24. const config = {
  25. productName: appConfig.appName + (isBeta ? '模拟盘' : ''),
  26. directories: {
  27. output: outputDir
  28. },
  29. files: [
  30. 'dist',
  31. 'main.js',
  32. ],
  33. nsis: {
  34. oneClick: false,
  35. allowToChangeInstallationDirectory: true
  36. },
  37. win: {
  38. icon: convertPath('dist/favicon.ico'),
  39. target: [
  40. 'nsis'
  41. ]
  42. },
  43. // 应用更新服务器地址,配置了 publish 才会生成 latest.yml 文件,和 .exe 文件一起放到该服务器地址下
  44. publish: [
  45. {
  46. provider: 'generic',
  47. url: data.pcUpdateUrl ?? ''
  48. }
  49. ]
  50. }
  51. build({
  52. config
  53. })
  54. }).catch((err) => {
  55. console.error(err)
  56. })