Skip to content

Instantly share code, notes, and snippets.

@bozdoz
Created June 4, 2024 00:18
Show Gist options
  • Save bozdoz/4136cf0bbb072ec72c28b60c2c9f7ed5 to your computer and use it in GitHub Desktop.
Save bozdoz/4136cf0bbb072ec72c28b60c2c9f7ed5 to your computer and use it in GitHub Desktop.
Get a password from stdin
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