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