(
handle: SandboxHandle,
opts: BootstrapShellOptions = {},
)
| 208 | * marker lines) so the NEXT command inherits any directory change or exports. |
| 209 | */ |
| 210 | export function createExecBootstrapShell( |
| 211 | handle: SandboxHandle, |
| 212 | opts: BootstrapShellOptions = {}, |
| 213 | ): BootstrapShell { |
| 214 | let cwd = opts.cwd ?? '/' |
| 215 | let env: Record<string, string> = {} |
| 216 | let counter = 0 |
| 217 | |
| 218 | async function run( |
| 219 | command: string, |
| 220 | ): Promise<{ exitCode: number; stdout: string }> { |
| 221 | const id = counter |
| 222 | counter += 1 |
| 223 | const sentinel = `__BSSH_${id}__` |
| 224 | |
| 225 | // Run the command, then emit its exit code, cwd and exported env behind |
| 226 | // marker lines so we can recover state even when the command itself fails |
| 227 | // (no `set -e`). Capturing `$?` immediately after the command keeps the |
| 228 | // reported exit code the command's own, not the trailing introspection's. |
| 229 | const script = [ |
| 230 | command, |
| 231 | `__bssh_rc=$?`, |
| 232 | `printf '\\n%s %s\\n' '${sentinel}' "$__bssh_rc"`, |
| 233 | `printf '%s\\n' '${sentinel}_CWD'`, |
| 234 | `pwd`, |
| 235 | `printf '%s\\n' '${sentinel}_ENV'`, |
| 236 | `export -p`, |
| 237 | ].join('\n') |
| 238 | |
| 239 | const res = await handle.process.exec(script, { cwd, env }) |
| 240 | |
| 241 | const cmdOut: Array<string> = [] |
| 242 | const cwdLines: Array<string> = [] |
| 243 | const envLines: Array<string> = [] |
| 244 | let exitCode = res.exitCode |
| 245 | let phase: 'cmd' | 'await-cwd' | 'cwd' | 'env' = 'cmd' |
| 246 | |
| 247 | for (const line of res.stdout.split('\n')) { |
| 248 | if (phase === 'cmd') { |
| 249 | if (line.startsWith(`${sentinel} `)) { |
| 250 | const parsed = parseInt(line.slice(sentinel.length + 1).trim(), 10) |
| 251 | exitCode = Number.isFinite(parsed) ? parsed : res.exitCode |
| 252 | phase = 'await-cwd' |
| 253 | continue |
| 254 | } |
| 255 | cmdOut.push(line) |
| 256 | } else if (phase === 'await-cwd') { |
| 257 | if (line === `${sentinel}_CWD`) phase = 'cwd' |
| 258 | } else if (phase === 'cwd') { |
| 259 | if (line === `${sentinel}_ENV`) phase = 'env' |
| 260 | else cwdLines.push(line) |
| 261 | } else { |
| 262 | envLines.push(line) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // `pwd` prints a single line; the last non-empty one is the new cwd. |
| 267 | const newCwd = cwdLines |
no outgoing calls
no test coverage detected