Skip to content

Instantly share code, notes, and snippets.

@iegik
Last active November 17, 2022 10:24
Show Gist options
  • Save iegik/f1979ba0eee5b21ef4ac506e83d6407f to your computer and use it in GitHub Desktop.
Save iegik/f1979ba0eee5b21ef4ac506e83d6407f to your computer and use it in GitHub Desktop.

Node.JS

Install

brew unlink node
brew unlink npm
brew install node@16
brew link --overwrite node@16
const exitOnCtrlC = (key) => key === '\u0003' && process.exit();
const prompt = async (message = '', options) => new Promise((resolve, reject) => {
let line = '';
const {
isSecured = false,
show = true,
submitKey = '\u000D',
cancelKey = '\u001b',
} = options;
const onEnd = () => {
process.stdout.write('\n');
process.stdin.end();
process.stdin.removeListener('data', onData);
}
const onData = (key) => {
if (key === submitKey) {
onEnd();
resolve(line)
return;
}
if (key === cancelKey) {
onEnd();
reject()
return;
}
line += key === '\u000D' ? '\n' : key;
if (show) process.stdout.write(isSecured ? '*' : key);
}
process.stdout.write(message);
process.stdin.on('data', onData);
})
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
process.stdin.setEncoding( 'utf8' );
process.stdin.on('data', exitOnCtrlC);
prompt('Enter password: ', { isSecured: true, show: false }).then((data) => {
console.log('Password is:', data);
console.log('Press any key to exit...');
process.stdin.on('data', () => process.exit());
}).catch(() => {
console.log('Terminated');
console.log('Press any key to exit...');
process.stdin.on('data', () => process.exit());
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment