Skip to content

Commit c1d3f68

Browse files
finally git and env working
1 parent d93e37d commit c1d3f68

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
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": "2.0.0-rc.11",
3+
"version": "2.0.0-rc.10-debug",
44
"description": "The CodeSandbox SDK",
55
"author": "CodeSandbox",
66
"license": "MIT",

src/Sandbox.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@ export class Sandbox {
8888
customSession: SessionCreateOptions,
8989
session: SandboxSession
9090
) {
91-
if (!customSession.git && !customSession.env) {
92-
return;
93-
}
94-
9591
const client = await connectToSandbox({
9692
session,
9793
getSession: async () =>
@@ -100,9 +96,18 @@ export class Sandbox {
10096

10197
if (customSession.env) {
10298
const envStrings = Object.entries(customSession.env)
103-
.map(([key, value]) => `export ${key}=${value}`)
99+
.map(([key, value]) => {
100+
// escape any single-quotes in the value
101+
const safe = value.replace(/'/g, `'\\"'`);
102+
return `export ${key}='${safe}'`;
103+
})
104104
.join("\n");
105-
await client.commands.run(`echo "${envStrings}" > $HOME/.private/.env`);
105+
const cmd = [
106+
`cat << 'EOF' > "$HOME/.private/.env"`,
107+
envStrings,
108+
`EOF`,
109+
].join("\n");
110+
await client.commands.run(cmd);
106111
}
107112

108113
if (customSession.git) {
@@ -155,13 +160,6 @@ export class Sandbox {
155160
body: {
156161
session_id: customSession.id,
157162
permission: customSession.permission ?? "write",
158-
...(customSession.git
159-
? {
160-
git_access_token: customSession.git.accessToken,
161-
git_user_email: customSession.git.email,
162-
git_user_name: customSession.git.name,
163-
}
164-
: {}),
165163
},
166164
path: {
167165
id: this.id,
@@ -225,16 +223,20 @@ export class Sandbox {
225223
async createSession(
226224
customSession?: SessionCreateOptions
227225
): Promise<SandboxSession> {
228-
const session = await this.getSession(
229-
this.pitcherManagerResponse,
230-
customSession
231-
);
232-
233-
if (customSession) {
234-
const client = await this.initializeCustomSession(customSession, session);
235-
client?.disconnect();
226+
if (customSession?.git || customSession?.env) {
227+
const configureSession = await this.getSession(
228+
this.pitcherManagerResponse,
229+
customSession
230+
);
231+
232+
const client = await this.initializeCustomSession(
233+
customSession,
234+
configureSession
235+
);
236+
237+
client?.dispose();
236238
}
237239

238-
return session;
240+
return this.getSession(this.pitcherManagerResponse, customSession);
239241
}
240242
}

src/SandboxClient/commands.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,18 @@ export class Commands {
4848

4949
command = Array.isArray(command) ? command.join(" && ") : command;
5050

51-
const allEnv = Object.assign(opts?.env ?? {});
51+
const passedEnv = Object.assign(opts?.env ?? {});
52+
53+
const escapedCommand = command.replace(/'/g, "'\\''");
5254

5355
// TODO: use a new shell API that natively supports cwd & env
54-
let commandWithEnv = Object.keys(allEnv).length
56+
let commandWithEnv = Object.keys(passedEnv).length
5557
? `source $HOME/.private/.env 2>/dev/null || true && env ${Object.entries(
56-
allEnv
58+
passedEnv
5759
)
5860
.map(([key, value]) => `${key}=${value}`)
59-
.join(" ")} bash -c '${command}'`
60-
: `source $HOME/.private/.env 2>/dev/null || true && ${command}`;
61+
.join(" ")} bash -c '${escapedCommand}'`
62+
: `source $HOME/.private/.env 2>/dev/null || true && bash -c '${escapedCommand}'`;
6163

6264
if (opts?.cwd) {
6365
commandWithEnv = `cd ${opts.cwd} && ${commandWithEnv}`;

src/bin/commands/build.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ export const buildCommand: yargs.CommandModule<
236236

237237
await waitForSetup(session, index);
238238

239-
spinner.start(updateSpinnerMessage(index, "Creating fork state..."));
239+
spinner.start(
240+
updateSpinnerMessage(index, "Optimizing initial state...")
241+
);
240242
sandboxVM = await withCustomError(
241243
sdk.sandboxes.restart(id, {
242244
vmTier: sandboxTier,

0 commit comments

Comments
 (0)