github-desktop/script/draft-release/version.ts

102 lines
2.6 KiB
TypeScript
Raw Normal View History

2018-02-09 04:08:15 +00:00
import { inc, parse, SemVer } from 'semver'
2018-02-04 11:17:04 +00:00
import { Channel } from './channel'
2018-02-09 04:08:15 +00:00
function isBetaTag(version: SemVer) {
return version.prerelease.some(p => p.startsWith('beta'))
}
function isTestTag(version: SemVer) {
return version.prerelease.some(p => p.startsWith('test'))
}
function tryGetBetaNumber(version: SemVer): number | null {
if (isBetaTag(version)) {
const tag = version.prerelease[0]
const text = tag.substring(4)
const betaNumber = parseInt(text, 10)
return isNaN(betaNumber) ? null : betaNumber
}
return null
}
2018-02-04 01:15:29 +00:00
export function getNextVersionNumber(
version: string,
2018-02-04 11:17:04 +00:00
channel: Channel
2018-02-04 01:15:29 +00:00
): string {
2018-02-09 03:30:37 +00:00
const semanticVersion = parse(version)
2018-02-09 03:30:37 +00:00
if (semanticVersion == null) {
throw new Error(`Unable to parse input '${version}' into version`)
}
2018-02-09 04:11:20 +00:00
switch (channel) {
case 'production':
if (isBetaTag(semanticVersion)) {
throw new Error(
`Unable to draft production release using beta version '${version}'`
)
}
2018-02-09 04:11:20 +00:00
if (isTestTag(semanticVersion)) {
throw new Error(
`Unable to draft production release using test version '${version}'`
)
}
2018-02-09 04:11:20 +00:00
const nextVersion = inc(version, 'patch')
if (nextVersion == null) {
throw new Error(
`Unable to increment next production version from release version '${version}'`
)
}
2018-02-09 04:11:20 +00:00
return nextVersion
2018-02-09 04:11:20 +00:00
case 'beta':
if (isTestTag(semanticVersion)) {
throw new Error(
2018-02-09 04:18:50 +00:00
`Unable to draft beta release using test version '${version}'`
2018-02-09 04:11:20 +00:00
)
}
const betaNumber = tryGetBetaNumber(semanticVersion)
if (betaNumber) {
return semanticVersion.version.replace(
2018-02-09 04:11:20 +00:00
`-beta${betaNumber}`,
`-beta${betaNumber + 1}`
2018-02-09 04:11:20 +00:00
)
} else {
const nextVersion = inc(semanticVersion, 'patch')
2018-02-09 04:11:20 +00:00
const firstBeta = `${nextVersion}-beta1`
return firstBeta
}
2022-05-03 13:20:44 +00:00
case 'test':
if (isBetaTag(semanticVersion)) {
throw new Error(
`Unable to draft test release using beta version '${version}'`
)
}
2018-02-04 01:15:29 +00:00
2022-05-03 13:20:44 +00:00
if (isTestTag(semanticVersion)) {
const tag = semanticVersion.prerelease[0]
const text = tag.substring(4)
const testNumber = parseInt(text, 10)
return semanticVersion.version.replace(
`-test${testNumber}`,
`-test${testNumber + 1}`
)
} else {
const nextVersion = inc(semanticVersion, 'patch')
const firstTest = `${nextVersion}-test1`
return firstTest
}
2018-02-09 04:11:20 +00:00
default:
throw new Error(
`Resolving the next version is not implemented for channel ${channel}`
)
}
}