|
| 1 | +#!/usr/bin/env ts-node-script |
| 2 | + |
| 3 | +import { gh } from "../src/github"; |
| 4 | +import { includeReadmeOssIntoMpk } from "../src/oss-clearance"; |
| 5 | +import { rm } from "../src/shell"; |
| 6 | +import { mkdtemp } from "node:fs/promises"; |
| 7 | +import { join } from "node:path"; |
| 8 | +import { tmpdir } from "node:os"; |
| 9 | +import chalk from "chalk"; |
| 10 | + |
| 11 | +/** |
| 12 | + * This script processes GitHub release artifacts to include READMEOSS HTML files into MPK files. |
| 13 | + * |
| 14 | + * Workflow: |
| 15 | + * 1. Check release assets for .mpk and .html files |
| 16 | + * 2. If only MPK exists - do nothing |
| 17 | + * 3. If both MPK and HTML exist: |
| 18 | + * - Download both files |
| 19 | + * - Merge HTML into MPK |
| 20 | + * - Replace the MPK asset in the release |
| 21 | + */ |
| 22 | + |
| 23 | +async function main(): Promise<void> { |
| 24 | + const releaseTag = process.env.TAG; |
| 25 | + |
| 26 | + if (!releaseTag) { |
| 27 | + throw new Error("TAG environment variable is required"); |
| 28 | + } |
| 29 | + |
| 30 | + await gh.ensureAuth(); |
| 31 | + |
| 32 | + console.log(chalk.bold.cyan(`\n🔍 Processing release: ${releaseTag}\n`)); |
| 33 | + |
| 34 | + const releaseId = await gh.getReleaseIdByReleaseTag(releaseTag); |
| 35 | + |
| 36 | + if (!releaseId) { |
| 37 | + throw new Error(`Could not find release ID for tag '${releaseTag}'`); |
| 38 | + } |
| 39 | + |
| 40 | + // Step 1: Get release artifacts |
| 41 | + console.log(chalk.blue("📦 Fetching release artifacts...")); |
| 42 | + const artifacts = await gh.listReleaseAssets(releaseId); |
| 43 | + |
| 44 | + const mpkAsset = artifacts.find(asset => asset.name.endsWith(".mpk")); |
| 45 | + const htmlAsset = artifacts.find(asset => asset.name.endsWith(".html")); |
| 46 | + |
| 47 | + if (!mpkAsset) { |
| 48 | + throw new Error(`No MPK file found in release '${releaseTag}'`); |
| 49 | + } |
| 50 | + |
| 51 | + console.log(chalk.green(`✅ Found MPK: ${mpkAsset.name}`)); |
| 52 | + |
| 53 | + // Step 2: Check if HTML file exists |
| 54 | + if (!htmlAsset) { |
| 55 | + console.log(chalk.yellow("⚠️ No HTML file found in release - nothing to include")); |
| 56 | + console.log(chalk.gray(" Skipping MPK modification\n")); |
| 57 | + process.exit(0); |
| 58 | + } |
| 59 | + |
| 60 | + console.log(chalk.green(`✅ Found HTML: ${htmlAsset.name}`)); |
| 61 | + |
| 62 | + // Step 3: Download both files to temp directory |
| 63 | + console.log(chalk.blue("\n📥 Downloading artifacts...")); |
| 64 | + |
| 65 | + const tmpFolder = await mkdtemp(join(tmpdir(), "mpk-oss-include-")); |
| 66 | + const mpkPath = join(tmpFolder, mpkAsset.name); |
| 67 | + const htmlPath = join(tmpFolder, htmlAsset.name); |
| 68 | + |
| 69 | + try { |
| 70 | + console.log(chalk.gray(` → Downloading ${mpkAsset.name}...`)); |
| 71 | + await gh.downloadReleaseAsset(mpkAsset.id, mpkPath); |
| 72 | + |
| 73 | + console.log(chalk.gray(` → Downloading ${htmlAsset.name}...`)); |
| 74 | + await gh.downloadReleaseAsset(htmlAsset.id, htmlPath); |
| 75 | + |
| 76 | + console.log(chalk.green("✅ Downloads completed")); |
| 77 | + |
| 78 | + // Step 4: Include HTML into MPK |
| 79 | + console.log(chalk.blue("\n🔧 Merging HTML into MPK...")); |
| 80 | + await includeReadmeOssIntoMpk(htmlPath, mpkPath); |
| 81 | + console.log(chalk.green("✅ Merge completed")); |
| 82 | + |
| 83 | + // Step 5: Remove old assets, upload patched MPK |
| 84 | + console.log(chalk.blue("\n🔄 Replacing MPK asset in release...")); |
| 85 | + |
| 86 | + console.log(chalk.gray(` → Deleting old MPK asset...`)); |
| 87 | + await gh.deleteReleaseAsset(mpkAsset.id); |
| 88 | + |
| 89 | + console.log(chalk.gray(` → Deleting old HTML asset...`)); |
| 90 | + await gh.deleteReleaseAsset(htmlAsset.id); |
| 91 | + |
| 92 | + console.log(chalk.gray(` → Uploading modified MPK...`)); |
| 93 | + const newAsset = await gh.uploadReleaseAsset(releaseId, mpkPath, mpkAsset.name); |
| 94 | + |
| 95 | + console.log(chalk.green(`✅ Successfully replaced MPK asset (ID: ${newAsset.id})`)); |
| 96 | + console.log(chalk.bold.green(`\n🎉 Process completed successfully!\n`)); |
| 97 | + } finally { |
| 98 | + // Step 8: Cleanup temp files |
| 99 | + console.log(chalk.gray("🧹 Cleaning up temporary files...")); |
| 100 | + await rm("-rf", tmpFolder); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +main().catch(error => { |
| 105 | + console.error(chalk.red(`\n❌ Error: ${error.message}\n`)); |
| 106 | + console.error(error); |
| 107 | + process.exit(1); |
| 108 | +}); |
0 commit comments