| 10 | * @param processRequest Async callback function that is invoked for each interactive input or each line in text file. |
| 11 | */ |
| 12 | export async function processRequests(interactivePrompt: string, inputFileName: string | undefined, processRequest: (request: string) => Promise<void>) { |
| 13 | if (inputFileName) { |
| 14 | const lines = fs.readFileSync(inputFileName).toString().split(/\r?\n/); |
| 15 | for (const line of lines) { |
| 16 | if (line.length) { |
| 17 | console.log(interactivePrompt + line); |
| 18 | await processRequest(line); |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | else { |
| 23 | const stdio = readline.createInterface({ input: process.stdin, output: process.stdout }); |
| 24 | while (true) { |
| 25 | const input = await stdio.question(interactivePrompt); |
| 26 | if (input.toLowerCase() === "quit" || input.toLowerCase() === "exit") { |
| 27 | break; |
| 28 | } |
| 29 | else if (input.length) { |
| 30 | await processRequest(input); |
| 31 | } |
| 32 | } |
| 33 | stdio.close(); |
| 34 | } |
| 35 | } |