dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.63k stars 278 forks source link

Can't prompt for more input when running action selected in CLI options #279

Open soletan opened 6 years ago

soletan commented 6 years ago

Using code provided in https://github.com/dthree/vorpal/issues/171#issuecomment-298230095 I can't use this.prompt() to ask user additional things. Obviously this is due to ui.js requiring to have some parent which is missing then.

According to the template given in comment linked before, when I write an action like this:

Vorpal
    .command( "some-cmd", "some command" )
    .action( function( args, cb ) {
        Promise.resolve( this.prompt( [ {
            type: "string",
            message: "enter your name: ",
            name: "username",
        } ] )
            .then( repl ? cb : null );
    } );

the prompt is showing up when running in REPL mode, but it's omitted when running script with some-cmd selected on CLI like this:

$ node myscript.js some-cmd

I was able to fix this issue by explicitly assigning current command's parent to the UI prior to prompting but I bet this isn't safe due to monkey-patching some more static part of Vorpal from a more instance-like part of code.

Vorpal
    .command( "some-cmd", "some command" )
    .action( function( args, cb ) {
        if ( !repl && !Vorpal.ui.parent ) {
            Vorpal.ui.parent = this.parent;
        }

        Promise.resolve( this.prompt( [ {
            type: "string",
            message: "enter your name: ",
            name: "username",
        } ] )
            .then( repl ? cb : null );
    } );
milesj commented 6 years ago

Have you tried returning the promise from action instead of relying on the callback?

soletan commented 6 years ago

Using callback is actually required due to #171 as callback isn't to be used in every case. In addition the Promise.resolve() is used here for this.prompt() is returning undefined w/o the fix resulting in a ReferenceError.