notarize.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * notarize.js
  3. *
  4. * @see https://oldj.net/blog/2019/12/29/electron-builder-sign-and-notarize-for-macos
  5. */
  6. require('dotenv').config()
  7. const { notarize } = require('@electron/notarize')
  8. const { exec } = require('child_process')
  9. function getPasswordFromKeychain(account, service) {
  10. return new Promise((resolve, reject) => {
  11. const command = `security find-generic-password -a '${account}' -s '${service}' -w`
  12. exec(command, (error, stdout, stderr) => {
  13. if (error) {
  14. reject(new Error(`Error fetching password: ${stderr}`))
  15. } else {
  16. resolve(stdout.trim())
  17. }
  18. })
  19. })
  20. }
  21. exports.default = async function notarizing(context) {
  22. const appName = context.packager.appInfo.productFilename
  23. const { electronPlatformName, appOutDir } = context
  24. console.log(`in notarize, ${electronPlatformName}...`)
  25. if (electronPlatformName !== 'darwin') {
  26. return
  27. }
  28. if (process.env.MAKE_FOR === 'dev' || process.env.SKIP_NOTARIZATION) {
  29. console.log('skip notarization.')
  30. return // for dev, skip notarization
  31. }
  32. let appPath = `${appOutDir}/${appName}.app`
  33. let {
  34. APP_BUNDLE_ID: appBundleId,
  35. TEAM_ID: teamId,
  36. APPLE_ID: appleId,
  37. APPLE_APP_SPECIFIC_PASSWORD: appleIdPassword,
  38. } = process.env
  39. if (!appleIdPassword) {
  40. //appleIdPassword = `@keychain:"Apple Notarize: ${appleId}"`
  41. appleIdPassword = await getPasswordFromKeychain(appleId, `Apple Notarize: ${appleId}`)
  42. process.env.APPLE_APP_SPECIFIC_PASSWORD = appleIdPassword
  43. }
  44. if (!appleId || !appBundleId || !teamId || !appleIdPassword) {
  45. console.log('Not notarized.')
  46. return
  47. }
  48. console.log('Start notarizing...')
  49. await notarize({
  50. appPath,
  51. tool: 'notarytool',
  52. //appBundleId,
  53. //ascProvider,
  54. appleId,
  55. appleIdPassword,
  56. teamId,
  57. })
  58. console.log('Notarize done.')
  59. }