Skip to content

Commit

Permalink
refactor: optimize process
Browse files Browse the repository at this point in the history
  • Loading branch information
a145789 committed Nov 15, 2023
1 parent ce9a362 commit 4bebc2c
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const cwd = process.cwd()
const { writeFileSync, readJSONSync } = fse
const { prompt } = inquirer

const releaseTypes = ['premajor', 'preminor', 'prepatch', 'major', 'minor', 'patch']
const releaseTypes = ['prerelease', 'premajor', 'preminor', 'prepatch', 'major', 'minor', 'patch'] as const

async function isWorktreeEmpty() {
const ret = await execa('git', ['status', '--porcelain'])
Expand Down Expand Up @@ -106,17 +106,23 @@ async function confirmRefs(remote = 'origin') {
return ret[name]
}

async function getReleaseType() {
async function getExpectVersion(currentVersion: string) {
const name = 'Please select release type'
const choices = releaseTypes.map((type) => `${type}(${semver.inc(currentVersion, type, 'alpha')})`)
const ret = await prompt([
{
name,
type: 'list',
choices: releaseTypes,
choices,
},
])

return ret[name]
const type = ret[name] as string

return {
isPreRelease: type.startsWith('pre'),
expectVersion: type.match(/\((.*?)\)/)![1],
}
}

export interface ReleaseCommandOptions {
Expand All @@ -133,6 +139,8 @@ export async function release(options: ReleaseCommandOptions) {
return
}

logger.start(`current version: ${currentVersion}`)

if (!(await isWorktreeEmpty())) {
logger.error('Git worktree is not empty, please commit changed')
return
Expand All @@ -146,10 +154,7 @@ export async function release(options: ReleaseCommandOptions) {
return
}

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

if (!(await confirmVersion(currentVersion, expectVersion))) {
return
Expand Down

0 comments on commit 4bebc2c

Please sign in to comment.