|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * 整理 GitHub Release 待发布资源(CommonJS,便于用 NODE_PATH 引入隔离安装的 js-yaml) |
| 4 | + * |
| 5 | + * 背景:各平台/架构在独立 job 构建并各自上传 artifact。electron-builder 会为 |
| 6 | + * Windows / macOS 生成同名的 `latest.yml` / `latest-mac.yml`(分别只含本架构条目), |
| 7 | + * 直接上传会互相覆盖,导致另一架构无法自动更新。Linux 则使用按架构区分的 |
| 8 | + * `latest-linux.yml` / `latest-linux-arm64.yml`,天然不冲突。 |
| 9 | + * |
| 10 | + * 本脚本将所有 artifact 扁平化到输出目录: |
| 11 | + * - 合并 Windows / macOS 的 latest*.yml(合并 files 列表,path/sha512 优先指向 x64) |
| 12 | + * - 保留各架构 Linux 清单原样 |
| 13 | + * - 剔除调试文件 builder-debug.yml |
| 14 | + * - 同名二进制去重(保留首个),避免 release 资源重名冲突 |
| 15 | + * |
| 16 | + * 用法: node prepare-release-assets.cjs <artifactsDir> <outDir> |
| 17 | + */ |
| 18 | +"use strict"; |
| 19 | + |
| 20 | +const fs = require("fs"); |
| 21 | +const path = require("path"); |
| 22 | +const yaml = require("js-yaml"); |
| 23 | + |
| 24 | +const srcDir = process.argv[2]; |
| 25 | +const outDir = process.argv[3]; |
| 26 | + |
| 27 | +if (!srcDir || !outDir) { |
| 28 | + console.error("用法: node prepare-release-assets.cjs <artifactsDir> <outDir>"); |
| 29 | + process.exit(1); |
| 30 | +} |
| 31 | + |
| 32 | +/** 需要跨架构合并的更新清单 */ |
| 33 | +const MERGE_MANIFESTS = new Set(["latest.yml", "latest-mac.yml"]); |
| 34 | +/** 不需要上传的文件 */ |
| 35 | +const SKIP_FILES = new Set(["builder-debug.yml"]); |
| 36 | + |
| 37 | +/** 递归收集文件(跳过 *-unpacked 目录) */ |
| 38 | +function walk(dir, out = []) { |
| 39 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 40 | + const full = path.join(dir, entry.name); |
| 41 | + if (entry.isDirectory()) { |
| 42 | + if (entry.name.endsWith("-unpacked")) continue; |
| 43 | + walk(full, out); |
| 44 | + } else { |
| 45 | + out.push(full); |
| 46 | + } |
| 47 | + } |
| 48 | + return out; |
| 49 | +} |
| 50 | + |
| 51 | +fs.mkdirSync(outDir, { recursive: true }); |
| 52 | + |
| 53 | +/** name -> 解析后的清单文档数组 */ |
| 54 | +const manifestDocs = new Map(); |
| 55 | +/** 已写入输出目录的文件名 */ |
| 56 | +const seen = new Set(); |
| 57 | + |
| 58 | +for (const file of walk(srcDir)) { |
| 59 | + const base = path.basename(file); |
| 60 | + if (SKIP_FILES.has(base)) continue; |
| 61 | + |
| 62 | + if (MERGE_MANIFESTS.has(base)) { |
| 63 | + const doc = yaml.load(fs.readFileSync(file, "utf8")); |
| 64 | + if (!manifestDocs.has(base)) manifestDocs.set(base, []); |
| 65 | + manifestDocs.get(base).push(doc); |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + if (seen.has(base)) { |
| 70 | + console.warn(`⚠️ 跳过重复同名文件: ${base}`); |
| 71 | + continue; |
| 72 | + } |
| 73 | + seen.add(base); |
| 74 | + fs.copyFileSync(file, path.join(outDir, base)); |
| 75 | +} |
| 76 | + |
| 77 | +for (const [name, docs] of manifestDocs) { |
| 78 | + let merged; |
| 79 | + if (docs.length === 1) { |
| 80 | + merged = docs[0]; |
| 81 | + } else { |
| 82 | + merged = { ...docs[0] }; |
| 83 | + const byUrl = new Map(); |
| 84 | + for (const doc of docs) { |
| 85 | + for (const f of doc.files || []) { |
| 86 | + if (!byUrl.has(f.url)) byUrl.set(f.url, f); |
| 87 | + } |
| 88 | + } |
| 89 | + merged.files = [...byUrl.values()]; |
| 90 | + // 基准 path/sha512 优先指向 x64(多数用户);electron-updater v6 仍会按架构从 files 中匹配 |
| 91 | + const x64 = merged.files.find((f) => /x64|x86_64/i.test(f.url)); |
| 92 | + if (x64) { |
| 93 | + merged.path = x64.url; |
| 94 | + merged.sha512 = x64.sha512; |
| 95 | + } |
| 96 | + } |
| 97 | + fs.writeFileSync(path.join(outDir, name), yaml.dump(merged, { lineWidth: -1 })); |
| 98 | + console.log(`✅ 合并清单 ${name}(files: ${(merged.files || []).length})`); |
| 99 | +} |
| 100 | + |
| 101 | +console.log(`release-assets 准备完成,共 ${fs.readdirSync(outDir).length} 个文件`); |
0 commit comments