Skip to content

Commit

Permalink
feat: prompt before bumpping
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 15, 2022
1 parent 07ca34d commit 75b62fe
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -10,6 +10,7 @@ Forked from [`version-bump-prompt`](https://github.com/JS-DevTools/version-bump-
- Ships ESM and CJS bundles.
- Add a new argument `--execute` to execute the command before committing.
- Use current version's `preid` when avaliable.
- Confirmation before bumping.
- Enable `--commit` `--tag` `--push` by default. (opt-out by `--no-push`, etc.)

<details>
Expand Down
2 changes: 2 additions & 0 deletions src/cli/parse-args.ts
Expand Up @@ -31,6 +31,7 @@ export function parseArgs(argv: string[]): ParsedArgs {
.option('-c, --commit [msg]', 'Commit message', { default: true })
.option('-t, --tag [tag]', 'Tag name', { default: true })
.option('-p, --push', 'Push to remote', { default: true })
.option('-y, --yes', 'Skip confirmation')
.option('--no-verify', 'Skip git verification')
.option('--ignore-scripts', 'Ignore scripts', { default: false })
.option('-q, --quiet', 'Quiet mode')
Expand All @@ -50,6 +51,7 @@ export function parseArgs(argv: string[]): ParsedArgs {
tag: args.tag,
push: args.push,
all: args.all,
confirm: !args.yes,
noVerify: !args.verify,
files: args['--'],
ignoreScripts: args.ignoreScripts,
Expand Down
17 changes: 9 additions & 8 deletions src/get-new-version.ts
Expand Up @@ -85,20 +85,21 @@ async function promptForNewVersion(operation: Operation): Promise<Operation> {

const next = getNextVersions(oldVersion, release.preid)

const PADDING = 13
const answers = await prompts([
{
type: 'autocomplete',
name: 'release',
message: `Current version: ${green(oldVersion)}`,
message: `Current version ${green(oldVersion)}`,
initial: 'next',
choices: [
{ value: 'major', title: `major - ${bold(next.major)}` },
{ value: 'minor', title: `minor - ${bold(next.minor)}` },
{ value: 'patch', title: `patch - ${bold(next.patch)}` },
{ value: 'next', title: `next - ${bold(next.next)}` },
{ value: 'prerelease', title: `pre-release - ${bold(next.prerelease)}` },
{ value: 'none', title: `as-is - ${bold(oldVersion)}` },
{ value: 'custom', title: 'custom...' },
{ value: 'major', title: `${'major'.padStart(PADDING, ' ')} ${bold(next.major)}` },
{ value: 'minor', title: `${'minor'.padStart(PADDING, ' ')} ${bold(next.minor)}` },
{ value: 'patch', title: `${'patch'.padStart(PADDING, ' ')} ${bold(next.patch)}` },
{ value: 'next', title: `${'next'.padStart(PADDING, ' ')} ${bold(next.next)}` },
{ value: 'prerelease', title: `${'pre-release'.padStart(PADDING, ' ')} ${bold(next.prerelease)}` },
{ value: 'none', title: `${'as-is'.padStart(PADDING, ' ')} ${bold(oldVersion)}` },
{ value: 'custom', title: 'custom ...'.padStart(PADDING + 4, ' ') },
],
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/git.ts
Expand Up @@ -87,7 +87,7 @@ export async function gitPush(operation: Operation): Promise<Operation> {
* If the template contains any "%s" placeholders, then they are replaced with the version number;
* otherwise, the version number is appended to the string.
*/
function formatVersionString(template: string, newVersion: string): string {
export function formatVersionString(template: string, newVersion: string): string {
if (template.includes('%s'))
return template.replace(/%s/g, newVersion)

Expand Down
7 changes: 7 additions & 0 deletions src/types/version-bump-options.ts
Expand Up @@ -57,6 +57,13 @@ export interface VersionBumpOptions {
*/
all?: boolean

/**
* Prompt for confirmation
*
* @default false
*/
confirm?: boolean

/**
* Indicates whether to bypass git commit hooks (`git commit --no-verify`).
*
Expand Down
30 changes: 28 additions & 2 deletions src/version-bump.ts
@@ -1,8 +1,10 @@
import * as ezSpawn from '@jsdevtools/ez-spawn'
import { bold, cyan, green } from 'chalk'
import { info, success } from 'log-symbols'
import prompts from 'prompts'
import { getNewVersion } from './get-new-version'
import { getOldVersion } from './get-old-version'
import { gitCommit, gitPush, gitTag } from './git'
import { formatVersionString, gitCommit, gitPush, gitTag } from './git'
import { Operation } from './operation'
import { runNpmScript } from './run-npm-script'
import type { VersionBumpOptions } from './types/version-bump-options'
Expand Down Expand Up @@ -39,7 +41,7 @@ export async function versionBump(options: VersionBumpOptions): Promise<VersionB
* Bumps the version number in one or more files, prompting the user if necessary.
* Optionally also commits, tags, and pushes to git.
*/
export async function versionBump(arg: VersionBumpOptions | string = {}): Promise<VersionBumpResults> {
export async function versionBump(arg: VersionBumpOptions | string = {}): Promise<VersionBumpResults | undefined> {
if (typeof arg === 'string')
arg = { release: arg }

Expand All @@ -49,6 +51,30 @@ export async function versionBump(arg: VersionBumpOptions | string = {}): Promis
await getOldVersion(operation)
await getNewVersion(operation)

if (arg.confirm) {
console.log()
console.log(` files ${operation.options.files.map(i => bold(i)).join(', ')}`)
if (operation.options.commit)
console.log(` commit ${bold(formatVersionString(operation.options.commit.message, operation.state.newVersion))}`)
if (operation.options.tag)
console.log(` tag ${bold(formatVersionString(operation.options.tag.name, operation.state.newVersion))}`)
if (operation.options.execute)
console.log(` execute ${bold(operation.options.execute)}`)
if (operation.options.push)
console.log(` push ${cyan(bold('yes'))}`)
console.log()
console.log(` from ${bold(operation.state.oldVersion)}`)
console.log(` to ${green(bold(operation.state.newVersion))}`)
console.log()

if (!await prompts({
name: 'yes',
type: 'confirm',
message: 'Bump',
}).then(r => r.yes))
return
}

// Run npm preversion script, if any
await runNpmScript(NpmScript.PreVersion, operation)

Expand Down

0 comments on commit 75b62fe

Please sign in to comment.