diff --git a/package.json b/package.json index cdf2241..45dea5f 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "test": "jest", "lint": "biome lint --error-on-warnings ./src", "format": "biome format --write ./src", + "update-version": "node scripts/update-version.js", "prepublishOnly": "npm run build" }, "devDependencies": { @@ -55,6 +56,6 @@ "node": ">=18" }, "volta": { - "node": "20.12.2" + "node": "20.16.0" } } diff --git a/scripts/update-version.js b/scripts/update-version.js new file mode 100644 index 0000000..6856fc1 --- /dev/null +++ b/scripts/update-version.js @@ -0,0 +1,78 @@ +#!/usr/bin/env node + +const fs = require('node:fs') +const path = require('node:path') + +const packageJsonPath = path.join(__dirname, '..', 'package.json') +const blutuiPath = path.join(__dirname, '..', 'src', 'blutui.ts') + +// Get the current version from package.json +function getPackageJsonVersion() { + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) + return packageJson.version +} + +// Bump the version based on the given version type +function bumpVersion(currentVersion, type) { + const versionParts = currentVersion.split('.').map(Number) + + switch (type) { + case '--major': + versionParts[0]++ + versionParts[1] = 0 + versionParts[2] = 0 + break + case '--minor': + versionParts[1]++ + versionParts[2] = 0 + break + case '--patch': + versionParts[2]++ + break + default: + throw new Error('Invalid argument. Use --major, --minor, or --patch') + } + + return versionParts.join('.') +} + +// Update the package.json version +function updatePackageJsonVersion(newVersion) { + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) + packageJson.version = newVersion + fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`) +} + +// Update VERSION constant in blutui.ts +function updateBlutuiVersion(newVersion) { + let blutuiContent = fs.readFileSync(blutuiPath, 'utf-8') + const versionRegex = /const VERSION = '([0-9]+\.[0-9]+\.[0-9]+)'/ + + blutuiContent = blutuiContent.replace( + versionRegex, + `const VERSION = '${newVersion}'` + ) + + fs.writeFileSync(blutuiPath, blutuiContent) +} + +// The main function +function main() { + const args = process.argv.slice(2) + if (args.length !== 1) { + console.error( + 'Please provide exactly one argument: --major, --minor, or --patch' + ) + process.exit(1) + } + + const currentVersion = getPackageJsonVersion() + const newVersion = bumpVersion(currentVersion, args[0]) + + updatePackageJsonVersion(newVersion) + updateBlutuiVersion(newVersion) + + console.log(`Version updated from ${$currentVersion} to ${newVersion}`) +} + +main()