Created
March 4, 2023 17:45
-
-
Save jgdev/5be8b23a5be6ad737132f9e579a3063f to your computer and use it in GitHub Desktop.
Generate react components using Plop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @typedef {import('plop').NodePlopAPI} NodePlopAPI | |
*/ | |
/** | |
@param plop {NodePlopAPI} | |
*/ | |
module.exports = (plop) => { | |
plop.setHelper("concat", function (...args) { | |
return args.reduce( | |
(result, arg) => (typeof arg === "string" && result + arg) || result, | |
"" | |
); | |
}); | |
plop.setGenerator("component", { | |
description: "Create a component", | |
prompts: [ | |
{ | |
type: "input", | |
name: "name", | |
message: "What is your component name?", | |
}, | |
{ | |
type: "confirm", | |
name: "container", | |
message: "Generate container?", | |
}, | |
{ | |
type: "confirm", | |
name: "scss", | |
message: "Generate sass module?", | |
}, | |
{ | |
type: "confirm", | |
name: "storybook", | |
message: "Generate storybook?", | |
}, | |
{ | |
type: "confirm", | |
name: "test", | |
message: "Generate tests?", | |
}, | |
], | |
actions: [ | |
// Create component file | |
{ | |
type: "add", | |
path: "components/{{pascalCase name}}/{{pascalCase name}}.tsx", | |
templateFile: "Component.tsx.hbs", | |
}, | |
// Create container file | |
{ | |
type: "add", | |
path: "components/{{pascalCase name}}/{{pascalCase name}}Container.tsx", | |
templateFile: "Component.container.tsx.hbs", | |
skip: ({ container }) => !container && "No container file needed", | |
}, | |
// Create storybook file | |
{ | |
type: "add", | |
path: "components/{{pascalCase name}}/{{pascalCase name}}.storybook.tsx", | |
templateFile: "Component.storybook.tsx.hbs", | |
skip: ({ storybook }) => !storybook && "No storybook file needed", | |
}, | |
// Create test file | |
{ | |
type: "add", | |
path: "components/{{pascalCase name}}/{{pascalCase name}}.test.tsx", | |
templateFile: "Component.test.tsx.hbs", | |
skip: ({ test }) => !test && "No test file needed", | |
}, | |
// Create test file | |
{ | |
type: "add", | |
path: "components/{{pascalCase name}}/{{pascalCase name}}.module.scss", | |
templateFile: "Component.module.scss.hbs", | |
skip: ({ scss }) => !scss && "No sass module needed", | |
}, | |
// Create index file | |
{ | |
type: "add", | |
path: "components/{{pascalCase name}}/index.tsx", | |
templateFile: "Component.index.tsx.hbs", | |
}, | |
], | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment