From df238921f77425c29f5114a7f8beb5c38deb161d Mon Sep 17 00:00:00 2001 From: a145789 <2091927351@qq.com> Date: Fri, 29 Mar 2024 14:56:17 +0800 Subject: [PATCH] refactor: optimize the logic for version validation --- src/release.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/release.ts b/src/release.ts index 3aa0e57..e1ec5d4 100644 --- a/src/release.ts +++ b/src/release.ts @@ -145,7 +145,7 @@ async function confirmRefs(remote = 'origin') { return ret[name] } -async function getReleaseType(): Promise { +async function getReleaseType() { const name = 'Please select release type' const ret = await prompt([ { @@ -155,7 +155,22 @@ async function getReleaseType(): Promise { }, ]) - 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 { @@ -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)