forked from pkgxdev/pkgx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
43 lines (36 loc) · 997 Bytes
/
utils.ts
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
38
39
40
41
42
43
import Path from "path"
import { usePrefix } from "hooks"
export async function shout({ tea: args, cwd, net }: { tea: string[], cwd?: Path, net?: boolean }) {
const srcroot = Deno.env.get("SRCROOT")
const cmd = [
'deno',
'run',
'--allow-env', '--allow-read', '--allow-run'
]
if (net) cmd.push('--allow-net')
cmd.push(
'--unstable',
`--allow-write=${usePrefix()}`,
`--import-map=${srcroot}/import-map.json`,
`${srcroot}/src/app.ts`,
...args
)
// unset these if set
const env = {
VERBOSE: '',
DEBUG: '',
TEA_DIR: ''
}
const proc = Deno.run({ cmd, stdout: "piped", cwd: cwd?.string, env})
const raw = await proc.output()
proc.close()
return new TextDecoder().decode(raw)
}
export async function sandbox<T>(body: (tmpdir: Path) => Promise<T>) {
const path = new Path(await Deno.makeTempDir({ prefix: "tea" }))
try {
return await body(path)
} finally {
await Deno.remove(path.string, { recursive: true })
}
}