|
| 1 | +#!/usr/bin/env node |
| 2 | +import { copyFileSync, existsSync, mkdirSync, readdirSync, rmSync } from 'fs'; |
| 3 | +import { basename, join } from 'path'; |
| 4 | + |
| 5 | +const args = parseArgs(process.argv.slice(2)); |
| 6 | +const assetsDir = requireArg(args, 'assets-dir'); |
| 7 | +const version = requireArg(args, 'version'); |
| 8 | +const outDir = requireArg(args, 'out-dir'); |
| 9 | + |
| 10 | +const requiredPlatforms = parseListArg( |
| 11 | + args['required-platforms'] || |
| 12 | + 'windows-x86_64,darwin-x86_64,darwin-aarch64,linux-x86_64,linux-aarch64' |
| 13 | +); |
| 14 | + |
| 15 | +if (!existsSync(assetsDir)) { |
| 16 | + fail(`Assets directory does not exist: ${assetsDir}`); |
| 17 | +} |
| 18 | + |
| 19 | +rmSync(outDir, { recursive: true, force: true }); |
| 20 | +mkdirSync(outDir, { recursive: true }); |
| 21 | + |
| 22 | +const collected = {}; |
| 23 | +for (const sigPath of walkFiles(assetsDir).filter((file) => file.endsWith('.sig'))) { |
| 24 | + const bundlePath = sigPath.slice(0, -'.sig'.length); |
| 25 | + if (!existsSync(bundlePath) || !isUpdaterBundle(bundlePath)) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + const platform = inferPlatform(bundlePath); |
| 30 | + if (!platform) { |
| 31 | + console.warn(`[collect-updater] Skipping artifact with unknown platform: ${bundlePath}`); |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + if (collected[platform]) { |
| 36 | + fail( |
| 37 | + `Duplicate updater artifact for ${platform}: ${bundlePath} conflicts with ${collected[platform].source}` |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + const outputName = updaterOutputName(bundlePath, version, platform); |
| 42 | + const outputPath = join(outDir, outputName); |
| 43 | + const outputSigPath = `${outputPath}.sig`; |
| 44 | + copyFileSync(bundlePath, outputPath); |
| 45 | + copyFileSync(sigPath, outputSigPath); |
| 46 | + |
| 47 | + collected[platform] = { |
| 48 | + source: bundlePath, |
| 49 | + output: outputPath, |
| 50 | + signature: outputSigPath, |
| 51 | + }; |
| 52 | + console.log(`[collect-updater] ${platform}: ${basename(outputPath)}`); |
| 53 | +} |
| 54 | + |
| 55 | +const missing = requiredPlatforms.filter((platform) => !collected[platform]); |
| 56 | +if (missing.length > 0) { |
| 57 | + const found = Object.keys(collected).sort(); |
| 58 | + const signedArtifacts = walkFiles(assetsDir) |
| 59 | + .filter((file) => file.endsWith('.sig')) |
| 60 | + .map((file) => file.replace(/\\/g, '/')) |
| 61 | + .sort(); |
| 62 | + console.error(`[collect-updater] Found platforms: ${found.length > 0 ? found.join(', ') : '(none)'}`); |
| 63 | + console.error('[collect-updater] Signed artifacts found:'); |
| 64 | + for (const artifact of signedArtifacts) { |
| 65 | + console.error(`[collect-updater] ${artifact}`); |
| 66 | + } |
| 67 | + fail(`Missing required updater platforms: ${missing.join(', ')}`); |
| 68 | +} |
| 69 | + |
| 70 | +console.log(`[collect-updater] Wrote ${Object.keys(collected).length} updater artifacts to ${outDir}`); |
| 71 | + |
| 72 | +function parseArgs(rawArgs) { |
| 73 | + const parsed = {}; |
| 74 | + for (let i = 0; i < rawArgs.length; i += 1) { |
| 75 | + const arg = rawArgs[i]; |
| 76 | + if (!arg.startsWith('--')) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + const key = arg.slice(2); |
| 80 | + const value = rawArgs[i + 1]; |
| 81 | + if (!value || value.startsWith('--')) { |
| 82 | + fail(`Missing value for --${key}`); |
| 83 | + } |
| 84 | + parsed[key] = value; |
| 85 | + i += 1; |
| 86 | + } |
| 87 | + return parsed; |
| 88 | +} |
| 89 | + |
| 90 | +function requireArg(parsed, key) { |
| 91 | + const value = parsed[key]; |
| 92 | + if (!value) { |
| 93 | + fail(`Missing required argument --${key}`); |
| 94 | + } |
| 95 | + return value; |
| 96 | +} |
| 97 | + |
| 98 | +function parseListArg(value) { |
| 99 | + return String(value) |
| 100 | + .split(',') |
| 101 | + .map((item) => item.trim()) |
| 102 | + .filter(Boolean); |
| 103 | +} |
| 104 | + |
| 105 | +function walkFiles(dir) { |
| 106 | + const files = []; |
| 107 | + for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 108 | + const fullPath = join(dir, entry.name); |
| 109 | + if (entry.isDirectory()) { |
| 110 | + files.push(...walkFiles(fullPath)); |
| 111 | + } else if (entry.isFile()) { |
| 112 | + files.push(fullPath); |
| 113 | + } |
| 114 | + } |
| 115 | + return files; |
| 116 | +} |
| 117 | + |
| 118 | +function isUpdaterBundle(file) { |
| 119 | + const lower = file.toLowerCase(); |
| 120 | + return ( |
| 121 | + lower.endsWith('.appimage') || |
| 122 | + lower.endsWith('.app.tar.gz') || |
| 123 | + lower.endsWith('.tar.gz') || |
| 124 | + lower.endsWith('.zip') || |
| 125 | + lower.endsWith('.exe') |
| 126 | + ); |
| 127 | +} |
| 128 | + |
| 129 | +function updaterOutputName(file, version, platform) { |
| 130 | + const lower = file.toLowerCase(); |
| 131 | + if (lower.endsWith('.app.tar.gz')) { |
| 132 | + return `BitFun_${version}_${platform}.app.tar.gz`; |
| 133 | + } |
| 134 | + if (lower.endsWith('.appimage')) { |
| 135 | + return `BitFun_${version}_${platform}.AppImage`; |
| 136 | + } |
| 137 | + if (lower.endsWith('.zip')) { |
| 138 | + return `BitFun_${version}_${platform}.zip`; |
| 139 | + } |
| 140 | + if (lower.endsWith('.exe')) { |
| 141 | + return `BitFun_${version}_${platform}-setup.exe`; |
| 142 | + } |
| 143 | + if (lower.endsWith('.tar.gz')) { |
| 144 | + return `BitFun_${version}_${platform}.tar.gz`; |
| 145 | + } |
| 146 | + fail(`Unsupported updater artifact extension: ${file}`); |
| 147 | +} |
| 148 | + |
| 149 | +function inferPlatform(file) { |
| 150 | + const lower = file.replace(/\\/g, '/').toLowerCase(); |
| 151 | + const arch = inferArch(lower); |
| 152 | + if (!arch) { |
| 153 | + return null; |
| 154 | + } |
| 155 | + |
| 156 | + if (lower.endsWith('.zip') || lower.includes('setup.exe')) { |
| 157 | + return `windows-${arch}`; |
| 158 | + } |
| 159 | + if (lower.endsWith('.appimage')) { |
| 160 | + return `linux-${arch}`; |
| 161 | + } |
| 162 | + if (lower.includes('.appimage.tar.gz')) { |
| 163 | + return `linux-${arch}`; |
| 164 | + } |
| 165 | + if (lower.includes('.app.tar.gz')) { |
| 166 | + return `darwin-${arch}`; |
| 167 | + } |
| 168 | + |
| 169 | + return null; |
| 170 | +} |
| 171 | + |
| 172 | +function inferArch(name) { |
| 173 | + if (/(^|[\\/_.-])(x86_64|x64|amd64)([\\/_.-]|$)/.test(name)) { |
| 174 | + return 'x86_64'; |
| 175 | + } |
| 176 | + if (/(^|[\\/_.-])(aarch64|arm64)([\\/_.-]|$)/.test(name)) { |
| 177 | + return 'aarch64'; |
| 178 | + } |
| 179 | + return null; |
| 180 | +} |
| 181 | + |
| 182 | +function fail(message) { |
| 183 | + console.error(`[collect-updater] ${message}`); |
| 184 | + process.exit(1); |
| 185 | +} |
0 commit comments