notarize-dmg.cjs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const { execSync } = require('child_process')
  2. const path = require('path')
  3. module.exports = async function (context) {
  4. // 1. 获取本次构建生成的所有产物路径
  5. // artifactPaths 数组只包含本次 build 出来的文件,例如:
  6. // ['/.../dist/scas-tool-1.0.0-x64.dmg', '/.../dist/scas-tool-1.0.0-arm64.dmg', '/.../dist/scas-tool-1.0.0-mac.zip']
  7. const { artifactPaths } = context
  8. // 2. 环境检查:只在 macOS 上运行
  9. if (process.platform !== 'darwin') {
  10. console.log('非 macOS 环境,跳过公证步骤。')
  11. return
  12. }
  13. // 3. 筛选本次生成的 .dmg 和 .zip 文件(苹果建议这两种格式都要公证)
  14. const targets = artifactPaths.filter((file) => file.endsWith('.dmg') || file.endsWith('.zip'))
  15. if (targets.length === 0) {
  16. console.log('本次构建未发现需要公证的 DMG 或 ZIP 文件。')
  17. return
  18. }
  19. console.log(`发现 ${targets.length} 个新产物待公证...`)
  20. const profile = 'ZYCOO' // 对应你本地的 keychain-profile 名称
  21. for (const artifactPath of targets) {
  22. const fileName = path.basename(artifactPath)
  23. console.log(`\n🔔 正在公证最新产物: ${fileName}`)
  24. try {
  25. // 执行公证
  26. console.log(`[1/2] 正在上传至苹果公证服务 (Profile: ${profile})...`)
  27. const submitCmd = `xcrun notarytool submit "${artifactPath}" --wait --keychain-profile "${profile}"`
  28. execSync(submitCmd, { stdio: 'inherit' })
  29. // 执行装订 (Staple) - 注意:zip文件不支持 staple,只有 dmg 和 app 支持
  30. if (artifactPath.endsWith('.dmg')) {
  31. console.log(`[2/2] 正在为 DMG 注入票据 (Staple)...`)
  32. try {
  33. // 使用 execSync 的更安全写法,或者 wrap 在 try-catch 里
  34. const stapleCmd = `xcrun stapler staple "${artifactPath}"`
  35. execSync(stapleCmd, { stdio: 'inherit' })
  36. console.log(`✅ Staple 成功!`)
  37. } catch (error) {
  38. console.error(error.message)
  39. console.warn(`⚠️ Staple 失败,但这通常是因为网络原因。`)
  40. console.warn(`只要苹果公证上传成功,App 依然是合法的。你可以手动运行命令重试。`)
  41. // 这里不 throw error,让流程继续完成
  42. }
  43. } else {
  44. console.log(`[2/2] ZIP 文件无需执行 Staple,公证已完成。`)
  45. }
  46. console.log(`✅ ${fileName} 处理完成!`)
  47. } catch (error) {
  48. console.error(`❌ ${fileName} 公证过程中出错:`)
  49. console.error(error.message)
  50. // 如果公证失败,建议抛出错误终止构建,防止发布损坏的包
  51. throw error
  52. }
  53. }
  54. }