make.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. win: {
  24. win: ['nsis:ia32', 'nsis:x64', 'portable:ia32'],
  25. },
  26. all: {
  27. mac: ['dmg:x64', 'dmg:arm64', 'dmg:universal'],
  28. linux: ['AppImage:x64', 'deb:x64'],
  29. win: ['nsis:ia32', 'nsis:x64', 'portable:ia32'],
  30. },
  31. }
  32. const APP_NAME = 'SwitchHosts'
  33. const { IDENTITY } = process.env
  34. const cfg_common = {
  35. copyright: `Copyright © ${new Date().getFullYear()}`,
  36. buildVersion: version[3].toString(),
  37. directories: {
  38. buildResources: 'build',
  39. app: 'build',
  40. },
  41. electronDownload: {
  42. cache: path.join(homedir, '.electron'),
  43. mirror: 'https://npm.taobao.org/mirrors/electron/',
  44. },
  45. }
  46. const beforeMake = async () => {
  47. console.log('-> beforeMake...')
  48. fse.removeSync(dist_dir)
  49. fse.ensureDirSync(dist_dir)
  50. const to_cp = [
  51. [
  52. path.join(root_dir, 'assets', 'app.png'),
  53. path.join(root_dir, 'build', 'assets', 'app.png'),
  54. ],
  55. ]
  56. to_cp.map(([src, target]) => {
  57. fse.copySync(src, target)
  58. })
  59. let pkg_base = require(path.join(root_dir, 'package.json'))
  60. let pkg_app = require(path.join(root_dir, 'app', 'package.json'))
  61. pkg_app.name = APP_NAME
  62. pkg_app.version = version.slice(0, 3).join('.')
  63. pkg_app.dependencies = pkg_base.dependencies
  64. fse.writeFileSync(
  65. path.join(root_dir, 'build', 'package.json'),
  66. JSON.stringify(pkg_app, null, 2),
  67. 'utf-8',
  68. )
  69. }
  70. const afterMake = async () => {
  71. console.log('-> afterMake...')
  72. }
  73. const doMake = async () => {
  74. console.log('-> make...')
  75. let targets = TARGET_PLATFORMS_configs.all
  76. if (process.env.MAKE_FOR === 'dev') {
  77. targets = TARGET_PLATFORMS_configs.macs
  78. } else if (process.env.MAKE_FOR === 'mac') {
  79. targets = TARGET_PLATFORMS_configs.mac
  80. } else if (process.env.MAKE_FOR === 'win') {
  81. targets = TARGET_PLATFORMS_configs.win
  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. provisioningProfile: 'scripts/app.provisionprofile',
  101. extendInfo: {
  102. ITSAppUsesNonExemptEncryption: false,
  103. CFBundleLocalizations: electronLanguages,
  104. CFBundleDevelopmentRegion: 'en',
  105. },
  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:
  130. '${productName}_mac_${arch}_${version}(${buildVersion}).${ext}',
  131. },
  132. win: {
  133. icon: 'assets/app.ico',
  134. //requestedExecutionLevel: 'requireAdministrator'
  135. },
  136. nsis: {
  137. installerIcon: 'assets/installer-icon.ico',
  138. oneClick: false,
  139. allowToChangeInstallationDirectory: true,
  140. artifactName:
  141. '${productName}_installer_${arch}_${version}(${buildVersion}).${ext}',
  142. },
  143. portable: {
  144. artifactName:
  145. '${productName}_portable_${arch}_${version}(${buildVersion}).${ext}',
  146. },
  147. linux: {
  148. icon: 'assets/app.png',
  149. artifactName:
  150. '${productName}_linux_${arch}_${version}(${buildVersion}).${ext}',
  151. category: 'Utility',
  152. synopsis: 'An App for hosts management and switching.',
  153. desktop: {
  154. Name: 'SwitchHosts',
  155. Type: 'Application',
  156. GenericName: 'An App for hosts management and switching.',
  157. },
  158. },
  159. },
  160. })
  161. console.log('done!')
  162. }
  163. ;(async () => {
  164. try {
  165. await beforeMake()
  166. await doMake()
  167. await afterMake()
  168. console.log('-> make Done!')
  169. } catch (e) {
  170. console.error(e)
  171. }
  172. })()