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

75 lines
1.9 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'))
}
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(
`Unable to resolve beta release using test version '${version}'`
)
}
const betaTagIndex = version.indexOf('-beta')
if (betaTagIndex > -1) {
const betaNumber = version.substr(betaTagIndex + 5)
const newBeta = parseInt(betaNumber, 10) + 1
2018-02-09 04:11:20 +00:00
const newVersion = version.replace(
`-beta${betaNumber}`,
`-beta${newBeta}`
)
return newVersion
} else {
const nextVersion = inc(version, 'patch')
const firstBeta = `${nextVersion}-beta1`
return firstBeta
}
2018-02-04 01:15:29 +00:00
2018-02-09 04:11:20 +00:00
default:
throw new Error(
`Resolving the next version is not implemented for channel ${channel}`
)
}
}