Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add interactive mode to our test runner
- Loading branch information
1 parent
edcc653
commit 0ec733b
Showing
4 changed files
with
72 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Adapted from https://github.com/tcql/node-yesno/blob/master/yesno.js | ||
|
||
import * as readline from "readline"; | ||
|
||
const options = { | ||
yes: ["yes", "y"], | ||
no: ["no", "n"], | ||
}; | ||
|
||
export async function ask(question: string): Promise<boolean> { | ||
const yValues = options.yes.map((v) => v.toLowerCase()); | ||
const nValues = options.no.map((v) => v.toLowerCase()); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
return new Promise(function (resolve) { | ||
rl.question(question + " ", async function (answer) { | ||
rl.close(); | ||
|
||
const cleaned = answer.trim().toLowerCase(); | ||
|
||
if (yValues.indexOf(cleaned) >= 0) return resolve(true); | ||
|
||
if (nValues.indexOf(cleaned) >= 0) return resolve(false); | ||
|
||
process.stdout.write("\nInvalid Response.\n"); | ||
process.stdout.write( | ||
"Answer either yes : (" + yValues.join(", ") + ") \n", | ||
); | ||
process.stdout.write("Or no: (" + nValues.join(", ") + ") \n\n"); | ||
const result = await ask(question); | ||
resolve(result); | ||
}); | ||
}); | ||
} |