|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'node:fs' |
| 4 | +import path from 'node:path' |
| 5 | +import process from 'node:process' |
| 6 | +import { fileURLToPath } from 'node:url' |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url) |
| 9 | +const __dirname = path.dirname(__filename) |
| 10 | + |
| 11 | +const CHANGELOG_PATH = path.join(__dirname, '../CHANGELOG.md') |
| 12 | + |
| 13 | +/** |
| 14 | + * Updates the CHANGELOG.md file by: |
| 15 | + * 1. Converting [Unreleased] section to a new version entry |
| 16 | + * 2. Adding today's date |
| 17 | + * 3. Creating a new empty [Unreleased] section |
| 18 | + * 4. Updating version comparison links at the bottom |
| 19 | + * |
| 20 | + * @param {string} version - The new version number (e.g., "1.1.8") |
| 21 | + */ |
| 22 | +function updateChangelog(version) { |
| 23 | + try { |
| 24 | + // Read current changelog |
| 25 | + const changelog = fs.readFileSync(CHANGELOG_PATH, 'utf-8') |
| 26 | + |
| 27 | + // Get today's date in YYYY-MM-DD format |
| 28 | + const date = new Date().toISOString().split('T')[0] |
| 29 | + |
| 30 | + // Check if there's actually content in the Unreleased section |
| 31 | + const unreleasedPattern = /## \[Unreleased\]\n([\s\S]*?)\n## \[/ |
| 32 | + const unreleasedMatch = changelog.match(unreleasedPattern) |
| 33 | + if (!unreleasedMatch || !unreleasedMatch[1].trim()) { |
| 34 | + console.error('❌ Error: No unreleased changes found in CHANGELOG.md') |
| 35 | + console.error('Please add changes to the [Unreleased] section before creating a release.') |
| 36 | + process.exit(1) |
| 37 | + } |
| 38 | + |
| 39 | + // Replace [Unreleased] header with new version and create new empty [Unreleased] section |
| 40 | + let updatedChangelog = changelog.replace( |
| 41 | + '## [Unreleased]', |
| 42 | + `## [Unreleased]\n\n## [${version}] - ${date}`, |
| 43 | + ) |
| 44 | + |
| 45 | + // Update version comparison links at the bottom |
| 46 | + // Find the [Unreleased] link pattern |
| 47 | + const linkPattern = /\[Unreleased\]: (.+)\/compare\/v(.+)\.\.\.HEAD/ |
| 48 | + const unreleasedLinkMatch = updatedChangelog.match(linkPattern) |
| 49 | + |
| 50 | + if (unreleasedLinkMatch) { |
| 51 | + const repoUrl = unreleasedLinkMatch[1] |
| 52 | + const previousVersion = unreleasedLinkMatch[2] |
| 53 | + |
| 54 | + // Update the [Unreleased] link to point to new version |
| 55 | + updatedChangelog = updatedChangelog.replace( |
| 56 | + linkPattern, |
| 57 | + `[Unreleased]: ${repoUrl}/compare/v${version}...HEAD`, |
| 58 | + ) |
| 59 | + |
| 60 | + // Add new version comparison link right after [Unreleased] |
| 61 | + const newVersionLink = `[${version}]: ${repoUrl}/compare/v${previousVersion}...v${version}` |
| 62 | + updatedChangelog = updatedChangelog.replace( |
| 63 | + `[Unreleased]: ${repoUrl}/compare/v${version}...HEAD`, |
| 64 | + `[Unreleased]: ${repoUrl}/compare/v${version}...HEAD\n${newVersionLink}`, |
| 65 | + ) |
| 66 | + } |
| 67 | + else { |
| 68 | + console.warn('⚠️ Warning: Could not find version comparison links. Please update them manually.') |
| 69 | + } |
| 70 | + |
| 71 | + // Write updated changelog |
| 72 | + fs.writeFileSync(CHANGELOG_PATH, updatedChangelog) |
| 73 | + |
| 74 | + console.log(`✅ Successfully updated CHANGELOG.md for version ${version}`) |
| 75 | + console.log(` - Date: ${date}`) |
| 76 | + console.log(` - Created new [${version}] section`) |
| 77 | + console.log(` - Updated version comparison links`) |
| 78 | + } |
| 79 | + catch (error) { |
| 80 | + console.error('❌ Error updating changelog:', error.message) |
| 81 | + process.exit(1) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Main execution |
| 86 | +const version = process.argv[2] |
| 87 | + |
| 88 | +if (!version) { |
| 89 | + console.error('❌ Error: Please provide a version number') |
| 90 | + console.error('Usage: node update-changelog.js <version>') |
| 91 | + console.error('Example: node update-changelog.js 1.1.8') |
| 92 | + process.exit(1) |
| 93 | +} |
| 94 | + |
| 95 | +// Validate version format (basic semver check) |
| 96 | +if (!/^\d+\.\d+\.\d+$/.test(version)) { |
| 97 | + console.error(`❌ Error: Invalid version format: ${version}`) |
| 98 | + console.error('Version must follow semantic versioning (e.g., 1.2.3)') |
| 99 | + process.exit(1) |
| 100 | +} |
| 101 | + |
| 102 | +updateChangelog(version) |
0 commit comments