-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbranch.ts
More file actions
114 lines (99 loc) · 3.32 KB
/
branch.ts
File metadata and controls
114 lines (99 loc) · 3.32 KB
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
106
107
108
109
110
111
112
113
114
import { Command } from "@cliffy/command";
import {
branchNameToBranch,
deleteBranch as deleteValbranch,
listBranches as listValBranches,
} from "~/sdk.ts";
import { colors } from "@cliffy/ansi/colors";
import { Table } from "@cliffy/table";
import { doWithSpinner } from "~/cmd/utils.ts";
import VTClient from "~/vt/vt/VTClient.ts";
import { findVtRoot } from "~/vt/vt/utils.ts";
async function listBranches(vt: VTClient) {
return await doWithSpinner("Loading branches...", async (spinner) => {
const vtState = await vt.getMeta().loadVtState();
const branches = await listValBranches(vtState.val.id);
const formatter = new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "short",
day: "2-digit",
});
// Separate current branch, place it at the top, and then sort the rest
// by update time
const currentBranch = branches
.find((branch) => branch.id === vtState.branch.id);
const otherBranches = branches
.filter((branch) => branch.id !== vtState.branch.id)
.sort((a, b) =>
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()
);
const sortedBranches = currentBranch
? [currentBranch, ...otherBranches]
: otherBranches;
// Stop the spinner before printing out the result
spinner.stop();
const branchesTableList = Table.from([
[
colors.bold("Name"),
colors.bold("Version"),
colors.bold("Created On"),
colors.bold("Updated On"),
],
...sortedBranches.map(
(branch) => [
branch.id === vtState.branch.id
? colors.green(`* ${branch.name}`)
: branch.name,
colors.cyan(branch.version.toString()),
colors.yellow(formatter.format(new Date(branch.createdAt))),
colors.magenta(formatter.format(new Date(branch.updatedAt))),
],
),
]);
console.log(branchesTableList.toString());
// A helpful FYI to let the user know that their current state is going to
// cause them issues
if (!sortedBranches.some((branch) => branch.id === vtState.branch.id)) {
console.log();
console.log(
colors
.yellow(
"Note that the current branch no longer exists. " +
"You will have to check out to a branch that exists.",
),
);
}
});
}
async function deleteBranch(vt: VTClient, toDeleteName: string) {
const meta = await vt.getMeta().loadVtState();
await doWithSpinner("Deleting branch...", async (spinner) => {
const toDeleteBranch = await branchNameToBranch(
meta.val.id,
toDeleteName,
);
if (toDeleteBranch.id === meta.branch.id) {
throw new Error(
"Cannot delete the current branch. Please switch to another branch first.",
);
}
await deleteValbranch(meta.val.id, toDeleteBranch.id);
spinner.succeed(`Branch '${toDeleteName}' has been deleted.`);
});
}
export const branchCmd = new Command()
.name("branch")
.description("List or delete branches")
.option(
"-D, --delete <name:string>",
"Delete a branch",
)
.example("List all branches", "vt branch")
.action(async ({ delete: delete_ }) => {
const vt = VTClient.from(await findVtRoot(Deno.cwd()));
if (delete_) {
await deleteBranch(vt, delete_);
} else {
await listBranches(vt);
}
});