What is node-plop?
node-plop is a tool that allows you to create and manage code generators using a pluggable and customizable approach. It is built on top of plop, which is a micro-generator framework that helps you create consistent and repeatable code structures.
What are node-plop's main functionalities?
Creating Generators
This feature allows you to create a generator with prompts and actions. The example shows a basic generator that prompts the user for a name and then creates a file using a Handlebars template.
const nodePlop = require('node-plop');
const plop = nodePlop();
plop.setGenerator('basic-generator', {
description: 'this is a basic generator',
prompts: [
{
type: 'input',
name: 'name',
message: 'What is your name?'
}
],
actions: [
{
type: 'add',
path: 'src/{{name}}.js',
templateFile: 'plop-templates/basic.hbs'
}
]
});
plop.getGenerator('basic-generator').runActions({ name: 'example' });
Custom Actions
This feature allows you to define custom actions that can be executed as part of the generator. The example shows how to create a custom action type and use it in a generator.
const nodePlop = require('node-plop');
const plop = nodePlop();
plop.setActionType('customAction', (answers, config, plop) => {
return 'Custom action executed';
});
plop.setGenerator('custom-generator', {
description: 'this is a custom generator',
prompts: [
{
type: 'input',
name: 'name',
message: 'What is your name?'
}
],
actions: [
{
type: 'customAction'
}
]
});
plop.getGenerator('custom-generator').runActions({ name: 'example' });
Using Handlebars Helpers
This feature allows you to add custom Handlebars helpers to your generators. The example shows how to add an 'upperCase' helper and use it in a generator to create a file with an uppercase name.
const nodePlop = require('node-plop');
const plop = nodePlop();
plop.addHelper('upperCase', (text) => text.toUpperCase());
plop.setGenerator('helper-generator', {
description: 'this is a helper generator',
prompts: [
{
type: 'input',
name: 'name',
message: 'What is your name?'
}
],
actions: [
{
type: 'add',
path: 'src/{{upperCase name}}.js',
templateFile: 'plop-templates/helper.hbs'
}
]
});
plop.getGenerator('helper-generator').runActions({ name: 'example' });
Other packages similar to node-plop
yeoman-generator
Yeoman Generator is a scaffolding tool that allows you to create and manage code generators. It provides a more extensive ecosystem and a larger community compared to node-plop. Yeoman generators are more feature-rich and offer more built-in functionalities, but they can be more complex to set up and use.
hygen
Hygen is a fast and lightweight code generator that focuses on simplicity and ease of use. It uses plain JavaScript and EJS templates, making it easy to get started. Compared to node-plop, Hygen is more straightforward but may lack some of the advanced customization options available in node-plop.
slush
Slush is a scaffolding tool that uses Gulp for task automation. It allows you to create and manage code generators with a focus on flexibility and customization. Slush provides a different approach compared to node-plop, leveraging the power of Gulp to handle complex tasks and workflows.
Node-Plop
This is an early publication of the plop core logic being removed from the CLI tool. Main purpose for this is to make it easier for others to automate code generation through processes and tools OTHER than the command line. This also makes it easier to test the code functionality of PLOP without needing to test via the CLI interface.
This is the backend code that drives the plop CLI tool using node-plop.
import nodePlop from 'node-plop';
const plop = await nodePlop(`./path/to/plopfile.js`);
const basicAdd = plop.getGenerator('basic-add');
basicAdd.runActions({name: 'this is a test'}).then(function (results) {
});