make.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. [
  69. path.join(root_dir, 'assets', 'app.png'),
  70. path.join(root_dir, 'build', 'assets', 'app.png'),
  71. ],
  72. ]
  73. to_cp.map(([src, target]) => {
  74. fse.copySync(src, target)
  75. })
  76. let pkg_base = require(path.join(root_dir, 'package.json'))
  77. let pkg_app = require(path.join(root_dir, 'app', 'package.json'))
  78. pkg_app.name = APP_NAME
  79. pkg_app.version = version.slice(0, 3).join('.')
  80. pkg_app.dependencies = pkg_base.dependencies
  81. fse.writeFileSync(
  82. path.join(root_dir, 'build', 'package.json'),
  83. JSON.stringify(pkg_app, null, 2),
  84. 'utf-8',
  85. )
  86. }
  87. const afterMake = async () => {
  88. console.log('-> afterMake...')
  89. }
  90. const doMake = async () => {
  91. console.log('-> make...')
  92. let targets = TARGET_PLATFORMS_configs.all
  93. if (process.env.MAKE_FOR === 'dev') {
  94. targets = TARGET_PLATFORMS_configs.macs
  95. } else if (process.env.MAKE_FOR === 'mac') {
  96. targets = TARGET_PLATFORMS_configs.mac
  97. } else if (process.env.MAKE_FOR === 'win') {
  98. targets = TARGET_PLATFORMS_configs.win
  99. } else if (process.env.MAKE_FOR === 'linux') {
  100. targets = TARGET_PLATFORMS_configs.linux
  101. }
  102. await builder.build({
  103. //targets: Platform.MAC.createTarget(),
  104. ...targets,
  105. config: {
  106. ...cfg_common,
  107. appId: 'SwitchHosts',
  108. productName: APP_NAME,
  109. mac: {
  110. type: 'distribution',
  111. category: 'public.app-category.productivity',
  112. icon: 'assets/app.icns',
  113. gatekeeperAssess: false,
  114. electronLanguages,
  115. identity: IDENTITY,
  116. hardenedRuntime: true,
  117. entitlements: 'scripts/entitlements.mac.plist',
  118. entitlementsInherit: 'scripts/entitlements.mac.plist',
  119. extendInfo: {
  120. ITSAppUsesNonExemptEncryption: false,
  121. CFBundleLocalizations: electronLanguages,
  122. CFBundleDevelopmentRegion: 'en',
  123. },
  124. },
  125. dmg: {
  126. //backgroundColor: '#f1f1f6',
  127. background: 'assets/dmg-bg.png',
  128. //icon: 'assets/dmg-icon.icns',
  129. iconSize: 160,
  130. window: {
  131. width: 600,
  132. height: 420,
  133. },
  134. contents: [
  135. {
  136. x: 150,
  137. y: 200,
  138. },
  139. {
  140. x: 450,
  141. y: 200,
  142. type: 'link',
  143. path: '/Applications',
  144. },
  145. ],
  146. sign: false,
  147. artifactName:
  148. '${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:
  159. '${productName}_installer_${arch}_${version}(${buildVersion}).${ext}',
  160. },
  161. portable: {
  162. artifactName:
  163. '${productName}_portable_${arch}_${version}(${buildVersion}).${ext}',
  164. },
  165. linux: {
  166. icon: 'assets/app.icns',
  167. artifactName:
  168. '${productName}_linux_${arch}_${version}(${buildVersion}).${ext}',
  169. category: 'Utility',
  170. synopsis: 'An App for hosts management and switching.',
  171. desktop: {
  172. Name: 'SwitchHosts',
  173. Type: 'Application',
  174. GenericName: 'An App for hosts management and switching.',
  175. },
  176. },
  177. },
  178. })
  179. console.log('done!')
  180. }
  181. ;(async () => {
  182. try {
  183. await beforeMake()
  184. await doMake()
  185. await afterMake()
  186. console.log('-> make Done!')
  187. } catch (e) {
  188. console.error(e)
  189. }
  190. })()