|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require("fs"); |
| 4 | +const { execSync } = require("child_process"); |
| 5 | +const crypto = require("crypto"); |
| 6 | + |
| 7 | +function exec(command, options = {}) { |
| 8 | + console.log(`\n→ ${command}`); |
| 9 | + return execSync(command, { stdio: "inherit", ...options }); |
| 10 | +} |
| 11 | + |
| 12 | +function getOutput(command) { |
| 13 | + return execSync(command, { encoding: "utf-8" }).trim(); |
| 14 | +} |
| 15 | + |
| 16 | +async function release() { |
| 17 | + try { |
| 18 | + // Get current version |
| 19 | + const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8")); |
| 20 | + const version = pkg.version; |
| 21 | + const tarballName = `explainshell-cli-v${version}.tar.gz`; |
| 22 | + const githubRepo = "mjsarfatti/explainshell-cli"; |
| 23 | + |
| 24 | + console.log(`\n📦 Releasing version ${version}`); |
| 25 | + |
| 26 | + // 1. Run tests |
| 27 | + console.log("\n1️⃣ Running tests..."); |
| 28 | + exec("npm test"); |
| 29 | + |
| 30 | + // 2. Build |
| 31 | + console.log("\n2️⃣ Building..."); |
| 32 | + exec("npm run build"); |
| 33 | + |
| 34 | + // 3. Create tarball |
| 35 | + console.log("\n3️⃣ Creating tarball..."); |
| 36 | + exec( |
| 37 | + `tar -czf ${tarballName} --exclude=node_modules --exclude=.git --exclude=tests --exclude='*.tar.gz' --exclude='.DS_Store' .` |
| 38 | + ); |
| 39 | + |
| 40 | + // 4. Calculate SHA256 |
| 41 | + console.log("\n4️⃣ Calculating SHA256..."); |
| 42 | + const fileBuffer = fs.readFileSync(tarballName); |
| 43 | + const sha256 = crypto.createHash("sha256").update(fileBuffer).digest("hex"); |
| 44 | + console.log(` SHA256: ${sha256}`); |
| 45 | + |
| 46 | + // 5. Update Homebrew formula |
| 47 | + console.log("\n5️⃣ Updating Homebrew formula..."); |
| 48 | + let formula = fs.readFileSync("explainshell-cli.rb", "utf-8"); |
| 49 | + formula = formula.replace( |
| 50 | + /url\s+"[^"]+"/, |
| 51 | + `url "https://github.com/${githubRepo}/releases/download/v${version}/${tarballName}"` |
| 52 | + ); |
| 53 | + formula = formula.replace(/sha256\s+"[^"]+"/, `sha256 "${sha256}"`); |
| 54 | + fs.writeFileSync("explainshell-cli.rb", formula); |
| 55 | + console.log(" ✓ Updated explainshell-cli.rb"); |
| 56 | + |
| 57 | + // 6. Commit changes |
| 58 | + console.log("\n6️⃣ Committing changes..."); |
| 59 | + exec("git add package.json package-lock.json explainshell-cli.rb"); |
| 60 | + exec(`git commit -m "Release v${version}"`); |
| 61 | + |
| 62 | + // 7. Create git tag |
| 63 | + console.log("\n7️⃣ Creating git tag..."); |
| 64 | + exec(`git tag -a v${version} -m "Release v${version}"`); |
| 65 | + |
| 66 | + // 8. Push to GitHub |
| 67 | + console.log("\n8️⃣ Pushing to GitHub..."); |
| 68 | + exec("git push origin main"); |
| 69 | + exec("git push origin --tags"); |
| 70 | + |
| 71 | + // 9. Create GitHub release |
| 72 | + console.log("\n9️⃣ Creating GitHub release..."); |
| 73 | + |
| 74 | + // Check if gh CLI is available |
| 75 | + try { |
| 76 | + getOutput("which gh"); |
| 77 | + exec( |
| 78 | + `gh release create v${version} ${tarballName} --title "v${version}" --notes "Release v${version}"` |
| 79 | + ); |
| 80 | + console.log(" ✓ GitHub release created"); |
| 81 | + } catch (error) { |
| 82 | + console.log( |
| 83 | + "\n⚠️ GitHub CLI (gh) not found. Please create the release manually:" |
| 84 | + ); |
| 85 | + console.log(` 1. Go to https://github.com/${githubRepo}/releases/new`); |
| 86 | + console.log(` 2. Tag: v${version}`); |
| 87 | + console.log(` 3. Upload ${tarballName}`); |
| 88 | + console.log(` 4. Publish release`); |
| 89 | + console.log( |
| 90 | + "\nℹ️ For next time, you can install the GitHub CLI with `brew install gh`" |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + console.log("\n✅ Release complete!"); |
| 95 | + console.log(`\nVersion: ${version}`); |
| 96 | + console.log(`Tarball: ${tarballName}`); |
| 97 | + console.log(`SHA256: ${sha256}`); |
| 98 | + } catch (error) { |
| 99 | + console.error("\n❌ Release failed:", error.message); |
| 100 | + process.exit(1); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +release(); |
0 commit comments