File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11node_modules /
22node_modules /*
3- npm-debug.log
3+ npm-debug.log
4+
5+ package-lock.json
Original file line number Diff line number Diff 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)
5056In 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
Original file line number Diff line number Diff 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//
194194prompt . 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.
Original file line number Diff line number Diff 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.
1620function 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" : {
You can’t perform that action at this time.
0 commit comments