Skip to content
Myr

Added `Command#password(str, fn)`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent d4a9c40 · Browse files · 2 files+61 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+61 added−7 removed

  • other:+61 added−7 removed

2 files+61 added−7 removed

added:examples/password+15 added
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89program.password('Password: ', function(pass){10 console.log('got "%s"', pass);11 program.prompt('Username: ', function(name){12 console.log('got "%s"', name.trim());13 process.stdin.destroy();14 });15});
modified:lib/commander.js+46 added-7 removed
9 unmodified lines
10 */1112var EventEmitter = require('events').EventEmitter13 , path = require('path')14 , tty = require('tty')15 , basename = path.basename;1617/**18 * Expose the root command.
680 unmodified lines
699 *700 *701 * Examples:702 *702 * program.prompt('Username: ', function(name){703 * console.log('hi %s', name);704 * });703 * program.prompt('Username: ', function(name){704 * console.log('hi %s', name);705 * });706 * 707 * program.prompt('Description:', function(desc){708 * console.log('description was "%s"', desc.trim());709 * });710 *706 * program.prompt('Description:', function(desc){707 * console.log('description was "%s"', desc.trim());708 * });709 *711 * @param {String} str712 * @param {Function} fn713 * @api public714 */
3 unmodified lines
718 this.promptMultiLine(str, fn);719};720721/**722 * Prompt for password with `str` and callback `fn(val)`.723 *724 * Examples:725 *726 * program.password('Password: ', function(pass){727 * console.log('got "%s"', pass);728 * process.stdin.destroy();729 * });730 *731 * @param {String} str732 * @param {Function} fn733 * @api public734 */735736Command.prototype.password = function(str, fn){737 tty.setRawMode(true);738 var buf = '';739 process.stdout.write(str);740 process.stdin.on('keypress', function(c, key){741 if ('enter' == key.name) {742 console.log();743 process.stdin.removeAllListeners('keypress');744 tty.setRawMode(false);745 fn(buf);746 return;747 }748749 if (key && key.ctrl && 'c' == key.name) {750 console.log('%s', buf);751 process.exit();752 }753754 process.stdout.write('*');755 buf += c;756 }).resume();757};758759/**760 * Confirmation prompt with `str` and callback `fn(bool)`761 *762 * Examples:763 *
85 unmodified lines