|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from "fs" |
| 4 | +import { execSync } from "child_process" |
| 5 | + |
| 6 | +function main() { |
| 7 | + // Read package.json to get current version |
| 8 | + const pkg = JSON.parse(fs.readFileSync("package.json", "utf8")) |
| 9 | + const version = pkg.version |
| 10 | + |
| 11 | + console.log(`Creating tag for version ${version}...`) |
| 12 | + |
| 13 | + // Read changelog |
| 14 | + let changelog = fs.readFileSync("CHANGELOG.md", "utf8") |
| 15 | + |
| 16 | + // Update [Unreleased] section to current version if it exists |
| 17 | + const unreleasedHeader = "## [Unreleased]" |
| 18 | + const versionHeader = `## [${version}]` |
| 19 | + const today = new Date().toISOString().split("T")[0] // YYYY-MM-DD format |
| 20 | + const newVersionHeader = `## [${version}] - ${today}` |
| 21 | + |
| 22 | + if (changelog.includes(unreleasedHeader)) { |
| 23 | + console.log("Updating [Unreleased] section to current version...") |
| 24 | + changelog = changelog.replace(unreleasedHeader, newVersionHeader) |
| 25 | + |
| 26 | + // Add a new [Unreleased] section at the top for future changes |
| 27 | + const changelogLines = changelog.split("\n") |
| 28 | + const headerIndex = changelogLines.findIndex((line) => line.startsWith("## [")) |
| 29 | + if (headerIndex !== -1) { |
| 30 | + changelogLines.splice(headerIndex, 0, "## [Unreleased]", "") |
| 31 | + changelog = changelogLines.join("\n") |
| 32 | + } |
| 33 | + |
| 34 | + // Write updated changelog back to file |
| 35 | + fs.writeFileSync("CHANGELOG.md", changelog, "utf8") |
| 36 | + |
| 37 | + // Stage the changelog file for commit |
| 38 | + execSync("git add CHANGELOG.md", { stdio: "inherit" }) |
| 39 | + console.log("✅ Updated CHANGELOG.md and staged for commit") |
| 40 | + } |
| 41 | + |
| 42 | + // Find the section for this version |
| 43 | + const startIdx = |
| 44 | + changelog.indexOf(newVersionHeader) !== -1 ? changelog.indexOf(newVersionHeader) : changelog.indexOf(versionHeader) |
| 45 | + |
| 46 | + if (startIdx === -1) { |
| 47 | + console.error(`Error: Version ${version} not found in CHANGELOG.md`) |
| 48 | + process.exit(1) |
| 49 | + } |
| 50 | + |
| 51 | + // Find the next version section to know where this version's content ends |
| 52 | + const nextVersionIdx = changelog.indexOf("\n## [", startIdx + 1) |
| 53 | + |
| 54 | + // Extract the content for this version |
| 55 | + const content = changelog.substring(startIdx, nextVersionIdx === -1 ? undefined : nextVersionIdx).trim() |
| 56 | + |
| 57 | + console.log("Changelog content:") |
| 58 | + console.log(content) |
| 59 | + console.log() |
| 60 | + |
| 61 | + // Create the git tag with the changelog content as the message |
| 62 | + const tagName = `v${version}` |
| 63 | + |
| 64 | + // Check if tag already exists |
| 65 | + try { |
| 66 | + execSync(`git rev-parse ${tagName}`, { stdio: "pipe" }) |
| 67 | + console.log(`⚠️ Tag ${tagName} already exists. Skipping tag creation.`) |
| 68 | + return |
| 69 | + } catch { |
| 70 | + // Tag doesn't exist, continue with creation |
| 71 | + } |
| 72 | + |
| 73 | + // Escape backticks and other shell special characters in the content |
| 74 | + const escapedContent = content.replace(/`/g, "\\`").replace(/\$/g, "\\$") |
| 75 | + |
| 76 | + try { |
| 77 | + execSync(`git tag -a ${tagName} -m ${JSON.stringify(escapedContent)}`, { |
| 78 | + stdio: "inherit", |
| 79 | + }) |
| 80 | + console.log(`✅ Created tag ${tagName} successfully`) |
| 81 | + } catch (error) { |
| 82 | + console.error(`❌ Failed to create tag: ${error.message}`) |
| 83 | + process.exit(1) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +main() |
0 commit comments