Skip to content

Instantly share code, notes, and snippets.

@matteo-bombelli
Created September 26, 2016 00:24
Show Gist options
  • Save matteo-bombelli/f4c8bc17ec83a12cb0de058016aa1f8f to your computer and use it in GitHub Desktop.
Save matteo-bombelli/f4c8bc17ec83a12cb0de058016aa1f8f to your computer and use it in GitHub Desktop.
Hacky solution: Yeoman generator nested prompt...
'use strict';
var yeoman = require('yeoman-generator');
var chalk = require('chalk');
var yosay = require('yosay');
module.exports = yeoman.Base.extend({
prompting: function () {
// Have Yeoman greet the user.
this.log(yosay(
'Welcome to the amazing ' + chalk.red('generator-try-mb') + ' generator!'
));
var prompts = [{
type: 'checkbox',
name: 'someAnswer',
message: 'select languages',
choices: ["en","it","de","fr","es","pt","ru"],
default: []
}];
var questionary = this;
return this.prompt(prompts).then(function (props) {
// To access props later use this.props.someAnswer;
/*
if(props.someAnswer.length>0){
var questions2=[];
var i=0;
for(i=0; i<props.someAnswer.length; i++){
questions2[i] = {
type:'input',
name:'two'+props.someAnswer[i],
message:'translation of "this thing" in '+props.someAnswer[i],
default: ""
};
}
this.prompt(questions2).then(function(props2){
this.props2=props2;
}.bind(this));
}
*/
this.props = props;
}.bind(this));
},
configuring: function (){
},
promptingSecond:function(){
// this should fall in the default group
var props = this.props;
if(props.someAnswer.length>0){
var questions2=[];
var i=0;
for(i=0; i<props.someAnswer.length; i++){
questions2[i] = {
type:'input',
name:'two'+props.someAnswer[i],
message:'translation of "this thing" in '+props.someAnswer[i],
default: ""
};
}
return this.prompt(questions2).then(function(props2){
this.props2 = props2;
}.bind(this));
}
},
conflicts: function (){
},
writing: function () {
this.log(JSON.stringify(this.props));
this.log(JSON.stringify(this.props2));
this.fs.copy(
this.templatePath('dummyfile.txt'),
this.destinationPath('dummyfile.txt')
);
},
install: function () {
// this.installDependencies();
},
end: function(){
this.log(" bye! ");
return this;
}
});
@matteo-bombelli
Copy link
Author

matteo-bombelli commented Sep 26, 2016

I've created a "promptingSecond" (line 51) task, that will fall after prompting, cause it will fall in the default group but i don't really like it cause it can be strange if there are nested loops questions like...

how many elements?
how many thing in the first element?
name of the 1.1: ___
name of the 1.2: ___ 
...

how many thing in the second element?
name of the 2.1: ___
name of the 2.2: ___ 
...

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment