-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcreate.ts
More file actions
103 lines (98 loc) · 2.85 KB
/
create.ts
File metadata and controls
103 lines (98 loc) · 2.85 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
import { Command } from "@cliffy/command";
import { basename } from "@std/path";
import VTClient from "~/vt/vt/VTClient.ts";
import { getCurrentUser } from "~/sdk.ts";
import { APIError } from "@valtown/sdk";
import { doWithSpinner, getClonePath } from "~/cmd/utils.ts";
import { ensureAddEditorFiles } from "~/cmd/lib/utils/messages.ts";
import { Confirm } from "@cliffy/prompt";
import { DEFAULT_EDITOR_TEMPLATE } from "~/consts.ts";
export const createCmd = new Command()
.name("create")
.description("Create a new Val")
.arguments("<valName:string> [targetDir:string]")
.option("--public", "Create as public Val (default)", {
conflicts: ["private", "unlisted"],
})
.option("--private", "Create as private Val", {
conflicts: ["public", "unlisted"],
})
.option("--unlisted", "Create as unlisted Val", {
conflicts: ["public", "private"],
})
.option("--no-editor-files", "Skip creating editor configuration files")
.option("-d, --description <desc:string>", "Val description")
.example(
"Start fresh",
`
vt create my-val
cd ./my-val
vt browse
vt watch # syncs changes to Val town`,
)
.example(
"Work on an existing val",
`
vt clone username/valName
cd ./valName
vim index.tsx
vt push`,
)
.example(
"Check out a new branch",
`
cd ./valName
vt checkout -b my-branch
vim index.tsx
vt push
vt checkout main`,
)
.action(async (
{
private: isPrivate,
unlisted,
description,
editorFiles,
}: {
public?: boolean;
private?: boolean;
unlisted?: boolean;
description?: string;
editorFiles?: boolean;
},
valName: string,
targetDir?: string,
) => {
await doWithSpinner("Creating new Val...", async (spinner) => {
const user = await getCurrentUser();
const clonePath = getClonePath(targetDir, valName);
// Determine privacy setting (defaults to public)
const privacy = isPrivate ? "private" : unlisted ? "unlisted" : "public";
try {
const vt = await VTClient.create({
rootPath: clonePath,
valName,
username: user.username!,
privacy,
description,
});
if (editorFiles) {
spinner.stop();
const { editorTemplate } = await vt.getConfig().loadConfig();
const confirmed = await Confirm.prompt(
ensureAddEditorFiles(editorTemplate ?? DEFAULT_EDITOR_TEMPLATE),
);
if (confirmed) await vt.addEditorTemplate();
console.log();
}
spinner.succeed(
`Created ${privacy} Val "${valName}" in "${basename(clonePath)}"`,
);
} catch (error) {
if (error instanceof APIError && error.status === 409) {
await Deno.remove(clonePath, { recursive: true });
throw new Error(`Val name "${valName}" already exists`);
} else throw error;
}
});
});