Skip to content

Commit d35bfdf

Browse files
Add env and cwd parameters to command execution docs
Updates documentation for exec(), execStream(), and startProcess() methods to reflect new environment variable and working directory support added in cloudflare/sandbox-sdk#204. Changes: - Add env and cwd parameters to exec() options - Add env and cwd parameters to execStream() options - Expand startProcess() documentation with all available options - Add examples showing usage of new parameters 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent f1e1a9d commit d35bfdf

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/content/docs/sandbox/api/commands.mdx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const result = await sandbox.exec(command: string, options?: ExecOptions): Promi
2525
- `stream` - Enable streaming callbacks (default: `false`)
2626
- `onOutput` - Callback for real-time output: `(stream: 'stdout' | 'stderr', data: string) => void`
2727
- `timeout` - Maximum execution time in milliseconds
28+
- `env` - Environment variables for this command (merged with session defaults)
29+
- `cwd` - Working directory for this command (overrides session default)
2830

2931
**Returns**: `Promise<ExecuteResponse>` with `success`, `stdout`, `stderr`, `exitCode`
3032

@@ -38,6 +40,12 @@ if (result.success) {
3840
console.error('Build failed:', result.stderr);
3941
}
4042
43+
// With custom environment and working directory
44+
await sandbox.exec('npm test', {
45+
cwd: '/workspace/my-app',
46+
env: { NODE_ENV: 'test', CI: 'true' }
47+
});
48+
4149
// With streaming
4250
await sandbox.exec('npm install', {
4351
stream: true,
@@ -56,7 +64,7 @@ const stream = await sandbox.execStream(command: string, options?: ExecOptions):
5664

5765
**Parameters**:
5866
- `command` - The command to execute
59-
- `options` - Same as `exec()`
67+
- `options` (optional) - Same as `exec()` (includes `timeout`, `env`, `cwd`)
6068

6169
**Returns**: `Promise<ReadableStream>` emitting `ExecEvent` objects (`start`, `stdout`, `stderr`, `complete`, `error`)
6270

@@ -79,6 +87,12 @@ for await (const event of parseSSEStream<ExecEvent>(stream)) {
7987
break;
8088
}
8189
}
90+
91+
// With custom environment
92+
const testStream = await sandbox.execStream('npm run test', {
93+
env: { NODE_ENV: 'test' },
94+
cwd: '/workspace/tests'
95+
});
8296
```
8397
</TypeScriptExample>
8498

@@ -93,8 +107,11 @@ const process = await sandbox.startProcess(command: string, options?: ProcessOpt
93107
**Parameters**:
94108
- `command` - The command to start as a background process
95109
- `options` (optional):
96-
- `cwd` - Working directory
97-
- `env` - Environment variables
110+
- `cwd` - Working directory (overrides session default)
111+
- `env` - Environment variables (merged with session defaults)
112+
- `timeout` - Maximum execution time in milliseconds
113+
- `processId` - Custom process ID for later reference
114+
- `autoCleanup` - Automatically cleanup process record after exit (default: `true`)
98115

99116
**Returns**: `Promise<ProcessInfo>` with `id`, `pid`, `command`, `status`
100117

@@ -103,11 +120,17 @@ const process = await sandbox.startProcess(command: string, options?: ProcessOpt
103120
const server = await sandbox.startProcess('python -m http.server 8000');
104121
console.log('Started with PID:', server.pid);
105122
106-
// With custom environment
123+
// With custom environment and working directory
107124
const app = await sandbox.startProcess('node app.js', {
108125
cwd: '/workspace/my-app',
109126
env: { NODE_ENV: 'production', PORT: '3000' }
110127
});
128+
129+
// With custom process ID for easy reference
130+
const db = await sandbox.startProcess('redis-server', {
131+
processId: 'redis',
132+
env: { REDIS_PORT: '6379' }
133+
});
111134
```
112135
</TypeScriptExample>
113136

0 commit comments

Comments
 (0)