tinmegali
1/9/2020 - 4:01 PM

YO: Recursive prompting on Yeoman

const Generator = require('yeoman-generator');
let answers;

// questions that will be asked recursivelly
const question_property = [
    {
        type: 'input:',
        name: 'input_properties',
        message: 'Property name and type',
        default: 'String property'
    },
    {
        type: 'confirm',
        name: 'another',
        message: 'Add more properties?',
        default: false
    }
];

// array to hold answers
const input_properties = [];

module.exports = class extends Generator {

    // getting input from user
    async prompting() {
        // assure that 'done' is async
        const done = this.async();
        this.answers = await this.prompt(
            question_property
        );
        // add answer to array
        input_properties.push(this.answers.input_properties);

        // check if questions sbould be asked again
        if (this.answers.another) this.prompting();
        else {
            this.log(input_properties);
            done();
        }
    }

    // creating the templates and stuff
    writing() {
        // ... writing part
    }

};