make.js 5.2 KB

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