Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to do a while loop for prompted input questions, bind them and have all answers available in an array?

I am building a Yeoman generator and dependencies needed for this come from https://github.com/sboudrias/mem-fs-editor#copytplfrom-to-context-settings and https://github.com/SBoudrias/Inquirer.js/

The idea is to be able to ask the user a question and repeat the same question i.e. would you like to add another... if the user adds another then it will bind and record that answer and if the user says 'no' or hits return the prompt will stop.

I would like to then have all of the answers binded to an arrary that can be passed to another object function so that it can list out the responses as an array.

Here is the code so far... First is the prompt:

askForTest1: function () {
    if (this.type == 'foundation5') {
        var cb = this.async();

        var prompts = {
            type: 'input',
            name: 'test1',
            message: chalk.yellow('  What is your favorite movie'),
            default: 'Star Wars!'
        };

        this.prompt(prompts, function (props) {
            this.templatedata.test1 = props.test1;

            cb();
        }.bind(this));
    }
},

Then is the copyTpl object that will bind options for template builds: This is the desired output I would like to occur... and keep in mind this copy tpl lives in the same index.js file as the prompts. i.e. this template...

       this.fs.copyTpl(
              this.templatePath('/index2.html'),
              this.destinationPath('app/index2.html'),
              { title: [this.templatedata.test1-a, this.templatedata.test1-b, this.templatedata.test1-c, ...], h1: this.applicationName }
            );

as a result... a template with this code...

using

will produce this...

using foo1
using foo2

is this possible and how would I go about doing this.

like image 873
Christian Matthew Avatar asked Apr 11 '15 00:04

Christian Matthew


2 Answers

This is fairly simple programming task.

Use recursion of a method asking the question to the user. Then if the user answered "yes add more", you just call the same function again.

Something roughly like that:

initializing: function () {
  this.movies = [];
},

askMovie: function (cb) {
  cb = cb || this.async();

  var prompts = [{
      type: 'input',
      name: 'movie',
      message: chalk.yellow('  What is your favorite movie'),
      default: 'Star Wars!'
  }, {
    type: 'confirm',
    name: 'askAgain',
    message: 'ask again?'
  }];

  this.prompt(prompts, function (props) {
    this.movies.push(props.movie)
    if (props.askAgain) {
      this.askMovie(cb);
    } else {
      cb();
    }
  }.bind(this));
  }
}
like image 136
Simon Boudrias Avatar answered Nov 17 '22 16:11

Simon Boudrias


Something like this should work for you:

function() {
    var answers = {
        times: [],
        title: undefined,
        type: undefined
    };

    function promptMe(prompt, cb) {
        self.prompt(prompt, function (props) {
            if(props.answer!= "done") {
                answers.times.push(props.time);
                promptMe(prompt, cb);
            } else {
                cb();
            }
        });
    }

    var cb = this.async(),
        self = this,
        movieTypePrompt = {
            type: 'input',
            name: 'movieType',
            message: chalk.yellow('What is your favorite type of movie?'),
            default: 'Action'
        },
        movieTitilePrompt = {
            type: 'input',
            name: 'movieTitle',
            message: chalk.yellow('What is your favorite movie?'),
            default: 'Tron: Legacy'
        }
        movieTimePrompt = {
            type: 'input',
            name: 'time',
            message: chalk.yellow('When can you watch a movie? (enter \'done\' when you are done entering times)'),
            default: '8PM'
        };

    //Prompt for movie type and title
    this.prompt([movieTypePrompt, movieTitlePrompt], function (props) {
        answers.title = props.movieTitle;
        answers.type = props.movieType;

        //Repeatedly prompt for movie times
        promptMe(moviePrompt, function() {
            console.log('done');

            cb();
        });
    }.bind(this));
}
like image 4
Andrew Koroluk Avatar answered Nov 17 '22 18:11

Andrew Koroluk