-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.ts
62 lines (56 loc) · 1.56 KB
/
io.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
import { io } from "./deps.ts";
/** Obtain a boolean confirmation - present a message
* @param msg - Message presented
* @param defValue - default boolean
* @param source - value to read from (stdin)
*/
export async function confirmation(
msg: string,
defValue: boolean,
source: Deno.Reader = Deno.stdin,
): Promise<boolean> {
console.log(`${msg} (${defValue ? "Y/n" : "y/N"}):`);
const br = new io.BufReader(source);
const resp = await br.readString("\n");
if (resp === null) {
return defValue;
}
return resp.startsWith("Y") || resp.startsWith("y");
}
/** Write string content to Writer and close */
export async function writeAllClose(
content: string,
dest: Deno.Writer & Deno.Closer,
): Promise<void> {
const encoder = new TextEncoder();
const buf = encoder.encode(content);
await io.writeAll(dest, buf);
dest.close();
}
/** Copy between source and destination Reader/Writer - close both */
export async function copyAllClose(
src: Deno.Reader & Deno.Closer,
dst: Deno.Writer & Deno.Closer,
options?: {
bufSize?: number;
},
): Promise<number> {
let totalCopiedCount = 0;
let copyCount = 0;
do {
copyCount = await Deno.copy(src, dst, options);
totalCopiedCount += copyCount;
} while (copyCount > 0);
src.close();
dst.close();
return totalCopiedCount;
}
/** Read all from Reader and close */
export async function readAllClose(
reader: Deno.Reader & Deno.Closer,
): Promise<string> {
const decoder = new TextDecoder();
const buf = await io.readAll(reader);
reader.close();
return decoder.decode(buf);
}