make.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * build
  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 execa = require('execa')
  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', 'zh_CN']
  16. const TARGET_PLATFORMS_configs = {
  17. mac: {
  18. mac: ['default']
  19. },
  20. mas: {
  21. mac: ['mas']
  22. },
  23. macs: {
  24. mac: ['default', 'mas']
  25. },
  26. win: {
  27. win: ['nsis:ia32', 'portable:ia32']
  28. },
  29. all: {
  30. mac: ['default'],
  31. linux: [/*'zip:x64', */'AppImage:x64', 'deb:x64'],
  32. win: ['nsis:ia32', 'nsis:x64', 'portable:ia32']
  33. }
  34. }
  35. const APP_NAME = 'SwitchHosts'
  36. const { IDENTITY } = process.env
  37. const cfg_common = {
  38. copyright: `Copyright © ${(new Date()).getFullYear()}`,
  39. buildVersion: version[3].toString(),
  40. directories: {
  41. buildResources: 'build',
  42. app: 'build'
  43. },
  44. electronDownload: {
  45. cache: path.join(homedir, '.electron'),
  46. mirror: 'https://npm.taobao.org/mirrors/electron/'
  47. }
  48. }
  49. const sign = async () => {
  50. console.log('-> to sign...')
  51. let wd = process.cwd()
  52. process.chdir(__dirname)
  53. let cmd = path.join(__dirname, 'sign-mac.sh')
  54. try {
  55. const { stdout } = await execa(cmd)
  56. console.log(stdout)
  57. } catch (e) {
  58. //console.error(e)
  59. console.log(e.stdout)
  60. console.error(e.stderr)
  61. }
  62. process.chdir(wd)
  63. }
  64. const beforeMake = async () => {
  65. console.log('-> beforeMake...')
  66. fse.removeSync(dist_dir)
  67. fse.ensureDirSync(dist_dir)
  68. const to_cp = [
  69. [
  70. path.join(root_dir, 'assets', 'app.png'),
  71. path.join(root_dir, 'build', 'assets', 'app.png')
  72. ]
  73. ]
  74. to_cp.map(([src, target]) => {
  75. fse.copySync(src, target)
  76. })
  77. let pkg_base = require(path.join(root_dir, 'package.json'))
  78. let pkg_app = require(path.join(root_dir, 'app', 'package.json'))
  79. pkg_app.name = APP_NAME
  80. pkg_app.version = version.slice(0, 3).join('.')
  81. pkg_app.dependencies = pkg_base.dependencies
  82. fse.writeFileSync(
  83. path.join(root_dir, 'build', 'package.json'),
  84. JSON.stringify(pkg_app, null, 2),
  85. 'utf-8'
  86. )
  87. }
  88. const afterMake = async () => {
  89. console.log('-> afterMake...')
  90. }
  91. const makeMacArm = async () => {
  92. console.log('-> makeMacArm...')
  93. await builder.build({
  94. config: {
  95. ...cfg_common,
  96. appId: 'SwitchHosts',
  97. productName: APP_NAME,
  98. mac: {
  99. target: [
  100. {
  101. target: 'dmg',
  102. arch: [
  103. //'x64',
  104. 'arm64'
  105. ]
  106. }
  107. ],
  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. provisioningProfile: 'scripts/app.provisionprofile',
  117. artifactName: '${productName}_arm64_${version}(${buildVersion}).${ext}'
  118. },
  119. dmg: {
  120. //backgroundColor: '#f1f1f6',
  121. background: 'assets/dmg-bg.png',
  122. //icon: 'assets/dmg-icon.icns',
  123. iconSize: 160,
  124. window: {
  125. width: 600,
  126. height: 420
  127. },
  128. contents: [{
  129. x: 150,
  130. y: 200
  131. }, {
  132. x: 450,
  133. y: 200,
  134. type: 'link',
  135. path: '/Applications'
  136. }],
  137. sign: false,
  138. artifactName: '${productName}_arm64_${version}(${buildVersion}).${ext}'
  139. }
  140. }
  141. })
  142. console.log('done!')
  143. }
  144. const makeDefault = async () => {
  145. console.log('-> makeDefault...')
  146. // forFullVersion.task(APP_NAME)
  147. await builder.build({
  148. //targets: Platform.MAC.createTarget(),
  149. //...TARGET_PLATFORMS_configs.mac,
  150. //...TARGET_PLATFORMS_configs.win,
  151. ...TARGET_PLATFORMS_configs.all,
  152. config: {
  153. ...cfg_common,
  154. appId: 'SwitchHosts',
  155. productName: APP_NAME,
  156. mac: {
  157. category: 'public.app-category.productivity',
  158. icon: 'assets/app.icns',
  159. gatekeeperAssess: false,
  160. electronLanguages,
  161. identity: IDENTITY,
  162. hardenedRuntime: true,
  163. entitlements: 'scripts/entitlements.mac.plist',
  164. entitlementsInherit: 'scripts/entitlements.mac.plist',
  165. provisioningProfile: 'scripts/app.provisionprofile',
  166. artifactName: '${productName}_${version}(${buildVersion}).${ext}'
  167. },
  168. dmg: {
  169. //backgroundColor: '#f1f1f6',
  170. background: 'assets/dmg-bg.png',
  171. //icon: 'assets/dmg-icon.icns',
  172. iconSize: 160,
  173. window: {
  174. width: 600,
  175. height: 420
  176. },
  177. contents: [{
  178. x: 150,
  179. y: 200
  180. }, {
  181. x: 450,
  182. y: 200,
  183. type: 'link',
  184. path: '/Applications'
  185. }],
  186. sign: false,
  187. artifactName: '${productName}_${version}(${buildVersion}).${ext}'
  188. },
  189. win: {
  190. icon: 'assets/app.ico',
  191. requestedExecutionLevel: 'requireAdministrator'
  192. },
  193. nsis: {
  194. installerIcon: 'assets/installer-icon.ico',
  195. oneClick: false,
  196. allowToChangeInstallationDirectory: true,
  197. artifactName: '${productName}_installer_${version}(${buildVersion}).${ext}'
  198. },
  199. portable: {
  200. artifactName: '${productName}_portable_${version}(${buildVersion}).${ext}'
  201. },
  202. linux: {
  203. icon: 'assets/app.png',
  204. artifactName: '${productName}_linux_${version}(${buildVersion}).${ext}',
  205. category: 'Office'
  206. }
  207. }
  208. })
  209. console.log('done!')
  210. }
  211. (async () => {
  212. try {
  213. await beforeMake()
  214. await makeMacArm()
  215. await makeDefault()
  216. await afterMake()
  217. await sign()
  218. console.log('-> meke Done!')
  219. } catch (e) {
  220. console.log(e)
  221. }
  222. })()