-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.js
49 lines (40 loc) · 1.41 KB
/
mod.js
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
44
45
46
47
48
49
// TODO: typescriptify
export function configure(opts) {
const {
ignoreExitCode = false, // throws on exit code != 0
encoding = 'utf-8', // null for Uint8Array or TextDecoder encoding
trim = true, // trim string output. invalid for encoding null
} = opts
const decoder = new TextDecoder(encoding)
if (encoding == null && trim) {
throw new Error('Must specify an encoding if trim is enabled')
}
return async function shell(strings, ...keys) {
let command = strings[0]
for (let i = 1; i < strings.length; i++) {
command += keys[i - 1].toString()
command += strings[i]
}
let proc = Deno.run({
cmd: ['/bin/sh', '-c', command],
stdin: 'piped',
stdout: 'piped',
stderr: 'piped',
})
let stdout = await proc.output()
let { code } = await proc.status()
if (code != 0 && !ignoreExitCode) {
let stderr = await Deno.readAll(proc.stderr)
let decoder = new TextDecoder('utf-8')
let error = decoder.decode(stderr).trim()
throw new Error(`Non-zero exit code: ${code} ${error}`)
}
if (encoding == null) {
return stdout
}
let text = decoder.decode(stdout)
if (trim) text = text.trim()
return text
}
}
export default configure({})