forked from JS-DevTools/version-bump-prompt
/
git.ts
96 lines (75 loc) · 2.65 KB
/
git.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as ezSpawn from '@jsdevtools/ez-spawn'
import type { Operation } from './operation'
import { ProgressEvent } from './types/version-bump-progress'
/**
* Commits the modififed files to Git, if the `commit` option is enabled.
*/
export async function gitCommit(operation: Operation): Promise<Operation> {
if (!operation.options.commit)
return operation
const { all, noVerify, message } = operation.options.commit
const { updatedFiles, newVersion } = operation.state
let args = []
if (all) {
// Commit ALL files, not just the ones that were bumped
args.push('--all')
}
if (noVerify) {
// Bypass git commit hooks
args.push('--no-verify')
}
// Create the commit message
const commitMessage = formatVersionString(message, newVersion)
args.push('--message', commitMessage)
// Append the file names last, as variadic arguments
if (!all)
args = args.concat(updatedFiles)
await ezSpawn.async('git', ['commit', ...args])
return operation.update({ event: ProgressEvent.GitCommit, commitMessage })
}
/**
* Tags the Git commit, if the `tag` option is enabled.
*/
export async function gitTag(operation: Operation): Promise<Operation> {
if (!operation.options.tag)
return operation
const { commit, tag } = operation.options
const { newVersion } = operation.state
const args = [
// Create an annotated tag, which is recommended for releases.
// See https://git-scm.com/docs/git-tag
'--annotate',
// Use the same commit message for the tag
'--message', formatVersionString(commit!.message, newVersion),
]
// Create the Tag name
const tagName = formatVersionString(tag.name, newVersion)
args.push(tagName)
await ezSpawn.async('git', ['tag', ...args])
return operation.update({ event: ProgressEvent.GitTag, tagName })
}
/**
* Pushes the Git commit and tag, if the `push` option is enabled.
*/
export async function gitPush(operation: Operation): Promise<Operation> {
if (!operation.options.push)
return operation
// Push the commit
await ezSpawn.async('git', 'push')
if (operation.options.tag) {
// Push the tag
await ezSpawn.async('git', ['push', '--tags'])
}
return operation.update({ event: ProgressEvent.GitPush })
}
/**
* Accepts a version string template (e.g. "release v" or "This is the %s release").
* If the template contains any "%s" placeholders, then they are replaced with the version number;
* otherwise, the version number is appended to the string.
*/
export function formatVersionString(template: string, newVersion: string): string {
if (template.includes('%s'))
return template.replace(/%s/g, newVersion)
else
return template + newVersion
}