Skip to content

Commit

Permalink
refactor: optimize the logic for version validation
Browse files Browse the repository at this point in the history
  • Loading branch information
a145789 committed Mar 29, 2024
1 parent 319a7e9 commit df23892
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async function confirmRefs(remote = 'origin') {
return ret[name]
}

async function getReleaseType(): Promise<ReleaseType> {
async function getReleaseType() {
const name = 'Please select release type'
const ret = await prompt([
{
Expand All @@ -155,7 +155,22 @@ async function getReleaseType(): Promise<ReleaseType> {
},
])

return ret[name]
return ret[name] as ReleaseType
}
async function getReleaseVersion(currentVersion: string) {
let isPreRelease = false
let expectVersion = ''
let confirmVersionRet = ''
do {
const type = await getReleaseType()
isPreRelease = type.startsWith('pre')
expectVersion = semver.inc(currentVersion, type, `alpha.${Date.now()}`) as string
expectVersion = isPreRelease ? expectVersion.slice(0, -2) : expectVersion

confirmVersionRet = await confirmVersion(currentVersion, expectVersion)
} while (confirmVersionRet === 'back')

return { isPreRelease, expectVersion }
}

export interface ReleaseCommandOptions {
Expand Down Expand Up @@ -189,17 +204,7 @@ export async function release(options: ReleaseCommandOptions) {
return
}

let confirmVersionRet = 'back'
let isPreRelease = false
let expectVersion = ''
while (confirmVersionRet === 'back') {
const type = await getReleaseType()
isPreRelease = type.startsWith('pre')
expectVersion = semver.inc(currentVersion, type, `alpha.${Date.now()}`) as string
expectVersion = isPreRelease ? expectVersion.slice(0, -2) : expectVersion

confirmVersionRet = await confirmVersion(currentVersion, expectVersion)
}
const { isPreRelease, expectVersion } = await getReleaseVersion(currentVersion)

updateVersion(expectVersion)

Expand Down

0 comments on commit df23892

Please sign in to comment.