Skip to content

Commit 312cf50

Browse files
authored
feat(cli): support signing git commits and tags (#47)
1 parent 24c49a5 commit 312cf50

File tree

7 files changed

+30
-0
lines changed

7 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Forked from [`version-bump-prompt`](https://github.com/JS-DevTools/version-bump-
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.)
15+
- `--sign` to sign the commit and tag.
1516
- `-r` or `--recursive` to bump all packages in the monorepo.
1617
- Conventional Commits by default.
1718
- Supports config file `bump.config.ts`:

src/cli/parse-args.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export async function parseArgs(): Promise<ParsedArgs> {
3333
preid: args.preid,
3434
commit: args.commit,
3535
tag: args.tag,
36+
sign: args.sign,
3637
push: args.push,
3738
all: args.all,
3839
confirm: !args.yes,
@@ -78,6 +79,7 @@ export function loadCliArgs(argv = process.argv) {
7879
.option('--no-commit', 'Skip commit', { default: false })
7980
.option('-t, --tag [tag]', 'Tag name', { default: true })
8081
.option('--no-tag', 'Skip tag', { default: false })
82+
.option('--sign', 'Sign commit and tag')
8183
.option('-p, --push', `Push to remote (default: ${bumpConfigDefaults.push})`)
8284
.option('-y, --yes', `Skip confirmation (default: ${!bumpConfigDefaults.confirm})`)
8385
.option('-r, --recursive', `Bump package.json files recursively (default: ${bumpConfigDefaults.recursive})`)

src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const bumpConfigDefaults: VersionBumpOptions = {
88
commit: true,
99
push: true,
1010
tag: true,
11+
sign: false,
1112
recursive: false,
1213
noVerify: false,
1314
confirm: true,
@@ -30,6 +31,7 @@ export async function loadBumpConfig(
3031
},
3132
cwd: configFile ? dirname(configFile) : cwd,
3233
})
34+
console.log(config)
3335
return config!
3436
}
3537

src/git.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export async function gitCommit(operation: Operation): Promise<Operation> {
2222
// Bypass git commit hooks
2323
args.push('--no-verify')
2424
}
25+
// Sign the commit with a GPG/SSH key
26+
if (operation.options.sign) {
27+
args.push('--gpg-sign')
28+
}
2529

2630
// Create the commit message
2731
const commitMessage = formatVersionString(message, newVersion)
@@ -60,6 +64,11 @@ export async function gitTag(operation: Operation): Promise<Operation> {
6064
const tagName = formatVersionString(tag.name, newVersion)
6165
args.push(tagName)
6266

67+
// Sign the tag with a GPG/SSH key
68+
if (operation.options.sign) {
69+
args.push('--sign')
70+
}
71+
6372
await ezSpawn.async('git', ['tag', ...args])
6473

6574
return operation.update({ event: ProgressEvent.GitTag, tagName })

src/normalize-options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface NormalizedOptions {
5555
tag?: {
5656
name: string
5757
}
58+
sign?: boolean
5859
push: boolean
5960
files: string[]
6061
cwd: string
@@ -72,6 +73,7 @@ export interface NormalizedOptions {
7273
export async function normalizeOptions(raw: VersionBumpOptions): Promise<NormalizedOptions> {
7374
// Set the simple properties first
7475
const preid = typeof raw.preid === 'string' ? raw.preid : 'beta'
76+
const sign = Boolean(raw.sign)
7577
const push = Boolean(raw.push)
7678
const all = Boolean(raw.all)
7779
const noVerify = Boolean(raw.noVerify)
@@ -173,6 +175,7 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
173175
release,
174176
commit,
175177
tag,
178+
sign,
176179
push,
177180
files,
178181
cwd,

src/types/version-bump-options.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ export interface VersionBumpOptions {
4949
*/
5050
tag?: boolean | string
5151

52+
/**
53+
* Sign the git commit and tag with a configured key (GPG/SSH).
54+
*
55+
* Defaults to `false`.
56+
*/
57+
sign?: boolean
58+
5259
/**
5360
* Indicates whether to push the git commit and tag.
5461
*

test/parse-args.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,10 @@ describe('loadCliArgs', async () => {
5959

6060
expect(result.args.commit).toBe(undefined)
6161
})
62+
63+
it('should have sign property set to true if `--sign` is present', () => {
64+
const result = loadCliArgs([...defaultArgs, '--sign'])
65+
66+
expect(result.args.sign).toBe(true)
67+
})
6268
})

0 commit comments

Comments
 (0)