-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathnested-dirs.ts
More file actions
37 lines (31 loc) · 1.29 KB
/
nested-dirs.ts
File metadata and controls
37 lines (31 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Nested directory structures in a bVisor sandbox.
*
* Because the sandbox doesn't track state between commands, any operation
* that needs to work inside a subdirectory must use `cd <dir> && <cmd>`.
*/
import { Sandbox } from "bvisor";
const sb = new Sandbox();
// Helper function to run a bash command and log its output streams.
async function run(cmd: string): Promise<void> {
const output = sb.runCmd(cmd);
const stdout = await output.stdout();
const stderr = await output.stderr();
console.log(`bvisor> ${cmd}`);
if (stdout) console.log(stdout.trimEnd());
if (stderr) console.error(stderr.trimEnd());
console.log();
}
// Build a directory tree and populate it with files.
await run("mkdir -p project/src/assets");
await run("echo 'hello from root' > project/README.md");
await run("echo 'print(\"hello from src\")' > project/src/main.py");
await run("echo 'hello from assets' > project/src/assets/info.txt");
// Show the full tree.
await run("find project -type f");
// Run the script from its own directory.
// The sandbox starts at / each command, so we cd into the directory first.
await run("cd project/src && python3 main.py");
// Append to a file and verify.
await run("echo 'goodbye from assets' >> project/src/assets/info.txt");
await run("cat project/src/assets/info.txt");