Skip to content

Instantly share code, notes, and snippets.

@BrianJenney
Created September 1, 2021 21:37
Show Gist options
  • Save BrianJenney/2b0d7a995128774747148648804cbeb8 to your computer and use it in GitHub Desktop.
Save BrianJenney/2b0d7a995128774747148648804cbeb8 to your computer and use it in GitHub Desktop.
const path = require('path');
const templatesPath = path.resolve(__dirname, 'templates'); // this is the path to our templates
const consumerPath = process.cwd(); // returns the current working directory
module.exports = (plop) => {
plop.setGenerator('component', componentPrompts({ templatesPath, consumerPath }));
};
const componentPrompts = ({ consumerPath, templatesPath }) => ({
description: 'Create a component',
prompts: [
{
type: 'input',
name: 'name',
message: 'What is the name of this component?'
},
{
type: 'checkbox',
name: 'globalState',
message: 'What global pieces of state are needed for this component?',
choices: ['products', 'account', 'cart', 'content', 'catalog', 'order']
},
{
type: 'confirm',
name: 'withTest',
message: 'Will this component need a test?'
}
],
actions: (data) => {
const actions = [
{
type: 'add',
path: `${consumerPath}/src/components/{{pascalCase name}}/{{pascalCase name}}.js`,
templateFile: `${templatesPath}/components/component.js.hbs`
},
{
type: 'add',
path: 'src/components/{{pascalCase name}}/index.js',
templateFile: `${templatesPath}/components/component.index.js.hbs`
}
];
if (data.withTest) {
actions.push({
type: 'add',
path: `${consumerPath}/src/components/{{pascalCase name}}/{{pascalCase name}}.test.js`,
templateFile: `${templatesPath}/components/component.test.js.hbs`
});
}
return actions;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment