|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +/** |
| 4 | + * Freebuff CLI release script. |
| 5 | + * |
| 6 | + * Triggers the freebuff-release.yml GitHub Actions workflow |
| 7 | + * to build, publish, and release the Freebuff CLI to npm. |
| 8 | + * |
| 9 | + * Usage: |
| 10 | + * bun freebuff/cli/release.ts [patch|minor|major] |
| 11 | + * |
| 12 | + * Requires: |
| 13 | + * CODEBUFF_GITHUB_TOKEN environment variable |
| 14 | + */ |
| 15 | + |
| 16 | +import { execSync } from 'child_process' |
| 17 | + |
| 18 | +const args = process.argv.slice(2) |
| 19 | +const versionType = args[0] || 'patch' |
| 20 | + |
| 21 | +function log(message: string) { |
| 22 | + console.log(`${message}`) |
| 23 | +} |
| 24 | + |
| 25 | +function error(message: string): never { |
| 26 | + console.error(`❌ ${message}`) |
| 27 | + process.exit(1) |
| 28 | +} |
| 29 | + |
| 30 | +function formatTimestamp() { |
| 31 | + const now = new Date() |
| 32 | + const options = { |
| 33 | + month: 'long', |
| 34 | + day: 'numeric', |
| 35 | + hour: '2-digit', |
| 36 | + minute: '2-digit', |
| 37 | + second: '2-digit', |
| 38 | + timeZoneName: 'short', |
| 39 | + } as const |
| 40 | + return now.toLocaleDateString('en-US', options) |
| 41 | +} |
| 42 | + |
| 43 | +function checkGitHubToken() { |
| 44 | + const token = process.env.CODEBUFF_GITHUB_TOKEN |
| 45 | + if (!token) { |
| 46 | + error( |
| 47 | + 'CODEBUFF_GITHUB_TOKEN environment variable is required but not set.\n' + |
| 48 | + 'Please set it with your GitHub personal access token or use the infisical setup.', |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + process.env.GITHUB_TOKEN = token |
| 53 | + return token |
| 54 | +} |
| 55 | + |
| 56 | +async function triggerWorkflow(versionType: string) { |
| 57 | + if (!process.env.GITHUB_TOKEN) { |
| 58 | + error('GITHUB_TOKEN environment variable is required but not set') |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + const triggerCmd = `curl -s -w "HTTP Status: %{http_code}" -X POST \ |
| 63 | + -H "Accept: application/vnd.github.v3+json" \ |
| 64 | + -H "Authorization: token ${process.env.GITHUB_TOKEN}" \ |
| 65 | + -H "Content-Type: application/json" \ |
| 66 | + https://api.github.com/repos/CodebuffAI/codebuff/actions/workflows/freebuff-release.yml/dispatches \ |
| 67 | + -d '{"ref":"main","inputs":{"version_type":"${versionType}"}}'` |
| 68 | + |
| 69 | + const response = execSync(triggerCmd, { encoding: 'utf8' }) |
| 70 | + |
| 71 | + if (response.includes('workflow_dispatch')) { |
| 72 | + log(`⚠️ Workflow dispatch failed: ${response}`) |
| 73 | + log( |
| 74 | + 'Please manually trigger the workflow at: https://github.com/CodebuffAI/codebuff/actions/workflows/freebuff-release.yml', |
| 75 | + ) |
| 76 | + } else { |
| 77 | + log('🎉 Freebuff release workflow triggered!') |
| 78 | + } |
| 79 | + } catch (err: unknown) { |
| 80 | + const message = err instanceof Error ? err.message : String(err) |
| 81 | + log(`⚠️ Failed to trigger workflow automatically: ${message}`) |
| 82 | + log( |
| 83 | + 'You may need to trigger it manually at: https://github.com/CodebuffAI/codebuff/actions/workflows/freebuff-release.yml', |
| 84 | + ) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +async function main() { |
| 89 | + log('🚀 Initiating Freebuff release...') |
| 90 | + log(`Date: ${formatTimestamp()}`) |
| 91 | + |
| 92 | + checkGitHubToken() |
| 93 | + log('✅ Using local CODEBUFF_GITHUB_TOKEN') |
| 94 | + |
| 95 | + log(`Version bump type: ${versionType}`) |
| 96 | + |
| 97 | + await triggerWorkflow(versionType) |
| 98 | + |
| 99 | + log('') |
| 100 | + log( |
| 101 | + 'Monitor progress at: https://github.com/CodebuffAI/codebuff/actions/workflows/freebuff-release.yml', |
| 102 | + ) |
| 103 | +} |
| 104 | + |
| 105 | +main().catch((err: unknown) => { |
| 106 | + const message = err instanceof Error ? err.message : String(err) |
| 107 | + error(`Release failed: ${message}`) |
| 108 | +}) |
0 commit comments