make.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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', 'dmg:universal'],
  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. let targets = TARGET_PLATFORMS_configs.all
  89. if (process.env.MAKE_FOR === 'dev') {
  90. targets = TARGET_PLATFORMS_configs.macs
  91. } else if (process.env.MAKE_FOR === 'mac') {
  92. targets = TARGET_PLATFORMS_configs.mac
  93. } else if (process.env.MAKE_FOR === 'win') {
  94. targets = TARGET_PLATFORMS_configs.win
  95. } else if (process.env.MAKE_FOR === 'linux') {
  96. targets = TARGET_PLATFORMS_configs.linux
  97. }
  98. await builder.build({
  99. //targets: Platform.MAC.createTarget(),
  100. ...targets,
  101. config: {
  102. ...cfg_common,
  103. appId: 'SwitchHosts',
  104. productName: APP_NAME,
  105. mac: {
  106. type: 'distribution',
  107. category: 'public.app-category.productivity',
  108. icon: 'assets/app.icns',
  109. gatekeeperAssess: false,
  110. electronLanguages,
  111. identity: IDENTITY,
  112. hardenedRuntime: true,
  113. entitlements: 'scripts/entitlements.mac.plist',
  114. entitlementsInherit: 'scripts/entitlements.mac.plist',
  115. extendInfo: {
  116. ITSAppUsesNonExemptEncryption: false,
  117. CFBundleLocalizations: electronLanguages,
  118. CFBundleDevelopmentRegion: 'en',
  119. },
  120. },
  121. dmg: {
  122. //backgroundColor: '#f1f1f6',
  123. background: 'assets/dmg-bg.png',
  124. //icon: 'assets/dmg-icon.icns',
  125. iconSize: 160,
  126. window: {
  127. width: 600,
  128. height: 420,
  129. },
  130. contents: [
  131. {
  132. x: 150,
  133. y: 200,
  134. },
  135. {
  136. x: 450,
  137. y: 200,
  138. type: 'link',
  139. path: '/Applications',
  140. },
  141. ],
  142. sign: false,
  143. artifactName: '${productName}_mac_${arch}_${version}(${buildVersion}).${ext}',
  144. },
  145. win: {
  146. icon: 'assets/icon.ico',
  147. //requestedExecutionLevel: 'requireAdministrator'
  148. },
  149. nsis: {
  150. installerIcon: 'assets/installer-icon.ico',
  151. oneClick: false,
  152. allowToChangeInstallationDirectory: true,
  153. artifactName: '${productName}_windows_installer_${arch}_${version}(${buildVersion}).${ext}',
  154. },
  155. portable: {
  156. artifactName: '${productName}_windows_portable_${arch}_${version}(${buildVersion}).${ext}',
  157. },
  158. linux: {
  159. icon: 'assets/app.icns',
  160. artifactName: '${productName}_linux_${arch}_${version}(${buildVersion}).${ext}',
  161. category: 'Utility',
  162. synopsis: 'An App for hosts management and switching.',
  163. desktop: {
  164. Name: 'SwitchHosts',
  165. Type: 'Application',
  166. GenericName: 'An App for hosts management and switching.',
  167. },
  168. },
  169. },
  170. })
  171. console.log('done!')
  172. }
  173. ;(async () => {
  174. try {
  175. await beforeMake()
  176. await doMake()
  177. await afterMake()
  178. console.log('-> make Done!')
  179. } catch (e) {
  180. console.error(e)
  181. }
  182. })()