UNPKG

stratumn-cli

Version:
46 lines (40 loc) 1.14 kB
// http://stackoverflow.com/questions/24037545/how-to-hide-password-in-the-nodejs-console function hidden(rl, query, cb) { function handleData(c) { const char = c + ''; switch (char) { case '\n': case '\r': case '\u0004': break; default: process.stdout.write('\x1B[2K\x1B[200D' + query + Array(rl.line.length + 1).join('*')); break; } } process.stdin.on('data', handleData); rl.question(query, value => { /*eslint-disable */ rl.history = rl.history.slice(1); /*eslint-enable */ process.stdin.removeListener('data', handleData); cb(value); }); } export default function question(rl, query, name, store = {}, opts = {}) { return new Promise(resolve => { const ask = opts.hidden ? hidden.bind(null, rl) : rl.question.bind(rl); ask(query, answer => { if (opts.required !== false && !answer) { question(rl, query, name, store, opts).then(resolve); return; } if (answer) { /*eslint-disable */ store[name] = answer; /*eslint-enable */ } resolve(answer); }); }); }