Last active
December 4, 2022 09:58
-
-
Save p32929/fb72afb606f82cd3e353280dc8ea7882 to your computer and use it in GitHub Desktop.
Simple way to ask prompts in a shell/terminal/cmd in nodejs typescript
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
// @ts-ignore | |
const readline = require("readline") | |
export class Question { | |
static ask(questionTexts: string, callback?: (answer: string) => void): Promise<string> { | |
return new Promise((resolve) => { | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}) | |
rl.question(questionTexts, (answer) => { | |
if (callback) { | |
callback(answer) | |
} | |
resolve(answer) | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment