-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpush.ts
More file actions
75 lines (67 loc) · 2.63 KB
/
push.ts
File metadata and controls
75 lines (67 loc) · 2.63 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
import { Command } from "@cliffy/command";
import { doWithSpinner } from "~/cmd/utils.ts";
import VTClient from "~/vt/vt/VTClient.ts";
import { findVtRoot } from "~/vt/vt/utils.ts";
import { getCurrentUser, getVal } from "~/sdk.ts";
import { displayFileStateChanges } from "~/cmd/lib/utils/displayFileStatus.ts";
import { noChangesDryRunMsg } from "~/cmd/lib/utils/messages.ts";
const nothingNewToPushMsg =
"No local changes to push, remote state is up to date";
export const pushCmd = new Command()
.name("push")
.description("Push local changes to a Val")
.example("Push local changes", "vt push")
.option(
"-d, --dry-run",
"Show what would be pushed without making any changes",
)
.action(async ({ dryRun }: { dryRun?: boolean }) => {
await doWithSpinner(
dryRun
? "Checking for local changes that would be pushed..."
: "Pushing local changes...",
async (spinner) => {
const vt = VTClient.from(await findVtRoot(Deno.cwd()));
const user = await getCurrentUser();
const vtState = await vt.getMeta().loadVtState();
const valToPush = await getVal(vtState.val.id);
if (valToPush.author.id !== user.id) {
throw new Error(
"You are not the owner of this Val, you cannot push." +
"\nTo make changes to this Val, go to the website, fork the Val, and clone the fork.",
);
}
// Note that we must wait until we have retrieved the status before
// stopping the spinner
if (dryRun) {
// Perform a dry push to get what would be pushed.
const statusResult = await vt.push({ dryRun: true });
spinner.stop();
console.log(displayFileStateChanges(statusResult, {
headerText: "Changes that would be pushed:",
summaryText: "Would push:",
emptyMessage: nothingNewToPushMsg,
includeSummary: true,
}));
console.log();
spinner.succeed(noChangesDryRunMsg);
} else {
// Perform the actual push, store the status, and then report it.
const statusResult = await vt.push();
spinner.stop();
// Display the changes that were pushed
console.log(displayFileStateChanges(statusResult, {
headerText: "Pushed:",
emptyMessage: nothingNewToPushMsg,
includeSummary: true,
}));
console.log();
if (statusResult.hasWarnings()) {
spinner.warn("Failed to push everything");
} else {
spinner.succeed("Successfully pushed local changes");
}
}
},
);
});