make.js 5.0 KB

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