make.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * make.js
  3. * @author: oldj
  4. * @homepage: https://oldj.net
  5. */
  6. require('dotenv').config()
  7. const path = require('path')
  8. const fse = require('fs-extra')
  9. const version = require('../src/version.json')
  10. const builder = require('electron-builder')
  11. const homedir = require('os').homedir()
  12. const { APP_NAME, root_dir, dist_dir, electronLanguages } = require('./vars')
  13. const TARGET_PLATFORMS_configs = {
  14. mac: {
  15. mac: ['dmg:x64', 'dmg:arm64'],
  16. },
  17. win: {
  18. win: ['nsis:ia32', 'nsis:x64', 'nsis:arm64', 'portable:x64'],
  19. },
  20. linux: {
  21. linux: ['AppImage:x64', 'AppImage:arm64', 'deb:x64', 'deb:arm64'],
  22. },
  23. all: {
  24. mac: ['dmg:x64', 'dmg:arm64', 'zip:universal'],
  25. win: ['nsis:ia32', 'nsis:x64', 'nsis:arm64', 'portable:x64', 'zip:x64' /* , 'appx:x64'*/],
  26. linux: ['AppImage:x64', 'AppImage:arm64', 'deb:x64', 'deb:arm64'],
  27. },
  28. }
  29. const { APP_BUNDLE_ID, IDENTITY } = process.env
  30. console.log(`APP_BUNDLE_ID: ${APP_BUNDLE_ID}`)
  31. const cfg_common = {
  32. copyright: `Copyright © ${new Date().getFullYear()}`,
  33. buildVersion: version[3].toString(),
  34. directories: {
  35. buildResources: 'build',
  36. app: 'build',
  37. },
  38. electronDownload: {
  39. cache: path.join(homedir, '.electron'),
  40. mirror: 'https://registry.npmmirror.com/-/binary/electron/',
  41. },
  42. asar: true,
  43. }
  44. const beforeMake = async () => {
  45. console.log('-> beforeMake...')
  46. fse.removeSync(dist_dir)
  47. fse.ensureDirSync(dist_dir)
  48. const to_cp = [
  49. [path.join(root_dir, 'assets', 'app.png'), path.join(root_dir, 'build', 'assets', 'app.png')],
  50. ]
  51. to_cp.map(([src, target]) => {
  52. fse.copySync(src, target)
  53. })
  54. let pkg_base = require(path.join(root_dir, 'package.json'))
  55. let pkg_app = require(path.join(root_dir, 'app', 'package.json'))
  56. pkg_app.name = APP_NAME
  57. pkg_app.version = version.slice(0, 3).join('.')
  58. pkg_app.dependencies = pkg_base.dependencies
  59. fse.writeFileSync(
  60. path.join(root_dir, 'build', 'package.json'),
  61. JSON.stringify(pkg_app, null, 2),
  62. 'utf-8',
  63. )
  64. }
  65. const afterMake = async () => {
  66. console.log('-> afterMake...')
  67. }
  68. const doMake = async () => {
  69. console.log('-> make...')
  70. const { MAKE_FOR } = process.env
  71. let targets = TARGET_PLATFORMS_configs.all
  72. cfg_common.compression = 'maximum'
  73. if (MAKE_FOR === 'dev') {
  74. targets = TARGET_PLATFORMS_configs.mac
  75. cfg_common.compression = 'store'
  76. } else if (MAKE_FOR === 'mac') {
  77. targets = TARGET_PLATFORMS_configs.mac
  78. } else if (MAKE_FOR === 'win') {
  79. targets = TARGET_PLATFORMS_configs.win
  80. } else if (MAKE_FOR === 'linux') {
  81. targets = TARGET_PLATFORMS_configs.linux
  82. }
  83. await builder.build({
  84. //targets: Platform.MAC.createTarget(),
  85. ...targets,
  86. config: {
  87. ...cfg_common,
  88. appId: 'SwitchHosts',
  89. productName: APP_NAME,
  90. mac: {
  91. type: 'distribution',
  92. category: 'public.app-category.productivity',
  93. icon: 'assets/app.icns',
  94. gatekeeperAssess: false,
  95. electronLanguages,
  96. identity: IDENTITY,
  97. hardenedRuntime: true,
  98. entitlements: 'scripts/entitlements.mac.plist',
  99. entitlementsInherit: 'scripts/entitlements.mac.plist',
  100. extendInfo: {
  101. ITSAppUsesNonExemptEncryption: false,
  102. CFBundleLocalizations: electronLanguages,
  103. CFBundleDevelopmentRegion: 'en',
  104. },
  105. notarize: false,
  106. },
  107. dmg: {
  108. //backgroundColor: '#f1f1f6',
  109. background: 'assets/dmg-bg.png',
  110. //icon: 'assets/dmg-icon.icns',
  111. iconSize: 160,
  112. window: {
  113. width: 600,
  114. height: 420,
  115. },
  116. contents: [
  117. {
  118. x: 150,
  119. y: 200,
  120. },
  121. {
  122. x: 450,
  123. y: 200,
  124. type: 'link',
  125. path: '/Applications',
  126. },
  127. ],
  128. sign: false,
  129. artifactName: '${productName}_mac_${arch}_${version}(${buildVersion}).${ext}',
  130. },
  131. win: {
  132. icon: 'assets/icon.ico',
  133. //requestedExecutionLevel: 'requireAdministrator'
  134. },
  135. nsis: {
  136. installerIcon: 'assets/installer-icon.ico',
  137. oneClick: false,
  138. allowToChangeInstallationDirectory: true,
  139. deleteAppDataOnUninstall: false,
  140. shortcutName: 'SwitchHosts',
  141. artifactName: '${productName}_windows_installer_${arch}_${version}(${buildVersion}).${ext}',
  142. },
  143. portable: {
  144. artifactName: '${productName}_windows_portable_${arch}_${version}(${buildVersion}).${ext}',
  145. },
  146. linux: {
  147. icon: 'assets/app.icns',
  148. artifactName: '${productName}_linux_${arch}_${version}(${buildVersion}).${ext}',
  149. category: 'Utility',
  150. synopsis: 'An App for hosts management and switching.',
  151. desktop: {
  152. Name: 'SwitchHosts',
  153. Type: 'Application',
  154. GenericName: 'An App for hosts management and switching.',
  155. },
  156. },
  157. publish: {
  158. provider: 'github',
  159. owner: 'oldj',
  160. repo: 'SwitchHosts',
  161. },
  162. },
  163. })
  164. console.log('done!')
  165. }
  166. ;(async () => {
  167. try {
  168. await beforeMake()
  169. await doMake()
  170. await afterMake()
  171. //await macSign()
  172. console.log('-> make Done!')
  173. } catch (e) {
  174. console.log(e)
  175. }
  176. })()