Files
geo/apps/desktop-client/scripts/bump-version.cjs
T

52 lines
1.4 KiB
JavaScript
Raw Normal View History

const fs = require('node:fs')
const path = require('node:path')
const packageJsonPath = path.resolve(__dirname, '../package.json')
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
const currentVersion = packageJson.version
const requestedVersion = process.env.DESKTOP_VERSION
const bumpType = process.argv[2] || process.env.DESKTOP_VERSION_BUMP || 'patch'
function assertVersion(version) {
if (!/^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/.test(version)) {
throw new Error(`Invalid desktop version "${version}". Expected semver like 1.2.3.`)
}
}
function bump(version, type) {
const match = /^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/.exec(version)
if (!match) {
throw new Error(`Cannot bump invalid desktop version "${version}".`)
}
const major = Number(match[1])
const minor = Number(match[2])
const patch = Number(match[3])
if (type === 'major') {
return `${major + 1}.0.0`
}
if (type === 'minor') {
return `${major}.${minor + 1}.0`
}
if (type === 'patch') {
return `${major}.${minor}.${patch + 1}`
}
assertVersion(type)
return type
}
assertVersion(currentVersion)
const nextVersion = requestedVersion || bump(currentVersion, bumpType)
assertVersion(nextVersion)
packageJson.version = nextVersion
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`)
console.log(`Desktop version bumped: ${currentVersion} -> ${nextVersion}`)