Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

promisify prompt.get #196

Merged
merged 2 commits into from
Mar 5, 2020
Merged
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/
node_modules/*
npm-debug.log
npm-debug.log

package-lock.json
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ This will result in the following command-line output:
email: [email protected]
```

If no callback is passed to `prompt.get(schema)`, then it returns a `Promise`, so you can also write:
```js
const {username, email} = await prompt.get(['username', 'email']);
```


### Prompting with Validation, Default Values, and More (Complex Properties)
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:

Expand Down
10 changes: 10 additions & 0 deletions lib/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ prompt.history = function (search) {
// Gets input from the user via stdin for the specified message(s) `msg`.
//
prompt.get = function (schema, callback) {
if (typeof callback === 'function') return prompt._get(schema, callback);

return new Promise(function (resolve, reject) {
prompt._get(schema, function (err, result) {
return err ? reject(err) : resolve(result);
});
});
};

prompt._get = function (schema, callback) {
//
// Transforms a full JSON-schema into an array describing path and sub-schemas.
// Used for iteration purposes.
Expand Down
20 changes: 20 additions & 0 deletions test/prompt-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ var assert = require('assert'),
macros = require('./macros'),
schema = helpers.schema;

// fix for vows, util.print/puts was removed from node
require('util').print = console.log;
require('util').puts = console.log;

// A helper to pass fragments of our schema into prompt as full schemas.
function grab () {
var names = [].slice.call(arguments),
Expand Down Expand Up @@ -751,6 +755,22 @@ vows.describe('prompt').addBatch({
}
}
}
}).addBatch({
"when using prompt": {
"the get() method also works as a Promise": {
topic: function () {
var that = this;

prompt.override = { xyz: 468, abc: 123 };
prompt.get(['xyz', 'abc'])
.then(function (result) { return that.callback(null, result); });
},
"should respond with overrides": function (err, results) {
assert.isNull(err);
assert.deepEqual(results, { xyz: 468, abc: 123 });
}
}
}
}).addBatch({
"When using prompt": {
"with fancy properties": {
Expand Down