|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | +const { Readable } = require("stream"); |
| 4 | +const { Octokit } = require("octokit"); |
| 5 | +const { createTokenAuth } = require("@octokit/auth-token"); |
| 6 | + |
| 7 | +const owner = "ruffle-rs"; |
| 8 | +const repo = "ruffle"; |
| 9 | +const assetName = "avm2_report.json"; |
| 10 | +const outputDir = path.resolve(__dirname, "..", "public"); |
| 11 | +const outputFile = path.join(outputDir, assetName); |
| 12 | + |
| 13 | +function createOctokit() { |
| 14 | + if (process.env.GITHUB_TOKEN) { |
| 15 | + const auth = createTokenAuth(process.env.GITHUB_TOKEN); |
| 16 | + return new Octokit({ authStrategy: auth }); |
| 17 | + } else { |
| 18 | + console.warn( |
| 19 | + "Please provide a GitHub Personal Access Token via the GITHUB_TOKEN environment variable.", |
| 20 | + ); |
| 21 | + return new Octokit(); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +async function downloadAsset(url, outputPath) { |
| 26 | + const res = await fetch(url); |
| 27 | + if (!res.ok) throw new Error(`Failed to download asset: ${res.statusText}`); |
| 28 | + |
| 29 | + fs.mkdirSync(path.dirname(outputPath), { recursive: true }); |
| 30 | + |
| 31 | + const nodeStream = Readable.from(res.body); |
| 32 | + const fileStream = fs.createWriteStream(outputPath); |
| 33 | + |
| 34 | + await new Promise((resolve, reject) => { |
| 35 | + nodeStream.pipe(fileStream); |
| 36 | + nodeStream.on("error", reject); |
| 37 | + fileStream.on("finish", resolve); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +async function downloadAVM2Report() { |
| 42 | + try { |
| 43 | + const octokit = createOctokit(); |
| 44 | + |
| 45 | + // Get recent releases |
| 46 | + const { data: releases } = await octokit.rest.repos.listReleases({ |
| 47 | + owner, |
| 48 | + repo, |
| 49 | + request: { next: { revalidate: 1800 } }, |
| 50 | + per_page: 7, |
| 51 | + }); |
| 52 | + |
| 53 | + // Find first release with the desired asset |
| 54 | + const latest = releases.find((release) => |
| 55 | + release.assets.some((a) => a.name === assetName), |
| 56 | + ); |
| 57 | + |
| 58 | + if (!latest) throw new Error(`No release contains asset "${assetName}"`); |
| 59 | + |
| 60 | + const asset = latest.assets.find((a) => a.name === assetName); |
| 61 | + if (!asset) throw new Error("Unexpected: asset missing"); |
| 62 | + |
| 63 | + await downloadAsset(asset.browser_download_url, outputFile); |
| 64 | + |
| 65 | + console.log( |
| 66 | + `✅ Downloaded ${assetName} from release "${latest.name}" to ${outputFile}`, |
| 67 | + ); |
| 68 | + } catch (err) { |
| 69 | + console.error("❌ Error:", err.message); |
| 70 | + process.exit(1); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +(async () => { |
| 75 | + await downloadAVM2Report(); |
| 76 | +})(); |
0 commit comments