Always Twisted

This page is old.

Although the content of this page may be useful. The page was part of a migratian and may look different and unmaintained. I am working my way through, tidying them up.

πŸ™πŸ–€

Using Lists In A Yeoman Generator

On This Page:

For a large project I’ve been working on with Monotype I recently, through some requirement changes, needed to create a list of options in Yeoman Generator. After some quick googling I found that the best option for me was to use the list type available.

Further reading various Stack Overflow questions and answers, examples from the inquirer.js repo and any documentation I was a little flummoxed.

Every example I could find gave simple one-word options. This works when choosing what size of pizza you require:

Code languagejs
var prompts = [{
type: 'list',
name: 'features',
message: 'What do you want to make today?',
choices: [ 'Jumbo', 'Large', 'Standard', 'Medium', 'Small' ]
}];
this.prompt(prompts, function () {
// do things with the answer here
}.bind(this));

For this project I needed a more descriptive explanation of what they were about to install. Was it a project for a new typeface specimen? A new email project? A quick prototype? We needed to be able to be verbose with out list of choices.

Code languagejs
var prompts = [{
type: 'list',
name: β€˜project’,
message: 'What type of project do you want to create today?',
choices: [
β€œa new email campaignβ€œ,
β€œa low fidelity prototypeβ€œ,
β€œa new typeface specimenβ€œ,
β€œa new landing pageβ€œ,
β€œa new bespoke article”
]
}];
this.prompt(prompts, function () {
// do things with the answer here
}.bind(this));

Looking at this and how the simpler example had one-word answers I thought this would be a problem when it came to assigning the choice made to various options that may or may be required.

I searched the documentation, various blogposts and Stack Overflow (again) for an answer and then I tried something.

Taking a look at the gulp web app generator from the Yeoman Github organisation I looked at how they had used the checkbox type from inquirer.js –

Code languagejs
var prompts = [{
type: 'checkbox',
name: 'features',
message: 'What more would you like?',
choices: [{
name: 'Sass',
value: 'includeSass',
checked: true
}, {
name: 'Bootstrap',
value: 'includeBootstrap',
checked: true
}, {
name: 'Modernizr',
value: 'includeModernizr',
checked: true
}]
}, {
//
}
}];

Here we had name, value and checked. Well I knew I didn’t need checked so I tried using name and value. It worked.

Code languagejs
var prompts = [{
type: 'list',
name: 'features',
message: 'What do you want to make today?',
choices: [{
name: 'a new email campaign',
value: 'includeEmail'
},{
name: 'a low fidelity prototype',
value: 'includePrototype'
},{
name: 'a new typeface specimen',
value: 'includeSpecimen'
}
//
]
}];

Now, using name we can have a verbose, descriptive option and use it’s value when generating a new project to include the relevant files, bits of HTML or CSS, images, fonts etc.

So now, when we are in a terminal window and Yo the generator we get a nice list to choose one option from.

Wondering how to output tokens into different formats for different platforms?

I can configure tools to output tokens into the formats your teams need.

get in touch!