|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Merge the per-architecture `latest-mac.yml` files produced by parallel macOS |
| 4 | + * release builds into the single manifest electron-updater expects. |
| 5 | + * |
| 6 | + * Why this exists |
| 7 | + * --------------- |
| 8 | + * macOS notarization is ~3.5 minutes per architecture and electron-builder runs |
| 9 | + * them serially, so a combined x64+arm64 build spends ~7 minutes waiting on |
| 10 | + * Apple. Splitting the two architectures into concurrent CI jobs halves that — |
| 11 | + * but each job then emits its OWN `latest-mac.yml` describing only the artifacts |
| 12 | + * it built. Uploading both to one release is last-writer-wins, which would leave |
| 13 | + * the auto-update feed advertising a single architecture and silently strand |
| 14 | + * every user on the other one. |
| 15 | + * |
| 16 | + * So the arch jobs publish nothing, and this merges their manifests. |
| 17 | + * |
| 18 | + * How electron-updater reads the result |
| 19 | + * ------------------------------------- |
| 20 | + * MacUpdater selects its download by filtering `files[]` on whether the URL |
| 21 | + * contains "arm64" — keeping those entries on Apple Silicon and rejecting them |
| 22 | + * on Intel. That works only because `artifactName` in electron-builder.json puts |
| 23 | + * the arch in the filename (`Redstring-mac-arm64.zip`). The merge below is |
| 24 | + * therefore just a union of the `files` arrays; no rewriting is needed. |
| 25 | + * |
| 26 | + * The top-level `path`/`sha512`/`size` fields are the pre-`files[]` format that |
| 27 | + * old updaters still read, and they can only name one artifact. They're pointed |
| 28 | + * at the x64 build deliberately: a client old enough to depend on them predates |
| 29 | + * Apple Silicon support, so Intel is the safer thing for it to be handed. |
| 30 | + * |
| 31 | + * Usage: node scripts/merge-mac-update-manifest.js <input-dir> <output-dir> |
| 32 | + * input-dir - searched recursively for latest-mac.yml (one per arch job) |
| 33 | + * output-dir - receives the single merged latest-mac.yml |
| 34 | + */ |
| 35 | + |
| 36 | +import fs from 'fs'; |
| 37 | +import path from 'path'; |
| 38 | +import yaml from 'js-yaml'; |
| 39 | + |
| 40 | +const [, , inputDir, outputDir] = process.argv; |
| 41 | + |
| 42 | +if (!inputDir || !outputDir) { |
| 43 | + console.error('Usage: merge-mac-update-manifest.js <input-dir> <output-dir>'); |
| 44 | + process.exit(1); |
| 45 | +} |
| 46 | + |
| 47 | +/** Every latest-mac.yml under `dir`, at any depth. */ |
| 48 | +const findManifests = (dir) => { |
| 49 | + const found = []; |
| 50 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 51 | + const full = path.join(dir, entry.name); |
| 52 | + if (entry.isDirectory()) found.push(...findManifests(full)); |
| 53 | + else if (entry.name === 'latest-mac.yml') found.push(full); |
| 54 | + } |
| 55 | + return found; |
| 56 | +}; |
| 57 | + |
| 58 | +const manifestPaths = findManifests(inputDir); |
| 59 | + |
| 60 | +if (manifestPaths.length === 0) { |
| 61 | + console.log('[merge-mac] No latest-mac.yml found — nothing to merge.'); |
| 62 | + process.exit(0); |
| 63 | +} |
| 64 | + |
| 65 | +const manifests = manifestPaths.map((file) => ({ |
| 66 | + file, |
| 67 | + doc: yaml.load(fs.readFileSync(file, 'utf8')) |
| 68 | +})); |
| 69 | + |
| 70 | +for (const { file, doc } of manifests) { |
| 71 | + const urls = (doc?.files ?? []).map((f) => f.url).join(', '); |
| 72 | + console.log(`[merge-mac] ${path.relative(inputDir, file)} → [${urls}]`); |
| 73 | +} |
| 74 | + |
| 75 | +// A version disagreement means artifacts from two different builds landed in the |
| 76 | +// same release. Merging them would produce a manifest that points at a mixture, |
| 77 | +// so fail rather than publish something incoherent. |
| 78 | +const versions = [...new Set(manifests.map((m) => m.doc?.version).filter(Boolean))]; |
| 79 | +if (versions.length > 1) { |
| 80 | + console.error(`[merge-mac] Refusing to merge: conflicting versions ${versions.join(' vs ')}`); |
| 81 | + process.exit(1); |
| 82 | +} |
| 83 | + |
| 84 | +// Union the file entries, keyed by url. Duplicates would make an updater |
| 85 | +// download the same artifact twice. |
| 86 | +const filesByUrl = new Map(); |
| 87 | +for (const { doc } of manifests) { |
| 88 | + for (const entry of doc?.files ?? []) { |
| 89 | + if (entry?.url) filesByUrl.set(entry.url, entry); |
| 90 | + } |
| 91 | +} |
| 92 | +const files = [...filesByUrl.values()]; |
| 93 | + |
| 94 | +if (files.length === 0) { |
| 95 | + console.error('[merge-mac] Refusing to merge: no file entries in any manifest.'); |
| 96 | + process.exit(1); |
| 97 | +} |
| 98 | + |
| 99 | +// Legacy single-artifact fields — Intel by preference, see the header note. |
| 100 | +// Fall back to the first entry if this build produced no x64 slice at all. |
| 101 | +const legacy = files.find((f) => !f.url.includes('arm64')) ?? files[0]; |
| 102 | + |
| 103 | +// Newest wins: the merged manifest describes whichever build finished last. |
| 104 | +const releaseDate = manifests |
| 105 | + .map((m) => m.doc?.releaseDate) |
| 106 | + .filter(Boolean) |
| 107 | + .sort() |
| 108 | + .pop(); |
| 109 | + |
| 110 | +const merged = { |
| 111 | + version: versions[0], |
| 112 | + files, |
| 113 | + path: legacy.url, |
| 114 | + sha512: legacy.sha512, |
| 115 | + releaseDate |
| 116 | +}; |
| 117 | +if (legacy.size != null) merged.size = legacy.size; |
| 118 | + |
| 119 | +fs.mkdirSync(outputDir, { recursive: true }); |
| 120 | +const outFile = path.join(outputDir, 'latest-mac.yml'); |
| 121 | +fs.writeFileSync(outFile, yaml.dump(merged, { lineWidth: -1 })); |
| 122 | + |
| 123 | +console.log(`[merge-mac] Merged ${manifests.length} manifest(s) → ${files.length} artifact(s):`); |
| 124 | +for (const f of files) console.log(`[merge-mac] ${f.url}`); |
| 125 | +console.log(`[merge-mac] Legacy path field → ${merged.path}`); |
| 126 | +console.log(`[merge-mac] Wrote ${outFile}`); |
0 commit comments