|
| 1 | +#!/usr/bin/env node |
| 2 | +// Copyright (c) Mapbox, Inc. |
| 3 | +// Licensed under the MIT License. |
| 4 | + |
| 5 | +/** |
| 6 | + * Prepares CHANGELOG.md for a new release by: |
| 7 | + * 1. Replacing "## Unreleased" with "## {version}" |
| 8 | + * 2. Adding current date to the version heading |
| 9 | + * 3. Adding a new empty "## Unreleased" section at the top |
| 10 | + * |
| 11 | + * Usage: |
| 12 | + * node scripts/prepare-changelog-release.cjs <version> |
| 13 | + * npm run changelog:prepare-release <version> |
| 14 | + * |
| 15 | + * Example: |
| 16 | + * node scripts/prepare-changelog-release.cjs 1.0.0 |
| 17 | + */ |
| 18 | + |
| 19 | +const fs = require('node:fs'); |
| 20 | +const path = require('node:path'); |
| 21 | +const process = require('node:process'); |
| 22 | + |
| 23 | +function prepareChangelogRelease(version) { |
| 24 | + if (!version) { |
| 25 | + console.error('Error: Version number is required'); |
| 26 | + console.error('Usage: node scripts/prepare-changelog-release.cjs <version>'); |
| 27 | + process.exit(1); |
| 28 | + } |
| 29 | + |
| 30 | + // Validate version format (basic semver check) |
| 31 | + if (!/^\d+\.\d+\.\d+(-[a-zA-Z0-9.-]+)?$/.test(version)) { |
| 32 | + console.error(`Error: Invalid version format: ${version}`); |
| 33 | + console.error('Expected format: X.Y.Z or X.Y.Z-prerelease'); |
| 34 | + process.exit(1); |
| 35 | + } |
| 36 | + |
| 37 | + const changelogPath = path.join(process.cwd(), 'CHANGELOG.md'); |
| 38 | + |
| 39 | + // Check if CHANGELOG.md exists |
| 40 | + if (!fs.existsSync(changelogPath)) { |
| 41 | + console.error('Error: CHANGELOG.md not found'); |
| 42 | + process.exit(1); |
| 43 | + } |
| 44 | + |
| 45 | + // Read CHANGELOG.md |
| 46 | + const content = fs.readFileSync(changelogPath, 'utf8'); |
| 47 | + |
| 48 | + // Check if "## Unreleased" exists |
| 49 | + if (!content.includes('## Unreleased')) { |
| 50 | + console.error('Error: No "## Unreleased" section found in CHANGELOG.md'); |
| 51 | + console.error('Nothing to release - add changes under "## Unreleased" first'); |
| 52 | + process.exit(1); |
| 53 | + } |
| 54 | + |
| 55 | + // Get current date in YYYY-MM-DD format |
| 56 | + const date = new Date().toISOString().split('T')[0]; |
| 57 | + |
| 58 | + // Replace "## Unreleased" with "## {version} - {date}" |
| 59 | + // and add a new "## Unreleased" section at the top |
| 60 | + const updatedContent = content.replace( |
| 61 | + '## Unreleased', |
| 62 | + `## Unreleased\n\n## ${version} - ${date}` |
| 63 | + ); |
| 64 | + |
| 65 | + // Write updated content back to CHANGELOG.md |
| 66 | + fs.writeFileSync(changelogPath, updatedContent, 'utf8'); |
| 67 | + |
| 68 | + console.log(`✓ CHANGELOG.md updated successfully`); |
| 69 | + console.log(` - Released version ${version} (${date})`); |
| 70 | + console.log(` - Added new "Unreleased" section`); |
| 71 | + console.log(''); |
| 72 | + console.log('Next steps:'); |
| 73 | + console.log(' 1. Review CHANGELOG.md'); |
| 74 | + console.log(' 2. Commit changes: git add CHANGELOG.md && git commit -m "Release v' + version + '"'); |
| 75 | + console.log(' 3. Create tag: git tag v' + version); |
| 76 | + console.log(' 4. Push: git push && git push --tags'); |
| 77 | +} |
| 78 | + |
| 79 | +// Process command line arguments |
| 80 | +const version = process.argv[2]; |
| 81 | +prepareChangelogRelease(version); |
0 commit comments