Skip to content

Commit

Permalink
feat(cli): support signing git commits and tags (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekomeowww authored Oct 9, 2024
1 parent 24c49a5 commit 312cf50
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Forked from [`version-bump-prompt`](https://github.com/JS-DevTools/version-bump-
- Use the current version's `preid` when available.
- Confirmation before bumping.
- Enable `--commit` `--tag` `--push` by default. (opt-out by `--no-push`, etc.)
- `--sign` to sign the commit and tag.
- `-r` or `--recursive` to bump all packages in the monorepo.
- Conventional Commits by default.
- Supports config file `bump.config.ts`:
Expand Down
2 changes: 2 additions & 0 deletions src/cli/parse-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export async function parseArgs(): Promise<ParsedArgs> {
preid: args.preid,
commit: args.commit,
tag: args.tag,
sign: args.sign,
push: args.push,
all: args.all,
confirm: !args.yes,
Expand Down Expand Up @@ -78,6 +79,7 @@ export function loadCliArgs(argv = process.argv) {
.option('--no-commit', 'Skip commit', { default: false })
.option('-t, --tag [tag]', 'Tag name', { default: true })
.option('--no-tag', 'Skip tag', { default: false })
.option('--sign', 'Sign commit and tag')
.option('-p, --push', `Push to remote (default: ${bumpConfigDefaults.push})`)
.option('-y, --yes', `Skip confirmation (default: ${!bumpConfigDefaults.confirm})`)
.option('-r, --recursive', `Bump package.json files recursively (default: ${bumpConfigDefaults.recursive})`)
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const bumpConfigDefaults: VersionBumpOptions = {
commit: true,
push: true,
tag: true,
sign: false,
recursive: false,
noVerify: false,
confirm: true,
Expand All @@ -30,6 +31,7 @@ export async function loadBumpConfig(
},
cwd: configFile ? dirname(configFile) : cwd,
})
console.log(config)
return config!
}

Expand Down
9 changes: 9 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export async function gitCommit(operation: Operation): Promise<Operation> {
// Bypass git commit hooks
args.push('--no-verify')
}
// Sign the commit with a GPG/SSH key
if (operation.options.sign) {
args.push('--gpg-sign')
}

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

// Sign the tag with a GPG/SSH key
if (operation.options.sign) {
args.push('--sign')
}

await ezSpawn.async('git', ['tag', ...args])

return operation.update({ event: ProgressEvent.GitTag, tagName })
Expand Down
3 changes: 3 additions & 0 deletions src/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface NormalizedOptions {
tag?: {
name: string
}
sign?: boolean
push: boolean
files: string[]
cwd: string
Expand All @@ -72,6 +73,7 @@ export interface NormalizedOptions {
export async function normalizeOptions(raw: VersionBumpOptions): Promise<NormalizedOptions> {
// Set the simple properties first
const preid = typeof raw.preid === 'string' ? raw.preid : 'beta'
const sign = Boolean(raw.sign)
const push = Boolean(raw.push)
const all = Boolean(raw.all)
const noVerify = Boolean(raw.noVerify)
Expand Down Expand Up @@ -173,6 +175,7 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
release,
commit,
tag,
sign,
push,
files,
cwd,
Expand Down
7 changes: 7 additions & 0 deletions src/types/version-bump-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export interface VersionBumpOptions {
*/
tag?: boolean | string

/**
* Sign the git commit and tag with a configured key (GPG/SSH).
*
* Defaults to `false`.
*/
sign?: boolean

/**
* Indicates whether to push the git commit and tag.
*
Expand Down
6 changes: 6 additions & 0 deletions test/parse-args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ describe('loadCliArgs', async () => {

expect(result.args.commit).toBe(undefined)
})

it('should have sign property set to true if `--sign` is present', () => {
const result = loadCliArgs([...defaultArgs, '--sign'])

expect(result.args.sign).toBe(true)
})
})

0 comments on commit 312cf50

Please sign in to comment.