Skip to content

Instantly share code, notes, and snippets.

@p32929
Last active December 4, 2022 09:58
Show Gist options
  • Save p32929/fb72afb606f82cd3e353280dc8ea7882 to your computer and use it in GitHub Desktop.
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
// @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