|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +/* |
| 4 | +Add the following scripts to package.json file: |
| 5 | + "prepublishOnly": "npm run lint && npm run test && npm run cleanBuild", |
| 6 | + "npmPublish": "npm publish", |
| 7 | + "gitPublish": "npm run lint && npm run test && npm run cleanBuild && node create-release.js", |
| 8 | + "preversion": "npm run lint && npm run test && npm run cleanBuild", |
| 9 | + "postversion": "git push && git push --tags && node create-release.js", |
| 10 | + "version:patch": "npm version patch", |
| 11 | + "version:minor": "npm version minor", |
| 12 | + "version:major": "npm version major", |
| 13 | +*/ |
| 14 | + |
| 15 | +import { execSync } from 'child_process'; |
| 16 | +import { readFileSync, writeFileSync, unlinkSync } from 'fs'; |
| 17 | +import path from 'path'; |
| 18 | +import readline from 'readline'; |
| 19 | + |
| 20 | +// Get the latest tag |
| 21 | +let tag = execSync('git describe --tags --abbrev=0').toString().trim(); |
| 22 | +if (tag.startsWith('v')) { |
| 23 | + tag = tag.substring(1); |
| 24 | +} |
| 25 | + |
| 26 | +// Read the changelog file |
| 27 | +const changelogPath = path.join(process.cwd(), 'CHANGELOG.md'); |
| 28 | +const changelog = readFileSync(changelogPath, 'utf8'); |
| 29 | + |
| 30 | +// Extract the relevant section from the changelog |
| 31 | +const changelogSection = extractChangelogSection(changelog, tag); |
| 32 | + |
| 33 | +const title = `Release ${tag}`; |
| 34 | +const notes = `Release notes for version ${tag}\n\n## [${tag}] ${changelogSection}`; |
| 35 | + |
| 36 | +// Log the release details |
| 37 | +console.log(`Creating release ${tag} with the following details:\nTitle:\n${title}\nNotes:\n${notes}`); |
| 38 | + |
| 39 | +// Write the release notes to a temporary file |
| 40 | +const notesFilePath = path.join(process.cwd(), 'release-notes.md'); |
| 41 | +writeFileSync(notesFilePath, notes); |
| 42 | + |
| 43 | +// Wait for user input before proceeding |
| 44 | +await pressAnyKey(); |
| 45 | + |
| 46 | +// Create the release using the temporary file |
| 47 | +execSync(`gh release create ${tag} -t "${title}" -F "${notesFilePath}"`, { stdio: 'inherit' }); |
| 48 | + |
| 49 | +// Clean up the temporary file |
| 50 | +unlinkSync(notesFilePath); |
| 51 | + |
| 52 | +/** |
| 53 | + * Extracts the relevant section from the changelog for the given tag. |
| 54 | + * Assumes that each version section in the changelog starts with a heading like "## [tag]". |
| 55 | + * @param {string} changelog - The content of the changelog file. |
| 56 | + * @param {string} tag - The tag for which to extract the changelog section. |
| 57 | + * @returns {string} - The extracted changelog section. |
| 58 | + */ |
| 59 | +function extractChangelogSection(changelog, tag) { |
| 60 | + const regex = new RegExp(`## \\[${tag}\\](.*?)(## \\[|$)`, 's'); |
| 61 | + const match = changelog.match(regex); |
| 62 | + return match ? match[1].trim() : 'No changelog entry found for this version.'; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Waits for the user to press any key. |
| 67 | + * @returns {Promise<void>} |
| 68 | + */ |
| 69 | +function pressAnyKey() { |
| 70 | + return new Promise((resolve) => { |
| 71 | + const rl = readline.createInterface({ |
| 72 | + input: process.stdin, |
| 73 | + output: process.stdout, |
| 74 | + }); |
| 75 | + |
| 76 | + rl.question('Press any key to continue...', () => { |
| 77 | + rl.close(); |
| 78 | + resolve(); |
| 79 | + }); |
| 80 | + }); |
| 81 | +} |
0 commit comments