Skip to content
This repository has been archived by the owner on Oct 26, 2024. It is now read-only.

Commit

Permalink
Improved Version Bumper
Browse files Browse the repository at this point in the history
  • Loading branch information
burgil committed Oct 16, 2024
1 parent 0ea8795 commit f7877d4
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 42 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
145 changes: 107 additions & 38 deletions bump.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs';
import * as readline from 'node:readline';

const bump_files: {
[key: string]: {
Expand All @@ -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<boolean> {
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<string> {
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}`);

main();
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@supeprojects/supe-project-creator",
"version": "1.3.0",
"version": "1.3.1",
"exports": "./index.ts",
"license": "MIT"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "supe-project-creator",
"version": "1.3.0",
"version": "1.3.1",
"license": "MIT",
"module": "index.ts",
"type": "module",
Expand Down

0 comments on commit f7877d4

Please sign in to comment.