Skip to content

Commit de758bc

Browse files
nexdrewbcoe
authored andcommitted
feat: add --sign flag to sign git commit and tag (#29)
* feat(cli): add tag signing option (--sign-tag and its alias -s) By default, a tag is not signed (git tag -a). The option `--sign-tag` enables tag signing (git tag -s). * feat: add --sign flag to sign git commit and tag
1 parent 3f51e94 commit de758bc

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ var argv = require('yargs')
2424
default: false,
2525
global: true
2626
})
27+
.option('sign', {
28+
alias: 's',
29+
describe: 'Should the git commit and tag be signed?',
30+
type: 'boolean',
31+
default: false,
32+
global: true
33+
})
2734
.help()
2835
.alias('help', 'h')
2936
.example('$0', 'Update changelog and tag release')
@@ -106,7 +113,7 @@ function commit (argv, newVersion, cb) {
106113
args.unshift('package.json')
107114
}
108115
checkpoint(msg, args)
109-
exec('git add package.json ' + argv.infile + ';git commit package.json ' + argv.infile + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) {
116+
exec('git add package.json ' + argv.infile + ';git commit ' + (argv.sign ? '-S ' : '') + 'package.json ' + argv.infile + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) {
110117
var errMessage = null
111118
if (err) errMessage = err.message
112119
if (stderr) errMessage = stderr
@@ -123,8 +130,14 @@ function formatCommitMessage (msg, newVersion) {
123130
}
124131

125132
function tag (newVersion, argv) {
133+
var tagOption
134+
if (argv.sign) {
135+
tagOption = '-s '
136+
} else {
137+
tagOption = '-a '
138+
}
126139
checkpoint('tagging release %s', [newVersion])
127-
exec('git tag -a v' + newVersion + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) {
140+
exec('git tag ' + tagOption + 'v' + newVersion + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) {
128141
var errMessage = null
129142
if (err) errMessage = err.message
130143
if (stderr) errMessage = stderr

test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ describe('cli', function () {
7979
})
8080
})
8181

82+
it('respects the --sign option', function () {
83+
fs.writeFileSync('package.json', JSON.stringify({
84+
version: '1.0.0'
85+
}), 'utf-8')
86+
87+
commit('feat: first commit')
88+
89+
// this should fail without a GPG key
90+
var result = shell.exec(cliPath + ' --sign')
91+
result.code.should.equal(1)
92+
result.stdout.should.match(/gpg\: signing failed\: secret key not available/)
93+
result.stdout.should.match(/error\: gpg failed to sign the data/)
94+
})
95+
8296
it('handles commit messages longer than 80 characters', function () {
8397
fs.writeFileSync('package.json', JSON.stringify({
8498
version: '1.0.0'

0 commit comments

Comments
 (0)