Skip to content

Commit e05fac1

Browse files
authoredOct 28, 2024
feat: execute could receive a function (#54)
1 parent 14305e3 commit e05fac1

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed
 

‎README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Forked from [`version-bump-prompt`](https://github.com/JS-DevTools/version-bump-
88

99
- Renamed to `bumpp` - so you can use `npx bumpp` directly.
1010
- Ships ESM and CJS bundles.
11-
- Add a new argument `--execute` to execute the command before committing.
11+
- Add a new argument `--execute` to execute the command, or execute a function before committing.
1212
- Use the current version's `preid` when available.
1313
- Confirmation before bumping.
1414
- Enable `--commit` `--tag` `--push` by default. (opt-out by `--no-push`, etc.)
@@ -23,5 +23,8 @@ import { defineConfig } from 'bumpp'
2323

2424
export default defineConfig({
2525
// ...options
26+
execute(config) {
27+
// ...`execute` could receive a function here
28+
}
2629
})
2730
```

‎src/normalize-options.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import type { Operation } from './operation'
12
import type { ReleaseType } from './release-type'
23
import type { VersionBumpOptions } from './types/version-bump-options'
34
import fsSync from 'node:fs'
45
import fs from 'node:fs/promises'
56
import process from 'node:process'
6-
import { glob } from 'tinyglobby'
77
import yaml from 'js-yaml'
8+
import { glob } from 'tinyglobby'
89
import { isReleaseType } from './release-type'
910

1011
interface Interface {
@@ -61,7 +62,7 @@ export interface NormalizedOptions {
6162
cwd: string
6263
interface: Interface
6364
ignoreScripts: boolean
64-
execute?: string
65+
execute?: string | ((config?: Operation) => void | PromiseLike<void>)
6566
printCommits?: boolean
6667
customVersion?: VersionBumpOptions['customVersion']
6768
currentVersion?: string

‎src/types/version-bump-options.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type _semver from 'semver'
2+
import type { Operation } from '../operation'
23
import type { VersionBumpProgress } from './version-bump-progress'
34

45
/**
@@ -135,7 +136,7 @@ export interface VersionBumpOptions {
135136
/**
136137
* Excute additional command after bumping and before commiting
137138
*/
138-
execute?: string
139+
execute?: string | ((config?: Operation) => void | PromiseLike<void>)
139140

140141
/**
141142
* Bump the files recursively for monorepo. Only works without `files` option.

‎src/version-bump.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ export async function versionBump(arg: (VersionBumpOptions) | string = {}): Prom
7272
await updateFiles(operation)
7373

7474
if (operation.options.execute) {
75-
console.log(symbols.info, 'Executing script', operation.options.execute)
76-
await ezSpawn.async(operation.options.execute, { stdio: 'inherit' })
77-
console.log(symbols.success, 'Script finished')
75+
if (typeof operation.options.execute === 'function') {
76+
await operation.options.execute(operation)
77+
}
78+
else {
79+
console.log(symbols.info, 'Executing script', operation.options.execute)
80+
await ezSpawn.async(operation.options.execute, { stdio: 'inherit' })
81+
console.log(symbols.success, 'Script finished')
82+
}
7883
}
7984

8085
// Run npm version script, if any
@@ -101,7 +106,7 @@ function printSummary(operation: Operation) {
101106
if (operation.options.tag)
102107
console.log(` tag ${c.bold(formatVersionString(operation.options.tag.name, operation.state.newVersion))}`)
103108
if (operation.options.execute)
104-
console.log(` execute ${c.bold(operation.options.execute)}`)
109+
console.log(` execute ${c.bold(typeof operation.options.execute === 'function' ? 'function' : operation.options.execute)}`)
105110
if (operation.options.push)
106111
console.log(` push ${c.cyan(c.bold('yes'))}`)
107112
console.log()

0 commit comments

Comments
 (0)
Please sign in to comment.