
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Plop is a micro-generator framework that helps you create consistent and repeatable code snippets, files, or projects. It allows you to define templates and automate the creation of boilerplate code, making it easier to maintain consistency across your codebase.
File Generation
This feature allows you to generate files based on templates. In this example, a new component file is created using a Handlebars template. The user is prompted for the component name, and the file is generated in the specified path.
module.exports = function (plop) {
plop.setGenerator('component', {
description: 'Create a new component',
prompts: [
{
type: 'input',
name: 'name',
message: 'Component name?'
}
],
actions: [
{
type: 'add',
path: 'src/components/{{pascalCase name}}/{{pascalCase name}}.js',
templateFile: 'plop-templates/component.hbs'
}
]
});
};
Custom Actions
Plop allows you to define custom actions that can be executed as part of the generation process. In this example, a custom action type is defined and used in a generator.
module.exports = function (plop) {
plop.setActionType('customAction', function (answers, config, plop) {
// Custom action logic here
return 'Custom action executed';
});
plop.setGenerator('custom', {
description: 'Run a custom action',
prompts: [],
actions: [
{
type: 'customAction'
}
]
});
};
Template Rendering
This feature allows you to render templates with dynamic data. In this example, a README file is generated using a Handlebars template, with the project name provided by the user.
module.exports = function (plop) {
plop.setGenerator('readme', {
description: 'Create a README file',
prompts: [
{
type: 'input',
name: 'projectName',
message: 'Project name?'
}
],
actions: [
{
type: 'add',
path: 'README.md',
templateFile: 'plop-templates/readme.hbs'
}
]
});
};
Yeoman Generator is a scaffolding tool that allows you to create custom generators for project templates. It is more feature-rich and has a larger ecosystem compared to Plop, but it can be more complex to set up and use.
Hygen is a fast, scalable, and developer-friendly code generator that uses plain text templates. It is similar to Plop in terms of simplicity and ease of use, but it focuses more on speed and scalability.
Slush is a scaffolding tool that uses Gulp for code generation. It is similar to Plop in that it allows you to create custom generators, but it leverages the Gulp ecosystem for more advanced build and task automation capabilities.
Micro-generator framework that makes it easy for an entire team to create files with a level or uniformity.
Plop is essentially glue code between inquirer prompts and handlebar templates. You can also add your own handlebar helper methods and use them in your templates.
npm install -g plop
The main parts of a plop generator are the plop file (plopfile.js
) and the templates. Templates can either be inline, or in separate files.
A basic plop file starts its life as a lowly node module that exports a function that accepts the plop
object.
module.exports = function (plop) {};
The plop
object offers three main functions (addHelper
, addPartial
, setGenerator
).
addHelper
directly corresponds to the handlebars method registerHelper
. So if you are familiar with handlebars helpers, then you already know how this works.
module.exports = function (plop) {
plop.addHelper('upperCase', function (text) {
return text.toUpperCase();
});
};
addPartial
directly corresponds to the handlebars method registerPartial
. So if you are familiar with handlebars partials, then you should be good to go here as well.
module.exports = function (plop) {
plop.addPartial('fullName', '{{ firstName }} {{ lastName }}');
};
Next we need to setup a generator using plop.setGenerator
The config object needs to include prompts
and actions
(description
is optional). The prompts array is passed to inquirer. The actions
array is a list of actions to take (described in greater detail below)
module.exports = function (plop) {
plop.setGenerator('test', {
description: 'this is a test',
prompts: [{
type: 'input',
name: 'name',
message: 'What is your name?',
validate: function (value) {
if ((/.+/).test(value)) { return true; }
return 'name is required';
}
}],
actions: [{
type: 'add',
path: 'folder/{{dashCase name}}.txt',
templateFile: 'templates/temp.txt'
}]
});
};
The plop.setGenerator
config object includes an array of actions to take. There are two types of actions you can include (add and modify). Both types of actions require a path to take action on (all paths are based on the location of the plopfile), and a template to use.
Let's start with the simpler of the 2 actions, add
.
The add
action is used to (you guessed it) add files to your project. The path property is a handlebars template that will be used to create the file by name. The file contents will be determined by the template
or templateFile
property. As you've probably guessed, the template
property is used for an inline template while the templateFile
is a path to the template stored in a file somewhere else in the project. I suggest keeping your template files in a plop-templates
folder at the root of the project.
The modify
action is similar to add
, but the main difference is that it will use a pattern
property to find/replace text in the file specified by the path
property. The pattern
property should be a RegExp and capture groups can be used in the replacement template using $1, $2, etc. More details on modify can be found in the example folder.
The Add
and Modify
actions will take care of almost every case that plop is designed to handle. However, plop does offer custom actions for the node/js guru. A custom action is a function that is provided in the actions array.
See the example plopfile for a sample synchronous custom action.
Alternatively, actions
can be a function that takes responses data
as a parameter and should return an array of actions.
This allows you to adapt actions to provided answers:
module.exports = function (plop) {
plop.setGenerator('test', {
description: 'this is a test',
prompts: [{
type: 'input',
name: 'name',
message: 'What is your name?',
validate: function (value) {
if ((/.+/).test(value)) { return true; }
return 'name is required';
}
},{
type: 'confirm',
name: 'wantTacos',
message: 'Do you want tacos?'
}],
actions: function(data) {
var actions = [];
if(data.wantTacos) {
actions.push({
type: 'add',
path: 'folder/{{dashCase name}}.txt',
templateFile: 'templates/tacos.txt'
});
} else {
actions.push({
type: 'add',
path: 'folder/{{dashCase name}}.txt',
templateFile: 'templates/burritos.txt'
});
}
return actions;
}
});
};
There are a few helpers that I have found useful enough to include with plop. They are mostly case modifiers, but here is the complete list.
Once plop is installed, and you have created a generator, you are ready to run plop from the terminal. Running plop
with no parameters will present you with a list of generators to pick from. You can also run plop [generatorName]
to trigger a generator directly.
Because when you create your boilerplate separate from your code, you naturally put more time and thought into it.
Because saving your team (or yourself) 5-15 minutes when creating every route, component, controller, helper, test, view, etc... really adds up.
Because context switching is expensive and saving time is not the only benefit to automating workflows
Yeoman is great and it does a fantastic job of scaffolding out an initial codebase for you. However, the initial codebase is just the beginning. I believe the true benefit to generators is not realized by saving a developer 40 hours in the beginning, but by saving a team days of work over the life of the project. Yes, yeoman has sub generators that do a similar job. However, if you're like me, you will continually tweak structure and code throughout the project till the sub generators that came built into your yeoman seed are no longer valid. These structures change as requirements change and code is refactored. Plop allows your generator code to live INSIDE your project and be versioned right along with the code it generates.
If you already have another generator that your organization uses and loves, use it :-). If you don't, try plop. It will make your code more consistent, save you lots of time, and (if you've read this far) you already know how to use it.
FAQs
Micro-generator framework that makes it easy for an entire team to create files with a level of uniformity
The npm package plop receives a total of 631,908 weekly downloads. As such, plop popularity was classified as popular.
We found that plop demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.