Prompts: Let Your Generator Ask Questions
🎯 Introduction
In this article, you will learn common ways to configure prompts in Plop. Different question types let your generator collect information before it runs, such as filenames, whether to create a style file, and which component type to use.
🧱 The Structure of prompts
prompts is an array of question objects. Plop uses Inquirer for command-line interaction.
A basic question object contains these fields:
{
type: 'input', // Question type
name: 'name', // Variable name for the answer in templates
message: 'Enter a name', // Prompt text shown to the user
default: 'MyComponent' // Default value (optional)
}
The answer is stored under the name key and is available in templates as {{name}}.
✨ Common Prompt Types
input: Text Input
The most common question type, used to collect strings such as names and paths.
{
type: 'input',
name: 'name',
message: 'Enter the component name'
}
confirm: Yes / No
Use this to ask whether an optional file is needed.
{
type: 'confirm',
name: 'hasStyle',
message: 'Create a style file?',
default: true
}
In the template, hasStyle is a boolean. You can use it directly with {{#if hasStyle}}.
list: Single Selection
Use this to let the user select one item from a fixed set of options.
{
type: 'list',
name: 'type',
message: 'Select a component type',
choices: ['Base component', 'Business component', 'Layout component'],
default: 'Base component'
}
checkbox: Multiple Selection
Use this to let the user select multiple options at once.
{
type: 'checkbox',
name: 'features',
message: 'Select the files to generate',
choices: ['script', 'style', 'test'],
default: ['script', 'style']
}
checkbox returns an array. In templates, iterate over it with {{#each features}}.🛠 Putting It Together: Asking Multiple Questions
Let's build a more practical generator that asks for the component name, component type, and whether to include styles:
export default function (plop) {
plop.setGenerator('component', {
description: 'Create a Vue component',
prompts: [
{
type: 'input',
name: 'name',
message: 'Enter the component name'
},
{
type: 'list',
name: 'type',
message: 'Select a component type',
choices: ['base', 'business', 'layout'],
default: 'base'
},
{
type: 'confirm',
name: 'hasStyle',
message: 'Create a style file?',
default: true
}
],
actions: [
{
type: 'add',
path: 'src/components/{{pascalCase name}}.vue',
templateFile: 'plop-templates/component.hbs'
}
]
});
}
Template file:
<template>
<div class="{{kebabCase name}}">
{{pascalCase name}} component
</div>
</template>
<script setup>
// Component type: {{type}}
</script>
{{#if hasStyle}}
<style scoped>
.{{kebabCase name}} {
/* {{type}} component styles */
}
</style>
{{/if}}
If you choose to include styles, the generated file contains a <style> block; otherwise, it does not.
⚡ Default Values and Validation
default: Providing a Default Answer
{
type: 'input',
name: 'name',
message: 'Enter the component name',
default: 'MyComponent'
}
Pressing Enter without typing a value uses MyComponent.
validate: Validating Input
{
type: 'input',
name: 'name',
message: 'Enter the component name',
validate: function (value) {
if (value.trim() === '') {
return 'Component name cannot be empty';
}
if (!/^[A-Z][a-zA-Z0-9]*$/.test(value)) {
return 'Component name must start with an uppercase English letter';
}
return true;
}
}
If validation returns a string, Plop displays it as an error message. Returning true means the input is valid.
💡 Showing Questions Based on Previous Answers
Use when to show a question only when a condition is met.
{
type: 'confirm',
name: 'hasTest',
message: 'Generate a test file?',
default: false
},
{
type: 'input',
name: 'testName',
message: 'Enter the test filename',
when: function (answers) {
return answers.hasTest;
}
}
The test filename question appears only if the user chooses to generate a test file.
❓ Questions and Answers
Q1: What does the name field do?
name is the variable name for the answer. The user's response is stored under that name and accessed in templates with {{name}}.
Q2: What does a confirm prompt return?
It returns true or false. You can use it directly in Handlebars conditions such as {{#if hasStyle}}.
Q3: How do list and checkbox differ?
list allows one selection and returns a string; checkbox allows multiple selections and returns an array.
Q4: What do a string and true mean when returned by validation?
A string indicates validation failed and is displayed as an error message. Returning true indicates validation passed.
🧪 Exercise
Add a question to the component generator asking whether to generate a props definition.
{
type: 'confirm',
name: 'hasProps',
// Complete message and default here
}
Then use the answer in component.hbs:
{{#if hasProps}}
<script setup>
// Add the props definition here
</script>
{{/if}}
🎉 You now know how to configure interactive prompts in Plop! Next, we will explore actions and the operations a generator can perform.
