Consistency Made Simple
Micro-generator framework that makes it easy for an entire team to create files with a level of uniformity.
Plop is what I like to call a "micro-generator framework." Now, I call it that because it is a small tool that gives you a simple way to generate code or any other type of flat text files in a consistent way. You see, we all create structures and patterns in our code (routes, controllers, components, helpers, etc). These patterns change and improve over time so when you need to create a NEW insert-name-of-pattern-here, it's not always easy to locate the files in your codebase that represent the current "best practice." That's where plop saves you. With plop, you have your "best practice" method of creating any given pattern in CODE. Code that can easily be run from the terminal by typing plop. Not only does this save you from hunting around in your codebase for the right files to copy, but it also turns "the right way" into "the easiest way" to make new files.
If you boil plop down to its core, it is basically glue code between inquirer prompts and handlebar templates.
This documentation is a work in progress. If you have great ideas, I'd love to hear them.
$ npm install --save-dev plop$ npm install -g plopexport default function (plop) {
// create your generators here
plop.setGenerator("basics", {
description: "this is a skeleton plopfile",
prompts: [], // array of inquirer prompts
actions: [], // array of actions
});
}
export defaultis only allowed in NodeJS inside "ESM" supported files. To use this syntax, yourplopfilemust be either:
- An ESM .js file with type: "module" in package.json
- An ESM .mjs file with any type declared in package.json
Alternatively, you can have a
plopfilewithmodule.exports = function (plop)instead. For this syntax, yourplopfilemust be either:
- A CommonJS .js file with type: "commonjs" in package.json
- A CommonJS .cjs file with any type declared in package.json
A plopfile starts its life as a node module that exports a function which accepts the plop object as its first parameter.
export default function (plop) {}The plop object exposes the plop API object which contains the setGenerator(name, config) function. This is the function that you use to (wait for it) create a generator for this plopfile. When plop is run from the terminal in this directory (or any sub-directory), a list of these generators will be displayed.
Let's try setting up a basic generator to see how that looks.
export default function (plop) {
// controller generator
plop.setGenerator("controller", {
description: "application controller logic",
prompts: [
{
type: "input",
name: "name",
message: "controller name please",
},
],
actions: [
{
type: "add",
path: "src/{{name}}.js",
templateFile: "plop-templates/controller.hbs",
},
],
});
}The controller generator we created above will ask us 1 question, and create 1 file. This can be expanded to ask as many questions as needed, and create as many files as needed. There are also additional actions that can be used to alter our codebase in different ways.
Plop uses the inquirer.js library to gather user data. A list of prompt types can be found on the inquirer official website.
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. If you did not install plop globally, you will need to setup an npm script to run plop for you.
// package.json
{
...,
"scripts": {
"plop": "plop"
},
...
}Once you get to know a project (and its generators) well, you may want to provide answers to the prompts when you run the generator. If I have (for instance) a component generator that has one prompt (name), I can run that generator using plop component "some component name" and it will immediately execute as though I had typed "some component name" into the prompt. If that same generator had a second prompt, the same input would have resulted in the user being prompted for the second value.
Prompts like confirm and list try to make sense of your input as best they can. For instance entering "y", "yes", "t", or "true" for a confirm prompt will result in a boolean true value. You can select items from a list using their value, index, key, or name. Checkbox prompts can accept a comma separated list of values in order to select multiples.
If you want to provide bypass input for the second prompt but not the first, you can use an underscore "_" to skip the bypass (ie
plop component _ "input for second prompt").
Plop comes with bypass logic built-in for standard inquirer prompts, but there are also ways to provide custom logic for how to handle user input for a specific prompt.
If you have published a 3rd party inquirer prompt plugin and would like to support bypass functionality for plop users out of the box, that is covered in another section of this documentation.
You can also bypass prompts by name using -- and then providing arguments for each prompt that you'd like to bypass. Examples below.
## Bypassing both prompt 1 and 2
$ plop component "my component" react
$ plop component -- --name "my component" --type react
## Bypassing only prompt 2 (will be prompted for name)
$ plop component _ react
$ plop component -- --type reactBy default Plop actions keep your files safe by failing when things look fishy. The most obvious example of this is not allowing an add action to overwrite a file that already exists. Plop actions individually support the force property but you can also use the --force flag when running Plop from the terminal. Using the --force flag will tell every action to run forcefully. With great power...
Modern Node.js supports TypeScript files out-of-the-box. No extra setup or CLI flags are required to use a TypeScript plopfile.
First, make a TypesScript plopfile using plop --init-ts or by hand:
// plopfile.ts
import type { NodePlopAPI } from "plop";
export default function (plop: NodePlopAPI) {
// plop generator code
}Second, add plop to scripts in package.json:
// package.json
"scripts": {
"plop" : "plop"
}Plop bundles TypeScript declarations and supports TypeScript plopfiles via tsx loaders, a feature of NodeJS command line imports.
First, make a TypesScript plopfile using plop --init-ts or by hand:
// plopfile.ts
import { NodePlopAPI } from "plop";
export default function (plop: NodePlopAPI) {
// plop generator code
}Next, install tsx and optionally cross-env:
npm i -D tsx cross-envFinally, use NODE_OPTIONS to activate the tsx loader. Now Plop can import your plopfile.ts:
Node.js v20.6 and above
// package.json
"scripts": {
"plop" : "cross-env NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts"
}Node.js v20.5.1 and below
// package.json
"scripts": {
"plop": "cross-env NODE_OPTIONS='--loader tsx' plop --plopfile=plopfile.ts"
}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
The plopfile api is the collection of methods that are exposed by the plop object. Most of the work is done by setGenerator but this section documents the other methods that you may also find useful in your plopfile.
Plop bundles TypeScript declarations. See using TypeScript plopfiles for more details.
Whether or not you write your plopfile in TypeScript, many editors will offer code assistance via JSDoc declarations.
// plopfile.js
export default function (
/** @type {import('plop').NodePlopAPI} */
plop,
) {
// plop generator code
}These are the methods you will commonly use when creating a plopfile. Other methods that are mostly for internal use are list in the other methods section.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| setGenerator | String, GeneratorConfig | PlopGenerator | setup a generator |
| setHelper | String, Function | setup handlebars helper | |
| setPartial | String, String | setup a handlebars partial | |
| setActionType | String, CustomAction | register a custom action type | |
| setPrompt | String, InquirerPrompt | registers a custom prompt type with inquirer | |
| load | Array[String], Object, Object | loads generators, helpers and/or partials from another plo |
security issue -> plop -> node-plop -> handlebars
addMany has strange behavior for files with only one extension
security issue plop → node-plop → inquirer → external-editor → tmp
plopfile.ts Typescript support
Bug with backslashes
Base path with relative path fails to find templates
Is the type definition of your DynamicPromptsFunction wrong?
Async follow up prompts
Append action removes literal "$`" due to special replacement pattern handling
[ERROR] String.prototype.split called on null or undefined