From f7877d4617792f729b75b006f50a1cd640a39036 Mon Sep 17 00:00:00 2001 From: Burgil <41600149+burgil@users.noreply.github.com> Date: Thu, 17 Oct 2024 02:41:39 +0300 Subject: [PATCH] Improved Version Bumper --- README.md | 6 ++- bump.ts | 145 +++++++++++++++++++++++++++++++++++++-------------- index.ts | 2 +- jsr.json | 2 +- package.json | 2 +- 5 files changed, 115 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 00c1c06..466ec00 100644 --- a/README.md +++ b/README.md @@ -320,7 +320,11 @@ deno run bump.ts ## Changelog -# Version 1.3.0 - Version Bump Script for Contributors +### Version 1.3.1 - Improved Version Bumper + +- **Enhanced version bumping system**: Implemented a user-interactive auto bump system that requests confirmation before bumping the minor or major version. Added feature to input brief and secondary descriptions of changes for the README.md file. + +### Version 1.3.0 - Version Bump Script for Contributors - **Added Version Bump Script**: Implemented a simple script to streamline version bumping across multiple files, improving development efficiency. diff --git a/bump.ts b/bump.ts index aea8d57..a01f298 100644 --- a/bump.ts +++ b/bump.ts @@ -1,4 +1,5 @@ import fs from 'node:fs'; +import * as readline from 'node:readline'; const bump_files: { [key: string]: { @@ -20,56 +21,124 @@ const bump_files: { }, "README.md": { // TODO: Ask for required information search: '## Changelog', - replace: '## Changelog\n\n# Version newVersion - mainDescription\n\n- **secondaryDescription**: explainChanges', + replace: '## Changelog\n\n### Version newVersion - updateTitle\n\n- **mainDescription**: secondaryDescription', }, }; const packageJsonPath = './package.json'; -let currentVersion: null | string = null; -try { - const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); - currentVersion = packageJson.version; -} catch (error) { - if (error instanceof Error) { - console.error(`Error reading package.json: ${error.message}`); - } else { - console.error(`Unknown error reading package.json: ${error}`); - } -} -if (currentVersion === null) { - console.error('Unable to retrieve current version'); - process.exit(1); +const MAX_PATCH_VERSION = 10; +const MAX_MINOR_VERSION = 10; + +async function askForConfirmation(question: string): Promise { + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + return new Promise((resolve) => { + rl.question(`${question} (y/n): `, (answer) => { + resolve(answer.toLowerCase() === 'y'); + rl.close(); + }); + }); } -// Add an option to force specific semantic bump -let [major, minor, patch] = currentVersion.split('.').map(Number); -patch++; -if (patch >= 10) { - // biome-ignore lint/correctness/noConstantCondition: TODO - ask if you want to bump minor version - if (true) { - patch = 0; - minor++; - if (minor >= 10) { - // biome-ignore lint/correctness/noConstantCondition: TODO - ask if you want to bump major - if (true) { - minor = 0; - major++; +async function bumpVersion(currentVersion: string, bumpType = 'auto'): Promise { + const [major, minor, patch] = currentVersion.split('.').map(Number); + let newPatch = patch + 1; + let newMinor = minor; + let newMajor = major; + + switch (bumpType) { + case 'patch': + // Increment patch version + console.log("Force Patch is not implemented yet...") + break; + case 'minor': + // Increment minor version and reset patch + console.log("Force Patch is not implemented yet...") + break; + case 'major': + // Increment major version and reset minor and patch + console.log("Force Patch is not implemented yet...") + break; + default: + if (patch >= MAX_PATCH_VERSION) { + console.log('Proposed Version:', `${newMajor}.${newMinor}.${newPatch}`); + const bumpMinorPrompt = `Do you want to bump the minor version from ${minor} to ${newMinor} (otherwise only the patch will be bumped to ${newPatch})?`; + const bumpMinor = await askForConfirmation(bumpMinorPrompt); + if (bumpMinor) { + newPatch = 0; + newMinor++; + if (minor >= MAX_MINOR_VERSION) { + console.log('Proposed Version:', `${newMajor}.${newMinor}.${newPatch}`); + const bumpMajorPrompt = `Do you want to bump the major version from ${major} to ${newMajor} (otherwise only the minor will be bumped to ${newMinor})?`; + const bumpMajor = await askForConfirmation(bumpMajorPrompt); + if (bumpMajor) { + newMinor = 0; + newMajor++; + } + } + } } - } + break; } + + return `${newMajor}.${newMinor}.${newPatch}`; } -const newVersion = `${major}.${minor}.${patch}`; -for (const filePath in bump_files) { + +async function main() { + let currentVersion: null | string = null; + try { - let content = fs.readFileSync(filePath, 'utf8'); - content = content.replace(bump_files[filePath].search.replace('currentVersion', currentVersion), bump_files[filePath].replace.replace('newVersion', newVersion)); - fs.writeFileSync(filePath, content); - console.log(`Updated version in ${filePath}`); + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); + currentVersion = packageJson.version; } catch (error) { if (error instanceof Error) { - console.error(`Error updating version in ${filePath}: ${error.message}`); + console.error(`Error reading package.json: ${error.message}`); } else { - console.error(`Unknown error updating version in ${filePath}: ${error}`); + console.error(`Unknown error reading package.json: ${error}`); } } + + if (currentVersion === null) { + console.error('Unable to retrieve current version'); + process.exit(1); + } + + // TODO: Add an option to force specific semantic bump + const newVersion = await bumpVersion(currentVersion); + console.log('New Version:', newVersion); + + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + rl.question('Enter a title for the update: ', (updateTitle) => { + rl.question('Enter a brief description of the changes: ', (mainDescription) => { + rl.question('Enter a secondary description of the changes: ', (secondaryDescription) => { + for (const filePath in bump_files) { + try { + let content = fs.readFileSync(filePath, 'utf8'); + const replacement = bump_files[filePath].replace + .replace('newVersion', newVersion) + .replace('updateTitle', updateTitle) + .replace('mainDescription', mainDescription) + .replace('secondaryDescription', secondaryDescription); + content = content.replace(bump_files[filePath].search.replace('currentVersion', currentVersion), replacement); + fs.writeFileSync(filePath, content); + console.log(`Updated version in ${filePath}`); + } catch (error) { + if (error instanceof Error) { + console.error(`Error updating version in ${filePath}: ${error.message}`); + } else { + console.error(`Unknown error updating version in ${filePath}: ${error}`); + } + } + } + console.log(`Updated version from v${currentVersion} to v${newVersion}`); + rl.close(); + }); + }); + }); } -console.log(`Updated version from v${currentVersion} to v${newVersion}`); \ No newline at end of file + +main(); \ No newline at end of file diff --git a/index.ts b/index.ts index 5849d1c..fc4ea30 100644 --- a/index.ts +++ b/index.ts @@ -22,7 +22,7 @@ const argv: string[] = process.argv.slice(2); // Parse command line arguments // Variables: let CleanProject = true; let projectName = 'example-project'; -const supeVersion = '1.3.0'; +const supeVersion = '1.3.1'; const supeVersionDate = '16/10/2024'; // Loop through each argument diff --git a/jsr.json b/jsr.json index 2037c85..c8f9dca 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@supeprojects/supe-project-creator", - "version": "1.3.0", + "version": "1.3.1", "exports": "./index.ts", "license": "MIT" } \ No newline at end of file diff --git a/package.json b/package.json index 9187004..3e09ad1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "supe-project-creator", - "version": "1.3.0", + "version": "1.3.1", "license": "MIT", "module": "index.ts", "type": "module",