-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.ts
executable file
·64 lines (51 loc) · 1.61 KB
/
cmd.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
type stdout = "inherit" | "piped" | "null" | number;
type stderr = "inherit" | "piped" | "null" | number;
type stdin = "inherit" | "piped" | "null" | number;
const decode = (val: Uint8Array) => new TextDecoder().decode(val);
function filterEmpty(values: string[]): string[]{
return values.filter((val) => val.length !== 0);
}
function validate_quotes(values: string[]): string[] {
let quoted_string = ""
let ignored_quote_list: string[] = []
let flag = false;
for (const value of values){
if (value.startsWith('"') || value.endsWith('"')){
flag=true
}
if (flag) {
quoted_string += " " + value.replace('"','')
} else {
ignored_quote_list.push(value.replaceAll("'",""))
}
}
ignored_quote_list.push(quoted_string.trim())
return ignored_quote_list
}
/** Write simple shell command like a string
*
* cmd("ls .")
*
* Write like a script line by line with await
*
* const { code } = await cmd("ls .");
* console.log(code);
*
* const { code, stdout, stderr } = await cmd("ls .", "piped", "piped");
* console.log(code);
*
**/
export async function cmd(value: string, stdout?: stdout, stderr?: stdout, stdin?: stdin) {
const split = value.split(" ")
const quotes = filterEmpty(validate_quotes(split))
const p = Deno.run(
{ "cmd": quotes, stdout: stdout, stderr: stderr, stdin: stdin},
);
const { code } = await p.status();
if (stdout === "piped" && stderr === "piped") {
const out = await p.output();
const err = await p.stderrOutput();
return { code, stdout: decode(out), stderr: decode(err) };
}
return { code };
}