Created
June 4, 2024 00:18
-
-
Save bozdoz/4136cf0bbb072ec72c28b60c2c9f7ed5 to your computer and use it in GitHub Desktop.
Get a password from stdin
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
const readline = require("node:readline"); | |
const { Writable } = require("node:stream"); | |
const getPassword = (user) => | |
new Promise((resolve) => { | |
const mutableStdout = new Writable({ | |
write: function (chunk, encoding, callback) { | |
if (!this.muted) process.stdout.write(chunk, encoding); | |
callback(); | |
}, | |
}); | |
mutableStdout.muted = false; | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: mutableStdout, | |
terminal: true, | |
}); | |
rl.question(`Password for [${user}]: `, function (password) { | |
rl.close(); | |
resolve(password); | |
}); | |
mutableStdout.muted = true; | |
}); | |
module.exports = { | |
getPassword, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment