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

58 lines
1.4 KiB
TypeScript
Raw Normal View History

import { inc, parse } from 'semver'
2018-02-04 11:17:04 +00:00
import { Channel } from './channel'
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 {
const parsed = parse(version)
if (parsed == null) {
throw new Error(`Unable to parse input '${version}' into version`)
}
if (channel === 'production') {
if (parsed.prerelease.some(p => p.startsWith('beta'))) {
throw new Error(
`Unable to resolve production version using beta release '${version}'`
)
}
if (parsed.prerelease.some(p => p.startsWith('test'))) {
throw new Error(
`Unable to resolve production version using test release '${version}'`
)
}
const nextVersion = inc(version, 'patch')
if (nextVersion == null) {
throw new Error(
`Unable to resolve next version from release '${version}'`
)
}
return nextVersion
2018-02-04 01:15:29 +00:00
}
2018-02-04 01:17:05 +00:00
if (parsed.prerelease.some(p => p.startsWith('test'))) {
2018-02-04 01:15:29 +00:00
throw new Error(
`Unable to resolve production version using test release '${version}'`
)
}
2018-02-04 01:15:29 +00:00
const betaTagIndex = version.indexOf('-beta')
if (betaTagIndex > -1) {
const betaNumber = version.substr(betaTagIndex + 5)
const newBeta = parseInt(betaNumber, 10) + 1
const newVersion = version.replace(`-beta${betaNumber}`, `-beta${newBeta}`)
return newVersion
} else {
2018-02-04 01:15:29 +00:00
const nextVersion = inc(version, 'patch')
const firstBeta = `${nextVersion}-beta1`
return firstBeta
}
}