-
Notifications
You must be signed in to change notification settings - Fork 2
/
git.ts
52 lines (41 loc) · 1.21 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
import UserError from "./user-error.ts";
export class GitError extends Error {
constructor(message: string) {
super(message);
this.name = "GitError";
}
}
async function runCommand(...args: string[]): Promise<string> {
const cmd = Deno.run({
cmd: ["git", ...args],
stdout: "piped",
stderr: "piped",
});
const stdout = await cmd.output();
const stdoutString = new TextDecoder().decode(stdout);
const stderr = await cmd.stderrOutput();
const stderrString = new TextDecoder().decode(stderr);
const { code } = await cmd.status();
cmd.close();
if (code !== 0) {
throw new GitError(
`Failed to run \`git ${args.join(" ")}\`: ${stderrString}`,
);
}
return stdoutString;
}
export async function checkPrerequisites(): Promise<void> {
const status = await runCommand("status", "--porcelain");
if (status !== "") {
throw new UserError("Cannot release with uncommitted changes");
}
}
export async function commitAndTag(
normalizedVersion: string,
fileName: string,
) {
const tagName = `v${normalizedVersion}`;
await runCommand("add", fileName);
await runCommand("commit", "-m", normalizedVersion);
await runCommand("tag", tagName, "-m", normalizedVersion);
}