Actions: What Your Generator Can Do

Learn Plop's core actions, including add, modify, and addMany, to create, modify, and generate multiple files.

🎯 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'
}
FieldDescription
pathOutput file path; supports template variables
templateFilePath to the template file
templateAn 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 files work better for complex content, while inline 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.

FieldDescription
pathPath to the file to modify
patternA regular expression or string that matches the replacement location
templateReplacement content
templateFileRead replacement content from a file instead
Take care when writing 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/.

FieldDescription
destinationOutput directory
templateFilesTemplate file matching pattern; supports wildcards
baseDirectory prefix to remove from template paths
To omit the 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:

  1. Create a component file.
  2. Create a style file.
  3. Automatically import the new page in the router file.
plopfile.mjs
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"
            }
        ]
    });
}
Both 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.