Skip to content

Commit 8d5495c

Browse files
authored
Merge pull request #196 from caub/promisify
promisify prompt.get
2 parents 0ff93b6 + 33ddf56 commit 8d5495c

4 files changed

Lines changed: 39 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules/
22
node_modules/*
3-
npm-debug.log
3+
npm-debug.log
4+
5+
package-lock.json

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ This will result in the following command-line output:
4646
email: some-user@some-place.org
4747
```
4848

49+
If no callback is passed to `prompt.get(schema)`, then it returns a `Promise`, so you can also write:
50+
```js
51+
const {username, email} = await prompt.get(['username', 'email']);
52+
```
53+
54+
4955
### Prompting with Validation, Default Values, and More (Complex Properties)
5056
In addition to prompting the user with simple string prompts, there is a robust API for getting and validating complex information from a command-line prompt. Here's a quick sample:
5157

lib/prompt.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ prompt.history = function (search) {
192192
// Gets input from the user via stdin for the specified message(s) `msg`.
193193
//
194194
prompt.get = function (schema, callback) {
195+
if (typeof callback === 'function') return prompt._get(schema, callback);
196+
197+
return new Promise(function (resolve, reject) {
198+
prompt._get(schema, function (err, result) {
199+
return err ? reject(err) : resolve(result);
200+
});
201+
});
202+
};
203+
204+
prompt._get = function (schema, callback) {
195205
//
196206
// Transforms a full JSON-schema into an array describing path and sub-schemas.
197207
// Used for iteration purposes.

test/prompt-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ var assert = require('assert'),
1212
macros = require('./macros'),
1313
schema = helpers.schema;
1414

15+
// fix for vows, util.print/puts was removed from node
16+
require('util').print = console.log;
17+
require('util').puts = console.log;
18+
1519
// A helper to pass fragments of our schema into prompt as full schemas.
1620
function grab () {
1721
var names = [].slice.call(arguments),
@@ -751,6 +755,22 @@ vows.describe('prompt').addBatch({
751755
}
752756
}
753757
}
758+
}).addBatch({
759+
"when using prompt": {
760+
"the get() method also works as a Promise": {
761+
topic: function () {
762+
var that = this;
763+
764+
prompt.override = { xyz: 468, abc: 123 };
765+
prompt.get(['xyz', 'abc'])
766+
.then(function (result) { return that.callback(null, result); });
767+
},
768+
"should respond with overrides": function (err, results) {
769+
assert.isNull(err);
770+
assert.deepEqual(results, { xyz: 468, abc: 123 });
771+
}
772+
}
773+
}
754774
}).addBatch({
755775
"When using prompt": {
756776
"with fancy properties": {

0 commit comments

Comments
 (0)