-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
67 lines (61 loc) · 1.7 KB
/
util.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
import git from "isomorphic-git";
import fs from "fs";
import { join } from "path";
import { spawn } from "child_process";
import { compare, SemVer, valid } from "semver";
export const getLernaChangedPackages: () => Promise<Array<string>> = () =>
new Promise((resolve) => {
const cmd = spawn("node", [
join(process.cwd(), "node_modules", ".bin", "lerna"),
"changed",
"--all",
"--parseable",
"--long",
]);
cmd.stdout.on("data", (buf) =>
resolve(
buf
.toString()
.split("\n")
.filter((str: string) => !!str)
.map((str: string) => {
const [, pkg] = str.split(":");
return pkg;
})
)
);
});
export const getLastTag: () => Promise<SemVer | null> = async () => {
const tags = (
await git.listTags({
fs,
dir: process.cwd(),
})
).reduce((acc, curr) => {
const s = new SemVer(curr, { includePrerelease: true });
if (valid(s)) {
return [...acc, s].sort((v1, v2) => compare(v2, v1));
} else {
return acc;
}
}, [] as Array<SemVer>);
return tags[0] ?? null;
};
export const produceNextTag: (
prefix: string,
prerelease?: string
) => Promise<string> = async (prefix: string, prerelease?: string) => {
const d = new Date();
const major = d.getFullYear();
const minor = d.getMonth() + 1;
const patch = d.getDate();
const baseTag = new SemVer(`${major}.${minor}.${patch}-${prefix}.${prerelease ?? "0"}`, {
includePrerelease: true,
});
const lastTag = await getLastTag();
if (!lastTag || prerelease || compare(baseTag, lastTag) > 0) {
return baseTag.version;
} else {
return lastTag.inc("prerelease").version;
}
};