Skip to content

Commit f472813

Browse files
fix python inteprreter and template CLI visuals
1 parent 89f57ac commit f472813

File tree

3 files changed

+49
-75
lines changed

3 files changed

+49
-75
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codesandbox/sdk",
3-
"version": "1.0.0-rc.1",
3+
"version": "1.0.0-rc.3",
44
"description": "The CodeSandbox SDK",
55
"author": "CodeSandbox",
66
"license": "MIT",

src/bin/commands/build.ts

Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,6 @@ export type BuildCommandArgs = {
3131
vmTier?: VmUpdateSpecsRequest["tier"];
3232
};
3333

34-
function createSpinnerFactory() {
35-
let currentLineIndex = 0;
36-
let currentSpinnerIndex = 0;
37-
38-
return () => {
39-
const spinner = ora({ stream: process.stdout });
40-
const spinnerIndex = currentSpinnerIndex++;
41-
let lastMethod: string;
42-
43-
function updateCursor(method: string) {
44-
readline.moveCursor(
45-
process.stdout,
46-
0,
47-
spinnerIndex - currentLineIndex + (lastMethod !== "start" ? -1 : 0)
48-
);
49-
currentLineIndex = spinnerIndex;
50-
lastMethod = method;
51-
}
52-
53-
return {
54-
start(message: string) {
55-
updateCursor("start");
56-
spinner.start(message);
57-
},
58-
succeed(message: string) {
59-
updateCursor("succeed");
60-
spinner.succeed(message);
61-
},
62-
fail(message: string) {
63-
updateCursor("fail");
64-
spinner.fail(message);
65-
},
66-
info(message: string) {
67-
updateCursor("info");
68-
spinner.info(message);
69-
},
70-
};
71-
};
72-
}
73-
7434
export const buildCommand: yargs.CommandModule<
7535
Record<string, never>,
7636
BuildCommandArgs
@@ -121,8 +81,6 @@ export const buildCommand: yargs.CommandModule<
12181
})
12282
);
12383

124-
const createSpinner = createSpinnerFactory();
125-
12684
try {
12785
const clustersData = handleResponse(
12886
await vmListClusters({
@@ -133,33 +91,40 @@ export const buildCommand: yargs.CommandModule<
13391

13492
const clusters = clustersData.clusters;
13593

94+
const spinner = ora({ stream: process.stdout });
95+
let spinnerMessages: string[] = clusters.map(() => "");
96+
97+
function updateSpinnerMessage(
98+
index: number,
99+
message: string,
100+
sandboxId?: string
101+
) {
102+
spinnerMessages[index] = `[cluster: ${
103+
clusters[index].slug
104+
}, sandboxId: ${sandboxId || "-"}]: ${message}`;
105+
106+
return `\n${spinnerMessages.join("\n")}`;
107+
}
108+
136109
const sandboxIds = await Promise.all(
137-
clusters.map(async ({ host: cluster, slug }) => {
110+
clusters.map(async ({ host: cluster, slug }, index) => {
138111
const sdk = new CodeSandbox(API_KEY, {
139112
baseUrl: BASE_URL,
140113
headers: {
141114
"x-pitcher-manager-url": `https://${cluster}/api/v1`,
142115
},
143116
});
144-
const spinner = createSpinner();
145-
146-
function createSpinnerMessage(message: string, sandboxId?: string) {
147-
return `[cluster: ${slug}, sandboxId: ${
148-
sandboxId || "-"
149-
}]: ${message}`;
150-
}
151117

152118
let sandboxId: string | undefined;
153119

154120
try {
155121
const { hash, files: filePaths } = await hashDirectory(
156122
argv.directory
157123
);
158-
spinner.succeed(`Indexed ${filePaths.length} files`);
159124
const shortHash = hash.slice(0, 6);
160125
const tag = `sha:${shortHash}-${slug}`;
161126

162-
spinner.start(createSpinnerMessage("Creating sandbox..."));
127+
spinner.start(updateSpinnerMessage(index, "Creating sandbox..."));
163128
sandboxId = await createSandbox({
164129
apiClient,
165130
shaTag: tag,
@@ -170,7 +135,7 @@ export const buildCommand: yargs.CommandModule<
170135
});
171136

172137
spinner.start(
173-
createSpinnerMessage("Starting sandbox...", sandboxId)
138+
updateSpinnerMessage(index, "Starting sandbox...", sandboxId)
174139
);
175140

176141
const startResponse = await startVm(apiClient, sandboxId, {
@@ -180,7 +145,11 @@ export const buildCommand: yargs.CommandModule<
180145
let session = await sandbox.connect();
181146

182147
spinner.start(
183-
createSpinnerMessage("Writing files to sandbox...", sandboxId)
148+
updateSpinnerMessage(
149+
index,
150+
"Writing files to sandbox...",
151+
sandboxId
152+
)
184153
);
185154
let i = 0;
186155
for (const filePath of filePaths) {
@@ -196,9 +165,9 @@ export const buildCommand: yargs.CommandModule<
196165
}
197166

198167
spinner.start(
199-
createSpinnerMessage("Restarting sandbox...", sandboxId)
168+
updateSpinnerMessage(index, "Restarting sandbox...", sandboxId)
200169
);
201-
sandbox = await sdk.sandbox.restart(sandbox.id);
170+
sandbox = await sdk.sandboxes.restart(sandbox.id);
202171
session = await sandbox.connect();
203172

204173
const disposableStore = new DisposableStore();
@@ -210,7 +179,8 @@ export const buildCommand: yargs.CommandModule<
210179

211180
try {
212181
spinner.start(
213-
createSpinnerMessage(
182+
updateSpinnerMessage(
183+
index,
214184
`Running setup ${steps.indexOf(step) + 1} / ${
215185
steps.length
216186
} - ${step.name}...`,
@@ -230,13 +200,6 @@ export const buildCommand: yargs.CommandModule<
230200

231201
await step.waitUntilComplete();
232202
} catch (error) {
233-
spinner.fail(
234-
createSpinnerMessage(
235-
`Setup step failed: ${step.name}`,
236-
sandboxId
237-
)
238-
);
239-
console.log(buffer.join("\n"));
240203
throw new Error(`Setup step failed: ${step.name}`);
241204
}
242205
}
@@ -247,7 +210,8 @@ export const buildCommand: yargs.CommandModule<
247210
const updatePortSpinner = () => {
248211
const isMultiplePorts = ports.length > 1;
249212
spinner.start(
250-
createSpinnerMessage(
213+
updateSpinnerMessage(
214+
index,
251215
`Waiting for ${
252216
isMultiplePorts ? "ports" : "port"
253217
} ${ports.join(", ")} to open...`,
@@ -267,7 +231,7 @@ export const buildCommand: yargs.CommandModule<
267231

268232
// eslint-disable-next-line no-constant-condition
269233
while (true) {
270-
const res = await fetch("https://" + portInfo.url);
234+
const res = await fetch("https://" + portInfo.host);
271235
if (res.status !== 502 && res.status !== 503) {
272236
break;
273237
}
@@ -280,7 +244,8 @@ export const buildCommand: yargs.CommandModule<
280244
);
281245
} else {
282246
spinner.start(
283-
createSpinnerMessage(
247+
updateSpinnerMessage(
248+
index,
284249
"No ports to open, waiting 5 seconds for tasks to run...",
285250
sandboxId
286251
)
@@ -289,17 +254,22 @@ export const buildCommand: yargs.CommandModule<
289254
}
290255

291256
spinner.start(
292-
createSpinnerMessage("Creating memory snapshot...", sandboxId)
257+
updateSpinnerMessage(
258+
index,
259+
"Creating memory snapshot...",
260+
sandboxId
261+
)
293262
);
294-
await sdk.sandbox.hibernate(sandbox.id);
295-
spinner.succeed(
296-
createSpinnerMessage("Snapshot created", sandboxId)
263+
await sdk.sandboxes.hibernate(sandbox.id);
264+
spinner.start(
265+
updateSpinnerMessage(index, "Snapshot created", sandboxId)
297266
);
298267

299268
return sandbox.id;
300269
} catch (error) {
301270
spinner.fail(
302-
createSpinnerMessage(
271+
updateSpinnerMessage(
272+
index,
303273
error instanceof Error
304274
? error.message
305275
: "Unknown error occurred",
@@ -311,6 +281,8 @@ export const buildCommand: yargs.CommandModule<
311281
})
312282
);
313283

284+
spinner.succeed(`\n${spinnerMessages.join("\n")}`);
285+
314286
const data = handleResponse(
315287
await vmCreateTag({
316288
client: apiClient,
@@ -321,6 +293,7 @@ export const buildCommand: yargs.CommandModule<
321293
"Failed to create template"
322294
);
323295
console.log("Template created: " + data.tag_id);
296+
process.exit(0);
324297
} catch (error) {
325298
console.error(error);
326299
process.exit(1);

src/sessions/WebSocketSession/interpreters.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ EOF
3333
);
3434
}
3535
python(code: string) {
36-
return this.run(`python3 -c "exec('''\
36+
return this.run(`python3 <<'PYCODE'
3737
${code
3838
.split("\n")
3939
.map((line, index, lines) => {
@@ -42,6 +42,7 @@ ${code
4242
: line;
4343
})
4444
.join("\n")}
45-
''')"`);
45+
PYCODE
46+
`);
4647
}
4748
}

0 commit comments

Comments
 (0)