|
| 1 | +const { exec } = require('child_process'); |
| 2 | +const fs = require('fs'); |
| 3 | +const readline = require('readline'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const versionRegex = /^\d{1,4}\.\d{1,4}\.\d{1,4}$/; |
| 7 | + |
| 8 | +const changelogPath = path.join(__dirname, '..', 'CHANGELOG.md'); |
| 9 | +console.log(changelogPath); |
| 10 | +let data = fs.readFileSync(changelogPath, 'utf8'); |
| 11 | + |
| 12 | +// Find the current version number |
| 13 | +const match = /\[unreleased\]: https:\/\/github\.com\/gitkraken\/vscode-gitlens\/compare\/v(.+)\.\.\.HEAD/.exec(data); |
| 14 | +const currentVersion = match?.[1]; |
| 15 | +if (currentVersion == null || versionRegex.test(currentVersion) === false) { |
| 16 | + console.error('Unable to find current version number.'); |
| 17 | + return; |
| 18 | +} |
| 19 | + |
| 20 | +// Create readline interface for getting input from user |
| 21 | +const rl = readline.createInterface({ |
| 22 | + input: process.stdin, |
| 23 | + output: process.stdout, |
| 24 | +}); |
| 25 | + |
| 26 | +// Ask for new version number |
| 27 | +rl.question(`Enter the new version number (format x.x.x, current is ${currentVersion}): `, function (version) { |
| 28 | + // Validate the version input |
| 29 | + if (!versionRegex.test(version)) { |
| 30 | + console.error( |
| 31 | + 'Invalid version number. Please use the format x.y.z where x, y, and z are positive numbers no greater than 4 digits.', |
| 32 | + ); |
| 33 | + rl.close(); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // Get today's date |
| 38 | + const today = new Date(); |
| 39 | + const yyyy = today.getFullYear(); |
| 40 | + const mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0! |
| 41 | + const dd = String(today.getDate()).padStart(2, '0'); |
| 42 | + |
| 43 | + const newVersionHeader = `## [Unreleased]\n\n## [${version}] - ${yyyy}-${mm}-${dd}`; |
| 44 | + const newVersionLink = `[${version}]: https://github.com/gitkraken/vscode-gitlens/compare/v${currentVersion}...gitkraken:v${version}`; |
| 45 | + |
| 46 | + // Add the new version header below the ## [Unreleased] header |
| 47 | + data = data.replace('## [Unreleased]', newVersionHeader); |
| 48 | + |
| 49 | + const unreleasedLink = match[0].replace(/\/compare\/v(.+?)\.\.\.HEAD/, `\/compare\/v${version}...HEAD`); |
| 50 | + |
| 51 | + // Update the [unreleased]: line |
| 52 | + data = data.replace(match[0], `${unreleasedLink}\n${newVersionLink}`); |
| 53 | + |
| 54 | + // Writing the updated version data to CHANGELOG |
| 55 | + fs.writeFileSync(changelogPath, data); |
| 56 | + |
| 57 | + // Stage CHANGELOG |
| 58 | + exec('git add CHANGELOG.md', err => { |
| 59 | + if (err) { |
| 60 | + console.error(`Unable to stage CHANGELOG.md: ${err}`); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Call 'yarn version' to commit and create the tag |
| 65 | + exec(`yarn version --new-version ${version}`, err => { |
| 66 | + if (err) { |
| 67 | + console.error(`'yarn version' failed: ${err}`); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + console.log(`${version} is ready for release.`); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + rl.close(); |
| 76 | +}); |
0 commit comments