-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathinput.ts
36 lines (30 loc) · 919 Bytes
/
input.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
import fs from "fs";
import path from "path";
import readline from "readline";
export function fromStdin(callback: (value: string) => void) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "fluent>",
});
let lines: Array<string> = [];
rl.on("line", line => lines.push(line));
rl.on("close", () => callback(lines.join("\n") + "\n"));
}
export function fromFile(filePath: string) {
return fs.readFileSync(filePath, "utf8");
}
export function* files(destination: string, ext: string) {
let files;
if (destination.endsWith(ext)) {
files = [destination];
} else {
files = fs
.readdirSync(destination)
.filter(filename => filename.endsWith(ext))
.map(filename => path.join(destination, filename));
}
for (let file of files) {
yield file;
}
}