@pocketenv/cli 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,123 @@
1
+ import chalk from "chalk";
2
+ import consola from "consola";
3
+ import getAccessToken from "../lib/getAccessToken";
4
+ import { client } from "../client";
5
+ import { env } from "../lib/env";
6
+ import type { Volume } from "../types/volume";
7
+ import CliTable3 from "cli-table3";
8
+ import { c } from "../theme";
9
+ import dayjs from "dayjs";
10
+ import relativeTime from "dayjs/plugin/relativeTime";
11
+
12
+ dayjs.extend(relativeTime);
13
+
14
+ export async function listVolumes(sandboxId: string) {
15
+ const token = await getAccessToken();
16
+
17
+ const response = await client.get<{ volumes: Volume[] }>(
18
+ "/xrpc/io.pocketenv.volume.getVolumes",
19
+ {
20
+ params: {
21
+ sandboxId,
22
+ },
23
+ headers: {
24
+ Authorization: `Bearer ${env.POCKETENV_TOKEN || token}`,
25
+ },
26
+ },
27
+ );
28
+
29
+ const table = new CliTable3({
30
+ head: [
31
+ c.primary("ID"),
32
+ c.primary("NAME"),
33
+ c.primary("PATH"),
34
+ c.primary("CREATED AT"),
35
+ ],
36
+ chars: {
37
+ top: "",
38
+ "top-mid": "",
39
+ "top-left": "",
40
+ "top-right": "",
41
+ bottom: "",
42
+ "bottom-mid": "",
43
+ "bottom-left": "",
44
+ "bottom-right": "",
45
+ left: "",
46
+ "left-mid": "",
47
+ mid: "",
48
+ "mid-mid": "",
49
+ right: "",
50
+ "right-mid": "",
51
+ middle: " ",
52
+ },
53
+ style: {
54
+ border: [],
55
+ head: [],
56
+ },
57
+ });
58
+
59
+ for (const volume of response.data.volumes) {
60
+ table.push([
61
+ c.secondary(volume.id),
62
+ volume.name,
63
+ volume.path,
64
+ dayjs(volume.createdAt).fromNow(),
65
+ ]);
66
+ }
67
+
68
+ consola.log(table.toString());
69
+ }
70
+
71
+ export async function createVolume(
72
+ sandbox: string,
73
+ name: string,
74
+ path: string,
75
+ ) {
76
+ const token = await getAccessToken();
77
+
78
+ try {
79
+ await client.post(
80
+ "/xrpc/io.pocketenv.volume.addVolume",
81
+ {
82
+ volume: {
83
+ sandboxId: sandbox,
84
+ name,
85
+ path,
86
+ },
87
+ },
88
+ {
89
+ headers: {
90
+ Authorization: `Bearer ${env.POCKETENV_TOKEN || token}`,
91
+ },
92
+ },
93
+ );
94
+
95
+ consola.success(
96
+ `Volume ${chalk.rgb(0, 232, 198)(name)} successfully mounted in sandbox ${chalk.rgb(0, 232, 198)(sandbox)} at path ${chalk.rgb(0, 232, 198)(path)}`,
97
+ );
98
+ } catch (error) {
99
+ consola.error("Failed to create volume:", error);
100
+ }
101
+ }
102
+
103
+ export async function deleteVolume(id: string) {
104
+ const token = await getAccessToken();
105
+
106
+ try {
107
+ await client.post(`/xrpc/io.pocketenv.volume.deleteVolume`, undefined, {
108
+ params: {
109
+ id,
110
+ },
111
+ headers: {
112
+ Authorization: `Bearer ${env.POCKETENV_TOKEN || token}`,
113
+ },
114
+ });
115
+ } catch (error) {
116
+ consola.error(`Failed to delete volume: ${error}`);
117
+ return;
118
+ }
119
+
120
+ consola.success(
121
+ `Volume ${chalk.rgb(0, 232, 198)(id)} successfully deleted from sandbox`,
122
+ );
123
+ }
@@ -0,0 +1,32 @@
1
+ import consola from "consola";
2
+ import getAccessToken from "../lib/getAccessToken";
3
+ import { client } from "../client";
4
+ import { env } from "../lib/env";
5
+ import { c } from "../theme";
6
+
7
+ export async function exposeVscode(sandbox: string) {
8
+ const token = await getAccessToken();
9
+ try {
10
+ const response = await client.post<{ previewUrl?: string }>(
11
+ `/xrpc/io.pocketenv.sandbox.exposeVscode`,
12
+ undefined,
13
+ {
14
+ params: {
15
+ id: sandbox,
16
+ },
17
+ headers: {
18
+ Authorization: `Bearer ${env.POCKETENV_TOKEN || token}`,
19
+ },
20
+ },
21
+ );
22
+
23
+ consola.success(`VS Code Server exposed for sandbox ${c.primary(sandbox)}`);
24
+
25
+ if (response.data.previewUrl) {
26
+ consola.success(`Preview URL: ${c.secondary(response.data.previewUrl)}`);
27
+ }
28
+ } catch (error) {
29
+ consola.error("Failed to expose VS Code:", error);
30
+ process.exit(1);
31
+ }
32
+ }
package/src/index.ts CHANGED
@@ -14,16 +14,14 @@ import { deleteSecret, listSecrets, putSecret } from "./cmd/secret";
14
14
  import { deleteEnv, listEnvs, putEnv } from "./cmd/env";
15
15
  import { getSshKey, putKeys } from "./cmd/sshkeys";
16
16
  import { getTailscaleAuthKey, putAuthKey } from "./cmd/tailscale";
17
-
18
- const c = {
19
- primary: (s: string) => chalk.rgb(0, 232, 198)(s),
20
- secondary: (s: string) => chalk.rgb(0, 198, 232)(s),
21
- accent: (s: string) => chalk.rgb(130, 100, 255)(s),
22
- highlight: (s: string) => chalk.rgb(100, 232, 130)(s),
23
- muted: (s: string) => chalk.rgb(200, 210, 220)(s),
24
- link: (s: string) => chalk.rgb(255, 160, 100)(s),
25
- sky: (s: string) => chalk.rgb(0, 210, 255)(s),
26
- };
17
+ import { exposePort } from "./cmd/expose";
18
+ import { unexposePort } from "./cmd/unexpose";
19
+ import { createVolume, deleteVolume, listVolumes } from "./cmd/volume";
20
+ import { deleteFile, listFiles, putFile } from "./cmd/file";
21
+ import consola from "consola";
22
+ import { listPorts } from "./cmd/ports";
23
+ import { c } from "./theme";
24
+ import { exposeVscode } from "./cmd/vscode";
27
25
 
28
26
  const program = new Command();
29
27
 
@@ -77,6 +75,7 @@ program.command("ls").description("list sandboxes").action(listSandboxes);
77
75
  program
78
76
  .command("start")
79
77
  .argument("<sandbox>", "the sandbox to start")
78
+ .option("--ssh, -s", "connect to the Sandbox and automatically open a shell")
80
79
  .description("start the given sandbox")
81
80
  .action(start);
82
81
 
@@ -111,6 +110,97 @@ program
111
110
  .description("delete the given sandbox")
112
111
  .action(deleteSandbox);
113
112
 
113
+ program
114
+ .command("vscode")
115
+ .argument("<sandbox>", "the sandbox to expose VS Code for")
116
+ .description(
117
+ "expose a VS Code Server instance running in the given sandbox to the internet",
118
+ )
119
+ .action(exposeVscode);
120
+
121
+ program
122
+ .command("expose")
123
+ .argument("<sandbox>", "the sandbox to expose a port for")
124
+ .argument("<port>", "the port to expose", (val) => {
125
+ const port = parseInt(val, 10);
126
+ if (isNaN(port)) {
127
+ consola.error(`port must be a number, got: ${val}`);
128
+ process.exit(1);
129
+ }
130
+ return port;
131
+ })
132
+ .argument("[description]", "an optional description for the exposed port")
133
+ .description("expose a port from the given sandbox to the internet")
134
+ .action(exposePort);
135
+
136
+ program
137
+ .command("unexpose")
138
+ .argument("<sandbox>", "the sandbox to unexpose a port for")
139
+ .argument("<port>", "the port to unexpose", (val) => {
140
+ const port = parseInt(val, 10);
141
+ if (isNaN(port)) {
142
+ consola.error(`port must be a number, got: ${val}`);
143
+ process.exit(1);
144
+ }
145
+ return port;
146
+ })
147
+ .description("unexpose a port from the given sandbox")
148
+ .action(unexposePort);
149
+
150
+ const volume = program.command("volume").description("manage volumes");
151
+
152
+ volume
153
+ .command("put")
154
+ .argument("<sandbox>", "the sandbox to put the volume in")
155
+ .argument("<name>", "the name of the volume")
156
+ .argument("<path>", "the path to mount the volume at")
157
+ .description("put a volume in the given sandbox")
158
+ .action(createVolume);
159
+
160
+ volume
161
+ .command("list")
162
+ .aliases(["ls"])
163
+ .argument("<sandbox>", "the sandbox to list volumes for")
164
+ .description("list volumes in the given sandbox")
165
+ .action(listVolumes);
166
+
167
+ volume
168
+ .command("delete")
169
+ .aliases(["rm", "remove"])
170
+ .argument("<id>", "the ID of the volume to delete")
171
+ .description("delete a volume")
172
+ .action(deleteVolume);
173
+
174
+ const file = program.command("file").description("manage files");
175
+
176
+ file
177
+ .command("put")
178
+ .argument("<sandbox>", "the sandbox to put the file in")
179
+ .argument("<path>", "the remote path to upload the file to")
180
+ .argument("[localPath]", "the local path of the file to upload")
181
+ .description("upload a file to the given sandbox")
182
+ .action(putFile);
183
+
184
+ file
185
+ .command("list")
186
+ .aliases(["ls"])
187
+ .argument("<sandbox>", "the sandbox to list files for")
188
+ .description("list files in the given sandbox")
189
+ .action(listFiles);
190
+
191
+ file
192
+ .command("delete")
193
+ .aliases(["rm", "remove"])
194
+ .argument("<id>", "the ID of the file to delete")
195
+ .description("delete a file")
196
+ .action(deleteFile);
197
+
198
+ program
199
+ .command("ports")
200
+ .argument("<sandbox>", "the sandbox to list exposed ports for")
201
+ .description("list exposed ports for a sandbox")
202
+ .action(listPorts);
203
+
114
204
  const secret = program.command("secret").description("manage secrets");
115
205
 
116
206
  secret
package/src/theme.ts ADDED
@@ -0,0 +1,12 @@
1
+ import chalk from "chalk";
2
+
3
+ export const c = {
4
+ primary: (s: string | number) => chalk.rgb(0, 232, 198)(s),
5
+ secondary: (s: string | number) => chalk.rgb(0, 198, 232)(s),
6
+ accent: (s: string | number) => chalk.rgb(130, 100, 255)(s),
7
+ highlight: (s: string | number) => chalk.rgb(100, 232, 130)(s),
8
+ muted: (s: string | number) => chalk.rgb(200, 210, 220)(s),
9
+ link: (s: string | number) => chalk.rgb(255, 160, 100)(s),
10
+ sky: (s: string | number) => chalk.rgb(0, 210, 255)(s),
11
+ error: (s: string | number) => chalk.rgb(255, 100, 100)(s),
12
+ };
@@ -0,0 +1,5 @@
1
+ export type Port = {
2
+ port: number;
3
+ description?: string;
4
+ previewUrl?: string;
5
+ };