-
Notifications
You must be signed in to change notification settings - Fork 203
/
index.ts
105 lines (90 loc) · 2.73 KB
/
index.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
97
98
99
100
101
102
103
104
105
import {
Auto,
determineNextVersion,
execPromise,
IPlugin,
getCurrentBranch,
} from "@auto-it/core";
import { inc, ReleaseType } from "semver";
/** Manage your projects version through just a git tag. */
export default class GitTagPlugin implements IPlugin {
/** The name of the plugin */
name = "git-tag";
/** Tap into auto plugin points. */
apply(auto: Auto) {
/** Get the latest tag in the repo, if none then the first commit */
async function getTag() {
try {
return await auto.git!.getLatestTagInBranch();
} catch (error) {
return auto.prefixRelease("0.0.0");
}
}
auto.hooks.getPreviousVersion.tapPromise(this.name, async () => {
if (!auto.git) {
throw new Error(
"Can't calculate previous version without Git initialized!"
);
}
return getTag();
});
auto.hooks.version.tapPromise(this.name, async (version) => {
if (!auto.git) {
return;
}
const lastTag = await getTag();
const newTag = inc(lastTag, version as ReleaseType);
if (!newTag) {
auto.logger.log.info("No release found, doing nothing");
return;
}
const prefixedTag = auto.prefixRelease(newTag);
auto.logger.log.info(`Tagging new tag: ${lastTag} => ${prefixedTag}`);
await execPromise("git", [
"tag",
prefixedTag,
"-m",
`"Update version to ${prefixedTag}"`,
]);
});
auto.hooks.next.tapPromise(this.name, async (preReleaseVersions, bump) => {
if (!auto.git) {
return preReleaseVersions;
}
const prereleaseBranches = auto.config?.prereleaseBranches!;
const branch = getCurrentBranch() || "";
const prereleaseBranch = prereleaseBranches.includes(branch)
? branch
: prereleaseBranches[0];
const lastRelease = await auto.git.getLatestRelease();
const current =
(await auto.git.getLastTagNotInBaseBranch(prereleaseBranch)) ||
(await auto.getCurrentVersion(lastRelease));
const prerelease = determineNextVersion(
lastRelease,
current,
bump,
prereleaseBranch
);
await execPromise("git", [
"tag",
prerelease,
"-m",
`"Tag pre-release: ${prerelease}"`,
]);
await execPromise("git", ["push", auto.remote, "--tags"]);
preReleaseVersions.push(prerelease);
return preReleaseVersions;
});
auto.hooks.publish.tapPromise(this.name, async () => {
auto.logger.log.info("Pushing new tag to GitHub");
await execPromise("git", [
"push",
"--follow-tags",
"--set-upstream",
auto.remote,
getCurrentBranch() || auto.baseBranch,
]);
});
}
}