Actions: What Your Generator Can Do
🎯 Introduction
In this article, you will learn Plop's most common actions: add to create files, modify to update files, and addMany to create multiple files. Actions are the execution engine of every generator.
🧱 The Structure of actions
actions is an array in which each item describes an operation to perform. Plop executes them in order.
actions: [
{
type: 'add',
path: 'src/components/{{pascalCase name}}.vue',
templateFile: 'plop-templates/component.hbs'
}
]
Every action has at least a type field, which determines its behavior.
✨ add: Creating a Single File
add is the most common action. It creates a new file from a template.
{
type: 'add',
path: 'src/components/{{pascalCase name}}.vue',
templateFile: 'plop-templates/component.hbs'
}
| Field | Description |
|---|---|
path | Output file path; supports template variables |
templateFile | Path to the template file |
template | An inline template string (use this or templateFile) |
If you prefer not to maintain a separate template file, use template directly:
{
type: 'add',
path: 'src/components/{{pascalCase name}}.vue',
template: '<template>\n <div>{{name}}</div>\n</template>\n'
}
template strings suit simple snippets of one or two lines.✨ modify: Updating an Existing File
modify finds and replaces content in an existing file. It is useful for adding entries to an existing index or configuration.
{
type: 'modify',
path: 'src/router/index.js',
pattern: /\/\/ Import routes here/,
template: "import {{pascalCase name}} from '@/views/{{pascalCase name}}.vue';\n// Import routes here"
}
This example inserts an import statement immediately before the // Import routes here line.
| Field | Description |
|---|---|
path | Path to the file to modify |
pattern | A regular expression or string that matches the replacement location |
template | Replacement content |
templateFile | Read replacement content from a file instead |
pattern for modify. If it does not match, the original file remains unchanged. Test on a copy first.✨ addMany: Creating Multiple Files
Use addMany to simplify configuration when generating several related files at once.
{
type: 'addMany',
destination: 'src/components/{{pascalCase name}}',
templateFiles: 'plop-templates/component/**'
}
Suppose plop-templates/component/ contains these files:
plop-templates/component/
├── index.vue
├── index.js
└── style.scss
Running the action generates the corresponding index.vue, index.js, and style.scss files under src/components/UserInfo/.
| Field | Description |
|---|---|
destination | Output directory |
templateFiles | Template file matching pattern; supports wildcards |
base | Directory prefix to remove from template paths |
component directory level from the generated paths, add base: 'plop-templates/component'.🛠 Putting It Together: Component + Styles + Route Registration
The following generator performs three tasks:
- Create a component file.
- Create a style file.
- Automatically import the new page in the router file.
export default function (plop) {
plop.setGenerator('page', {
description: 'Create a page and register its route',
prompts: [
{
type: 'input',
name: 'name',
message: 'Enter the page name'
}
],
actions: [
{
type: 'add',
path: 'src/views/{{pascalCase name}}Page.vue',
templateFile: 'plop-templates/page.hbs'
},
{
type: 'add',
path: 'src/views/{{pascalCase name}}Page.scss',
templateFile: 'plop-templates/page.scss.hbs'
},
{
type: 'modify',
path: 'src/router/index.js',
pattern: /\/\/ Import pages here/,
template: "import {{pascalCase name}}Page from '@/views/{{pascalCase name}}Page.vue';\n// Import pages here"
}
]
});
}
path and template support Handlebars variables. You can render the same name in different formats using pascalCase, kebabCase, camelCase, and other helpers.⚡ abortOnFail: Stopping After an Action Fails
By default, Plop stops subsequent actions if an action fails. Use abortOnFail to control this behavior.
{
type: 'add',
path: 'src/components/{{pascalCase name}}.vue',
templateFile: 'plop-templates/component.hbs',
abortOnFail: false
}
When set to false, Plop continues with subsequent actions even if this action fails.
💡 The skip Function: Deciding Whether to Run an Action
Each action can have a skip function that decides whether to skip it based on the user's answers.
{
type: 'add',
path: 'src/components/{{pascalCase name}}.test.js',
templateFile: 'plop-templates/test.hbs',
skip: function (answers) {
if (!answers.hasTest) {
return 'Skipping test file generation';
}
}
}
If skip returns a string, the action is skipped and that message is displayed.
❓ Questions and Answers
Q1: How do template and templateFile differ in an add action?
template provides a template string directly, while templateFile reads the template from a file. Prefer templateFile for complex templates.
Q2: Does modify overwrite the entire file?
No. It replaces only the content matched by pattern, leaving the rest unchanged.
Q3: How do destination in addMany and path differ?
add uses path to specify a single file. addMany uses destination to specify an output directory and generates multiple files from a template directory.
Q4: Are actions executed in order?
Yes. Actions run in the order they appear in the actions array.
🧪 Exercise
Complete the following configuration to generate a test file with the same base name alongside the component:
{
type: 'add',
path: 'src/components/{{pascalCase name}}.vue',
templateFile: 'plop-templates/component.hbs'
},
// Add an action here to generate src/components/{{pascalCase name}}.test.js
🎉 You now know Plop's core actions! Next, we will explore template syntax and how Handlebars works within Plop.
