make.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 makeDefault = async () => {
  92. console.log('-> makeDefault...')
  93. // forFullVersion.task(APP_NAME)
  94. await builder.build({
  95. //targets: Platform.MAC.createTarget(),
  96. //...TARGET_PLATFORMS_configs.mac,
  97. ...TARGET_PLATFORMS_configs.all,
  98. config: {
  99. ...cfg_common,
  100. appId: 'SwitchHosts',
  101. productName: APP_NAME,
  102. mac: {
  103. category: 'public.app-category.productivity',
  104. icon: 'assets/app.icns',
  105. gatekeeperAssess: false,
  106. electronLanguages,
  107. identity: IDENTITY,
  108. hardenedRuntime: true,
  109. entitlements: 'scripts/entitlements.mac.plist',
  110. entitlementsInherit: 'scripts/entitlements.mac.plist',
  111. provisioningProfile: 'scripts/app.provisionprofile',
  112. artifactName: '${productName}_${version}(${buildVersion}).${ext}'
  113. },
  114. dmg: {
  115. //backgroundColor: '#f1f1f6',
  116. background: 'assets/dmg-bg.png',
  117. //icon: 'assets/dmg-icon.icns',
  118. iconSize: 160,
  119. window: {
  120. width: 600,
  121. height: 420
  122. },
  123. contents: [{
  124. x: 150,
  125. y: 200
  126. }, {
  127. x: 450,
  128. y: 200,
  129. type: 'link',
  130. path: '/Applications'
  131. }],
  132. sign: false,
  133. artifactName: '${productName}_${version}(${buildVersion}).${ext}'
  134. },
  135. mas: {
  136. category: 'public.app-category.productivity',
  137. icon: 'assets/app.icns',
  138. //gatekeeperAssess: false,
  139. electronLanguages,
  140. identity: IDENTITY,
  141. entitlements: 'scripts/parent.plist',
  142. entitlementsInherit: 'scripts/child.plist',
  143. //artifactName: '${productName}_${version}(${buildVersion}).${ext}',
  144. binaries: []
  145. },
  146. win: {
  147. icon: 'assets/app.ico'
  148. },
  149. nsis: {
  150. installerIcon: 'assets/installer-icon.ico',
  151. oneClick: false,
  152. allowToChangeInstallationDirectory: true,
  153. artifactName: '${productName}_Installer_${version}(${buildVersion}).${ext}'
  154. },
  155. portable: {
  156. artifactName: '${productName}_Portable_${version}(${buildVersion}).${ext}'
  157. },
  158. linux: {
  159. icon: 'assets/app.png',
  160. artifactName: '${productName}_linux_${version}(${buildVersion}).${ext}',
  161. category: 'Office'
  162. }
  163. }
  164. })
  165. console.log('done!')
  166. }
  167. (async () => {
  168. try {
  169. await beforeMake()
  170. //await makeMASLite()
  171. await makeDefault()
  172. await afterMake()
  173. await sign()
  174. console.log('-> meke Done!')
  175. } catch (e) {
  176. console.log(e)
  177. }
  178. })()