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