|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { execSync } = require("child_process"); |
| 4 | +const https = require("https"); |
| 5 | +const fs = require("fs"); |
| 6 | + |
| 7 | +function execCommand(command) { |
| 8 | + try { |
| 9 | + return execSync(command, { encoding: "utf8" }).trim(); |
| 10 | + } catch (error) { |
| 11 | + console.error(`Command failed: ${command}`); |
| 12 | + throw error; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +function makeGitHubRequest(options, data) { |
| 17 | + return new Promise((resolve, reject) => { |
| 18 | + const req = https.request(options, (res) => { |
| 19 | + let body = ""; |
| 20 | + res.on("data", (chunk) => (body += chunk)); |
| 21 | + res.on("end", () => { |
| 22 | + if (res.statusCode >= 200 && res.statusCode < 300) { |
| 23 | + resolve(JSON.parse(body)); |
| 24 | + } else { |
| 25 | + reject(new Error(`GitHub API error: ${res.statusCode} - ${body}`)); |
| 26 | + } |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + req.on("error", reject); |
| 31 | + |
| 32 | + if (data) { |
| 33 | + req.write(JSON.stringify(data)); |
| 34 | + } |
| 35 | + |
| 36 | + req.end(); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +async function createGitHubRelease() { |
| 41 | + try { |
| 42 | + // Get package info |
| 43 | + const packageJson = JSON.parse(fs.readFileSync("package.json", "utf8")); |
| 44 | + const version = packageJson.version; |
| 45 | + const tagName = `v${version}`; |
| 46 | + |
| 47 | + // Get repository info from package.json |
| 48 | + const repoUrl = packageJson.repository.url; |
| 49 | + const repoMatch = repoUrl.match(/github\.com[\/:]([^\/]+)\/([^\/\.]+)/); |
| 50 | + |
| 51 | + if (!repoMatch) { |
| 52 | + throw new Error("Could not parse GitHub repository from package.json"); |
| 53 | + } |
| 54 | + |
| 55 | + const owner = repoMatch[1]; |
| 56 | + const repo = repoMatch[2]; |
| 57 | + |
| 58 | + // Get GitHub token from environment |
| 59 | + const token = process.env.GITHUB_TOKEN; |
| 60 | + if (!token) { |
| 61 | + console.log("⚠️ GITHUB_TOKEN not found in environment variables."); |
| 62 | + console.log("📝 To create GitHub releases automatically, please:"); |
| 63 | + console.log(" 1. Go to https://github.com/settings/tokens"); |
| 64 | + console.log(' 2. Create a new token with "repo" permissions'); |
| 65 | + console.log( |
| 66 | + " 3. Add it to your environment: export GITHUB_TOKEN=your_token" |
| 67 | + ); |
| 68 | + console.log(" 4. Or add it to your ~/.zshrc or ~/.bashrc"); |
| 69 | + console.log("\n✅ For now, you can manually create a release at:"); |
| 70 | + console.log( |
| 71 | + ` https://github.com/${owner}/${repo}/releases/new?tag=${tagName}` |
| 72 | + ); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // Get recent commits for release notes |
| 77 | + let releaseNotes = ""; |
| 78 | + try { |
| 79 | + const lastTag = execCommand( |
| 80 | + 'git describe --tags --abbrev=0 HEAD~1 2>/dev/null || echo ""' |
| 81 | + ); |
| 82 | + const commitRange = lastTag ? `${lastTag}..HEAD` : "HEAD"; |
| 83 | + const commits = execCommand( |
| 84 | + `git log ${commitRange} --pretty=format:"- %s" --no-merges` |
| 85 | + ); |
| 86 | + releaseNotes = commits || "- Initial release"; |
| 87 | + } catch (error) { |
| 88 | + releaseNotes = "- Package updates and improvements"; |
| 89 | + } |
| 90 | + |
| 91 | + // Create the release |
| 92 | + const releaseData = { |
| 93 | + tag_name: tagName, |
| 94 | + target_commitish: "main", |
| 95 | + name: `Release ${tagName}`, |
| 96 | + body: `## Changes\n\n${releaseNotes}\n\n## Installation\n\n\`\`\`bash\nnpm install ${packageJson.name}@${version}\n\`\`\``, |
| 97 | + draft: false, |
| 98 | + prerelease: version.includes("-"), |
| 99 | + }; |
| 100 | + |
| 101 | + const options = { |
| 102 | + hostname: "api.github.com", |
| 103 | + port: 443, |
| 104 | + path: `/repos/${owner}/${repo}/releases`, |
| 105 | + method: "POST", |
| 106 | + headers: { |
| 107 | + Authorization: `token ${token}`, |
| 108 | + "User-Agent": "npm-release-script", |
| 109 | + "Content-Type": "application/json", |
| 110 | + Accept: "application/vnd.github.v3+json", |
| 111 | + }, |
| 112 | + }; |
| 113 | + |
| 114 | + console.log(`🔄 Creating GitHub release for ${tagName}...`); |
| 115 | + const release = await makeGitHubRequest(options, releaseData); |
| 116 | + |
| 117 | + console.log(`✅ GitHub release created successfully!`); |
| 118 | + console.log(`🔗 Release URL: ${release.html_url}`); |
| 119 | + } catch (error) { |
| 120 | + console.error("❌ Failed to create GitHub release:", error.message); |
| 121 | + |
| 122 | + // Provide fallback instructions |
| 123 | + const packageJson = JSON.parse(fs.readFileSync("package.json", "utf8")); |
| 124 | + const version = packageJson.version; |
| 125 | + const tagName = `v${version}`; |
| 126 | + const repoUrl = packageJson.repository.url; |
| 127 | + const repoMatch = repoUrl.match(/github\.com[\/:]([^\/]+)\/([^\/\.]+)/); |
| 128 | + |
| 129 | + if (repoMatch) { |
| 130 | + const owner = repoMatch[1]; |
| 131 | + const repo = repoMatch[2]; |
| 132 | + console.log(`\n📝 You can manually create the release at:`); |
| 133 | + console.log( |
| 134 | + ` https://github.com/${owner}/${repo}/releases/new?tag=${tagName}` |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +createGitHubRelease(); |
0 commit comments