Skip to content

Commit

Permalink
feat: support customVersion from config
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 9, 2023
1 parent aa93f21 commit 86e2766
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/get-new-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,25 @@ async function promptForNewVersion(operation: Operation): Promise<Operation> {
const release = operation.options.release as PromptRelease

const next = getNextVersions(oldVersion, release.preid)
const configCustomVersion = await operation.options.customVersion?.(oldVersion, semver)

const PADDING = 13
const answers = await prompts([
{
type: 'autocomplete',
name: 'release',
message: `Current version ${c.green(oldVersion)}`,
initial: 'next',
initial: configCustomVersion ? 'config' : 'next',
choices: [
{ value: 'major', title: `${'major'.padStart(PADDING, ' ')} ${c.bold(next.major)}` },
{ value: 'minor', title: `${'minor'.padStart(PADDING, ' ')} ${c.bold(next.minor)}` },
{ value: 'patch', title: `${'patch'.padStart(PADDING, ' ')} ${c.bold(next.patch)}` },
{ value: 'next', title: `${'next'.padStart(PADDING, ' ')} ${c.bold(next.next)}` },
...configCustomVersion
? [
{ value: 'config', title: `${'from config'.padStart(PADDING, ' ')} ${c.bold(configCustomVersion)}` },
]
: [],
{ value: 'prepatch', title: `${'pre-patch'.padStart(PADDING, ' ')} ${c.bold(next.prepatch)}` },
{ value: 'preminor', title: `${'pre-minor'.padStart(PADDING, ' ')} ${c.bold(next.preminor)}` },
{ value: 'premajor', title: `${'pre-major'.padStart(PADDING, ' ')} ${c.bold(next.premajor)}` },
Expand All @@ -115,21 +121,24 @@ async function promptForNewVersion(operation: Operation): Promise<Operation> {
},
},
]) as {
release: ReleaseType | 'next' | 'none' | 'custom'
release: ReleaseType | 'next' | 'none' | 'custom' | 'config'
custom?: string
}

const newVersion = answers.release === 'none'
? oldVersion
: answers.release === 'custom'
? cleanVersion(answers.custom!)!
: next[answers.release]
: answers.release === 'config'
? cleanVersion(configCustomVersion!)
: next[answers.release]

if (!newVersion)
process.exit(1)

switch (answers.release) {
case 'custom':
case 'config':
case 'next':
case 'none':
return operation.update({ newVersion })
Expand Down
14 changes: 13 additions & 1 deletion src/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface NormalizedOptions {
interface: Interface
ignoreScripts: boolean
execute?: string
customVersion?: VersionBumpOptions['customVersion']
}

/**
Expand Down Expand Up @@ -133,5 +134,16 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
if (release.type === 'prompt' && !(ui.input && ui.output))
throw new Error('Cannot prompt for the version number because input or output has been disabled.')

return { release, commit, tag, push, files, cwd, interface: ui, ignoreScripts, execute }
return {
release,
commit,
tag,
push,
files,
cwd,
interface: ui,
ignoreScripts,
execute,
customVersion: raw.customVersion,
}
}
4 changes: 2 additions & 2 deletions src/types/version-bump-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SemVer } from 'semver'
import type _semver from 'semver'
import type { VersionBumpProgress } from './version-bump-progress'

/**
Expand Down Expand Up @@ -127,7 +127,7 @@ export interface VersionBumpOptions {
/**
* Custom function to provide the version number
*/
customVersion?: (currentVersion: string, semver: typeof SemVer) => Promise<string | number>
customVersion?: (currentVersion: string, semver: typeof _semver) => Promise<string | void> | string | void
}

/**
Expand Down

0 comments on commit 86e2766

Please sign in to comment.