Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Usage
- `message`: text prompt for the user
- `handler`: function to be called with the entered text

or returns a promise if handler not provided:

#### `promise = prompt(message)`

- `promise`: promise which resolves with the entered text

Example
-------

Expand All @@ -33,6 +39,17 @@ prompt('enter your first name: ', function (val) {
});
});
```
#### with promise:
```js
var first, last;
prompt('enter your first name: ').then(function (val) {
first = val;
return prompt('and your last name: ');
}).then(function (val) {
last = val;
console.log('hi, ' + first + ' ' + last + '!');
});
```

### Password/hidden input

Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var tty = require('tty')
, keypress = require('keypress')
, promisify = require('./promisify')

function prompt (message, hideInput, cb) {
if (typeof hideInput === 'function') {
Expand Down Expand Up @@ -53,7 +54,7 @@ function prompt (message, hideInput, cb) {
var line = '';
process.stdin.on('keypress', listen).resume();
}
module.exports = prompt;
module.exports = promisify(prompt);

function password (message, cb) {
prompt(message, true, function (val) {
Expand All @@ -62,7 +63,7 @@ function password (message, cb) {
else cb(val, function () {}); // for backwards-compatibility, fake end() callback
});
}
module.exports.password = password;
module.exports.password = promisify(password);

function multi (questions, cb) {
var idx = 0, ret = {};
Expand Down Expand Up @@ -113,4 +114,4 @@ function multi (questions, cb) {
prompt(label, q.type === 'password', record);
})();
}
module.exports.multi = multi;
module.exports.multi = promisify(multi);
17 changes: 17 additions & 0 deletions promisify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = function promisify(fn) {
return function() {
if (typeof Promise === 'undefined')
return fn.apply(null, arguments);

var args = [].slice.call(arguments);
var cb = args[args.length - 1];
if (typeof cb !== 'function') {
return new Promise(function(resolve) {
args.push(resolve);
fn.apply(null, args);
});
} else {
return fn.apply(null, args);
}
}
}