Skip to content

Commit 068504d

Browse files
fix cli
1 parent 74fe27f commit 068504d

File tree

9 files changed

+132
-113
lines changed

9 files changed

+132
-113
lines changed
File renamed without changes.

src/bin/commands/previewHosts.ts

Lines changed: 118 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -12,115 +12,132 @@ import {
1212
import { handleResponse } from "../../utils/api";
1313
import { BASE_URL, getApiKey } from "../utils/constants";
1414

15-
export type PreviewHostsCommandArgs = {
16-
list?: boolean;
17-
add?: string;
18-
remove?: string;
19-
clear?: boolean;
20-
};
21-
22-
export const previewHostsCommand: yargs.CommandModule<
23-
Record<string, never>,
24-
PreviewHostsCommandArgs
25-
> = {
15+
export const previewHostsCommand: yargs.CommandModule = {
2616
command: "preview-hosts",
2717
describe:
28-
"Manage preview hosts for your workspace. This allows you to access use the preview API from trusted hosts.",
29-
builder: (yargs: yargs.Argv) =>
30-
yargs
31-
.option("list", {
18+
"Manage preview hosts that should be able to access the Preview API",
19+
builder: (yargs) => {
20+
return yargs
21+
.command({
22+
command: "list",
3223
describe: "List current preview hosts",
33-
type: "boolean",
34-
})
35-
.option("add", {
36-
describe: "Add a preview host, ex. ",
37-
type: "string",
24+
handler: async () => {
25+
const API_KEY = getApiKey();
26+
const apiClient: Client = createClient(
27+
createConfig({
28+
baseUrl: BASE_URL,
29+
headers: {
30+
Authorization: `Bearer ${API_KEY}`,
31+
},
32+
})
33+
);
34+
const resp = await previewHostList({ client: apiClient });
35+
const data = handleResponse(resp, "Failed to list preview hosts");
36+
const hosts = data.preview_hosts.map(({ host }) => host);
37+
if (hosts.length) {
38+
console.log(hosts.join("\n"));
39+
} else {
40+
console.log("No preview hosts found");
41+
}
42+
},
3843
})
39-
.option("clear", {
40-
describe: "Clear all preview hosts",
41-
type: "boolean",
44+
.command({
45+
command: "add <host>",
46+
describe: "Add a preview host",
47+
builder: (yargs) =>
48+
yargs.positional("host", {
49+
describe: "Host to add",
50+
type: "string",
51+
demandOption: true,
52+
}),
53+
handler: async (argv) => {
54+
const API_KEY = getApiKey();
55+
const apiClient: Client = createClient(
56+
createConfig({
57+
baseUrl: BASE_URL,
58+
headers: {
59+
Authorization: `Bearer ${API_KEY}`,
60+
},
61+
})
62+
);
63+
const resp = await previewHostList({ client: apiClient });
64+
const data = handleResponse(resp, "Failed to list preview hosts");
65+
let hosts = data.preview_hosts.map(({ host }) => host);
66+
const hostToAdd = (argv.host as string).trim();
67+
if (hosts.includes(hostToAdd)) {
68+
console.log(`Host already exists: ${hostToAdd}`);
69+
return;
70+
}
71+
hosts.push(hostToAdd);
72+
await previewHostUpdate({
73+
client: apiClient,
74+
body: { hosts },
75+
});
76+
console.log(`Added preview host: ${hostToAdd}`);
77+
},
4278
})
43-
.option("remove", {
79+
.command({
80+
command: "remove <host>",
4481
describe: "Remove a preview host",
45-
type: "string",
46-
}),
47-
handler: async (argv) => {
48-
// Only allow one operation at a time
49-
const ops = [argv.list, argv.add, argv.remove, argv.clear].filter(Boolean);
50-
if (ops.length !== 1) {
51-
throw new Error(
52-
"Please specify exactly one operation: --list, --add, --remove, or --clear"
53-
);
54-
}
55-
56-
const API_KEY = getApiKey();
57-
const apiClient: Client = createClient(
58-
createConfig({
59-
baseUrl: BASE_URL,
60-
headers: {
61-
Authorization: `Bearer ${API_KEY}`,
82+
builder: (yargs) =>
83+
yargs.positional("host", {
84+
describe: "Host to remove",
85+
type: "string",
86+
demandOption: true,
87+
}),
88+
handler: async (argv) => {
89+
const API_KEY = getApiKey();
90+
const apiClient: Client = createClient(
91+
createConfig({
92+
baseUrl: BASE_URL,
93+
headers: {
94+
Authorization: `Bearer ${API_KEY}`,
95+
},
96+
})
97+
);
98+
const resp = await previewHostList({ client: apiClient });
99+
const data = handleResponse(resp, "Failed to list preview hosts");
100+
let hosts = data.preview_hosts.map(({ host }) => host);
101+
const hostToRemove = (argv.host as string).trim();
102+
if (!hosts.includes(hostToRemove)) {
103+
console.log(`Host not found: ${hostToRemove}`);
104+
return;
105+
}
106+
hosts = hosts.filter((h) => h !== hostToRemove);
107+
await previewHostUpdate({
108+
client: apiClient,
109+
body: { hosts },
110+
});
111+
console.log(`Removed preview host: ${hostToRemove}`);
62112
},
63113
})
64-
);
65-
66-
if (argv.list) {
67-
const resp = await previewHostList({ client: apiClient });
68-
const data = handleResponse(resp, "Failed to list preview hosts");
69-
const hosts = data.preview_hosts.map(({ host }) => host);
70-
if (hosts.length) {
71-
console.log(hosts.join("\n"));
72-
} else {
73-
console.log("No preview hosts found");
74-
}
75-
return;
76-
}
77-
78-
// For add, remove, clear: always work with the full list
79-
const resp = await previewHostList({ client: apiClient });
80-
const data = handleResponse(resp, "Failed to list preview hosts");
81-
let hosts = data.preview_hosts.map(({ host }) => host);
82-
83-
if (argv.add) {
84-
const hostToAdd = argv.add.trim();
85-
if (hosts.includes(hostToAdd)) {
86-
console.log(`Host already exists: ${hostToAdd}`);
87-
return;
88-
}
89-
hosts.push(hostToAdd);
90-
await previewHostUpdate({
91-
client: apiClient,
92-
body: { hosts },
93-
});
94-
console.log(`Added preview host: ${hostToAdd}`);
95-
return;
96-
}
97-
98-
if (argv.remove) {
99-
const hostToRemove = argv.remove.trim();
100-
if (!hosts.includes(hostToRemove)) {
101-
console.log(`Host not found: ${hostToRemove}`);
102-
return;
103-
}
104-
hosts = hosts.filter((h) => h !== hostToRemove);
105-
await previewHostUpdate({
106-
client: apiClient,
107-
body: { hosts },
108-
});
109-
console.log(`Removed preview host: ${hostToRemove}`);
110-
return;
111-
}
112-
113-
if (argv.clear) {
114-
if (hosts.length === 0) {
115-
console.log("Preview host list is already empty.");
116-
return;
117-
}
118-
await previewHostUpdate({
119-
client: apiClient,
120-
body: { hosts: [] },
114+
.command({
115+
command: "clear",
116+
describe: "Clear all preview hosts",
117+
handler: async () => {
118+
const API_KEY = getApiKey();
119+
const apiClient: Client = createClient(
120+
createConfig({
121+
baseUrl: BASE_URL,
122+
headers: {
123+
Authorization: `Bearer ${API_KEY}`,
124+
},
125+
})
126+
);
127+
const resp = await previewHostList({ client: apiClient });
128+
const data = handleResponse(resp, "Failed to list preview hosts");
129+
const hosts = data.preview_hosts.map(({ host }) => host);
130+
if (hosts.length === 0) {
131+
console.log("Preview host list is already empty.");
132+
return;
133+
}
134+
await previewHostUpdate({
135+
client: apiClient,
136+
body: { hosts: [] },
137+
});
138+
console.log("Cleared all preview hosts.");
139+
},
121140
});
122-
console.log("Cleared all preview hosts.");
123-
return;
124-
}
125141
},
142+
handler: () => {},
126143
};

src/bin/commands/sandbox/fork.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ export async function forkSandbox(sandboxId: string) {
66
const sdk = new CodeSandbox();
77

88
const spinner = ora("Forking sandbox...").start();
9-
const sandbox2 = await sdk.sandbox.fork(sandboxId);
9+
const sandbox2 = await sdk.sandboxes.create({
10+
source: "template",
11+
id: sandboxId,
12+
});
1013
spinner.succeed("Sandbox forked successfully");
1114

12-
sandbox2.disconnect();
1315
// eslint-disable-next-line no-console
1416
console.log(sandbox2.id);
1517
}

src/bin/commands/sandbox/hibernate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async function hibernateSingleSandbox(
1111
spinner: ReturnType<typeof ora>
1212
): Promise<CommandResult> {
1313
try {
14-
await new CodeSandbox().sandbox.hibernate(id);
14+
await new CodeSandbox().sandboxes.hibernate(id);
1515
const message = `✔ Sandbox ${id} hibernated successfully`;
1616
// eslint-disable-next-line no-console
1717
console.log(message);

src/bin/commands/sandbox/host-tokens.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export async function listPreviewTokens(sandboxId: string) {
1111
const spinner = ora("Fetching preview tokens...").start();
1212

1313
try {
14-
const tokens = await sdk.previewTokens.list(sandboxId);
14+
const tokens = await sdk.hosts.listTokens(sandboxId);
1515
spinner.stop();
1616

1717
if (tokens.length === 0) {
@@ -68,7 +68,7 @@ export async function createPreviewToken(
6868
const spinner = ora("Creating preview token...").start();
6969

7070
try {
71-
const token = await sdk.previewTokens.create(sandboxId, {
71+
const token = await sdk.hosts.createToken(sandboxId, {
7272
expiresAt: expiresAt ? new Date(expiresAt) : undefined,
7373
});
7474
spinner.stop();
@@ -121,7 +121,7 @@ export async function revokePreviewToken(
121121
const spinner = ora("Revoking preview token...").start();
122122

123123
try {
124-
await sdk.previewTokens.revoke(sandboxId, previewTokenId);
124+
await sdk.hosts.revokeToken(sandboxId, previewTokenId);
125125
spinner.stop();
126126
console.log("Preview token revoked successfully");
127127
} catch (error) {
@@ -139,7 +139,7 @@ export async function updatePreviewToken(
139139
const spinner = ora("Updating preview token...").start();
140140

141141
try {
142-
await sdk.previewTokens.update(
142+
await sdk.hosts.updateToken(
143143
sandboxId,
144144
previewTokenId,
145145
expiresAt ? new Date(expiresAt) : null
@@ -157,7 +157,7 @@ export async function revokeAllPreviewTokens(sandboxId: string) {
157157
const spinner = ora("Revoking all preview tokens...").start();
158158

159159
try {
160-
await sdk.previewTokens.revokeAll(sandboxId);
160+
await sdk.hosts.revokeAllTokens(sandboxId);
161161
spinner.stop();
162162
console.log("All preview tokens have been revoked");
163163
} catch (error) {

src/bin/commands/sandbox/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export async function listSandboxes(
8383
sandboxes,
8484
totalCount: total,
8585
pagination,
86-
} = await sdk.sandbox.list({
86+
} = await sdk.sandboxes.list({
8787
...listOpts,
8888
pagination: {
8989
page: currentPage,

src/bin/commands/sandbox/shutdown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async function shutdownSingleSandbox(
1111
spinner: ReturnType<typeof ora>
1212
): Promise<CommandResult> {
1313
try {
14-
await new CodeSandbox().sandbox.shutdown(id);
14+
await new CodeSandbox().sandboxes.shutdown(id);
1515
const message = `✔ Sandbox ${id} shutdown successfully`;
1616
// eslint-disable-next-line no-console
1717
console.log(message);

src/bin/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { hideBin } from "yargs/helpers";
44
import { buildCommand } from "./commands/build";
55
import { sandboxesCommand } from "./commands/sandbox";
66
import { previewHostsCommand } from "./commands/previewHosts";
7-
import { hostTokensCommand } from "./commands/host-tokens";
7+
import { hostTokensCommand } from "./commands/hostTokens";
88

99
yargs(hideBin(process.argv))
1010
.usage("CodeSandbox SDK CLI - Manage your CodeSandbox projects")

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class CodeSandbox {
4949
createConfig({
5050
baseUrl,
5151
headers: {
52-
Authorization: `Bearer ${apiToken}`,
52+
Authorization: `Bearer ${evaluatedApiToken}`,
5353
...(opts.headers ?? {}),
5454
},
5555
fetch: opts.fetch ?? fetch,

0 commit comments

Comments
 (0)