Minimal hidden password prompt for Node.js! [npm] [github]
package | dependencies | dependents | total lines of production code |
---|---|---|---|
minimal-password-prompt | 0 | 0 | 48 |
password-prompt | 2 | 43 | 2147 |
- No dependencies
- Multiplatform: tested on Windows, Linux and macOS
- Minimal: Less than 50 lines of code
Seriously, you can also just copy and paste to your project:
const stdin = process.stdin;
const stdout = process.stdout;
const captureStdin = (onData) => {
stdin.setEncoding('utf-8');
stdin.setRawMode(true);
stdin.on('data', onData);
stdin.resume();
};
const releaseStdin = (onData) => {
stdin.pause();
stdin.removeListener('data', onData);
stdin.setRawMode(false);
stdout.write('\n');
};
const prompt = (question, ctrlcExits = true) => (
new Promise((resolve, reject) => {
let input = '';
const onData = (data) => {
switch (data) {
case '\u000A': // \n
case '\u000D': // \r
case '\u0004': // Ctrl D
releaseStdin(onData);
resolve(input);
break;
case '\u0003': // Ctrl C
releaseStdin(onData);
ctrlcExits // exit or raise error
? process.exit()
: reject(new Error('Ctrl C'));
break;
case '\u0008': // Backspace
case '\u007F': // Delete
input = input.slice(0, -1);
break;
default: // any other
input = data;
};
};
captureStdin(onData);
stdout.write(question);
})
);
module.exports = prompt;
(please sill do remember to include MIT license/copyright)
const prompt = require('minimal-password-prompt');
prompt('enter password: ').then(pw => {
console.log(`Password: ${pw}`);
});
or with async/await:
const prompt = require('minimal-password-prompt');
(async () => {
const pw = await prompt('enter password: ');
console.log(`password: ${pw}`);
})();
More examples here
Parameters:
question (String)
prompt output before user inputctrlcExits (Boolean)
, optional- true: Ctrl C exits program (default)
- false: Ctrl C throws error
Returns:
Promise<String>
user input during prompt
Install with npm:
npm install --save minimal-password-prompt