# Plop — Micro-Generator Framework for Consistent Code > A small tool that generates files from templates based on user prompts. Helps teams enforce consistent file structures for components, modules, and other repeatable patterns. ## Install Save as a script file and run: # Plop — Micro-Generator Framework for Consistent Code ## Quick Use ```bash npm install --save-dev plop ``` ```js // plopfile.js export default function (plop) { plop.setGenerator('component', { description: 'Create a React component', prompts: [{ type: 'input', name: 'name', message: 'Component name?' }], actions: [{ type: 'add', path: 'src/components/{{name}}/index.tsx', templateFile: 'plop-templates/component.hbs' }] }); } ``` ```bash npx plop component ``` ## Introduction Plop is a micro-generator framework that lets teams define small, focused code generators using Handlebars templates and interactive prompts. Instead of copying and renaming files manually, developers run a plop command and answer a few questions to scaffold new components, modules, or any repeatable code pattern. ## What Plop Does - Scaffolds files from Handlebars templates with dynamic names and content - Prompts users for input using Inquirer.js-compatible prompt types - Supports add, modify, and append actions for creating and updating files - Runs custom action functions for operations beyond simple file generation - Integrates into existing npm scripts and CI pipelines ## Architecture Overview Plop reads a plopfile that defines one or more generators. Each generator specifies a series of prompts (powered by Inquirer.js under the hood) and a list of actions. Actions are processed sequentially, with the prompt answers injected into Handlebars templates. Built-in action types handle file creation, text insertion, and modification, while custom action functions allow arbitrary logic. The Handlebars engine supports helpers and partials for reusable template fragments. ## Self-Hosting & Configuration - Install as a dev dependency and create a plopfile.js or plopfile.mjs at the project root - Define Handlebars templates in a dedicated directory (e.g., plop-templates/) - Register custom Handlebars helpers for string transformations like camelCase or kebab-case - Use the --plopfile flag to point to an alternative plopfile location - Compose multiple plopfiles with plop.load() for monorepo setups ## Key Features - Declarative generator definitions with prompts and template actions - Handlebars-powered templates with helpers, partials, and conditional blocks - Built-in string case helpers: camelCase, pascalCase, snakeCase, kebabCase, and more - File modification actions for inserting or appending code into existing files - Composable generators that can be shared across packages in a monorepo ## Comparison with Similar Tools - **Yeoman** — full-featured scaffolding ecosystem with a plugin registry; heavier setup and more boilerplate than Plop - **Hygen** — file-based code generator using EJS templates; similar scope, different template syntax - **Nx generators** — integrated into the Nx monorepo tool; tightly coupled to the Nx ecosystem - **Cookiecutter** — Python-based project templating; operates at the project level rather than file level ## FAQ **Q: Can I use Plop to modify existing files?** A: Yes. The modify action uses a regex pattern to find and replace content, and the append action adds text at a specific location in a file. **Q: Does Plop support TypeScript plopfiles?** A: Plop supports ESM plopfiles (.mjs). For TypeScript, use ts-node or tsx to register the loader. **Q: Can generators call other generators?** A: Yes. Use the built-in addMany action or call plop.getGenerator() within a custom action function. **Q: How do I share generators across a monorepo?** A: Use plop.load() to import generators from other packages or directories into a root plopfile. ## Sources - https://github.com/plopjs/plop - https://plopjs.com/ --- Source: https://tokrepo.com/en/workflows/asset-876a0062 Author: Script Depot