-
Notifications
You must be signed in to change notification settings - Fork 1
/
plopfile.ts
127 lines (118 loc) · 3.95 KB
/
plopfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import type { NodePlopAPI } from 'plop';
import type { Actions } from 'node-plop';
const types = {
atoms: 'atoms',
molecules: 'molecules',
organisms: 'organisms',
blocks: 'blocks',
none: '',
} as const;
type Keys = keyof typeof types;
type Type = typeof types[Keys];
const typePrefixMap: Record<Type, string> = {
[types.atoms]: 'a',
[types.molecules]: 'm',
[types.organisms]: 'o',
[types.blocks]: 'b',
[types.none]: '',
};
export default function plopfile(plop: NodePlopAPI): void {
plop.setGenerator('component', {
description: 'Component generator',
prompts: [
{
name: 'type',
type: 'list',
message: 'What type of component would you like to generate?',
choices: [
{ name: 'Atom', value: 'atoms' },
{ name: 'Molecule', value: 'molecules' },
{ name: 'Organism', value: 'organisms' },
{ name: 'Block', value: 'blocks' },
{ name: 'No specific type', value: 'none' },
],
},
{
type: 'input',
name: 'name',
message: 'What is the name of your component?',
validate: (value) => (value.length === 0 ? 'Please enter a component name' : true),
},
{
name: 'id',
type: 'input',
message: 'What is the id of your component?',
when: ({ type }) => type !== types.none,
validate: (value) =>
value.length === 0 || !value.match(/^[0-9]+[a-z0-9]?$/)
? 'This value needs follow the [number][variant] pattern (for example: 1 or 2a).'
: true,
},
{
name: 'typePrefix',
type: 'input',
message: 'What do you want to use as a prefix?',
when: ({ type }) => type !== types.none,
default: ({ type }: { type: Type }) => typePrefixMap[type],
},
{
name: 'componentPrefix',
type: 'list',
message: 'What do you want to use as a component prefix?',
when: ({ type }) => type !== types.none,
choices: [
{ name: 'No prefix', value: '' },
{ name: 'Components Framework', value: 'cf' },
],
},
],
actions: (userData) => {
const {
componentPrefix = '',
type,
typePrefix = '',
name,
id = '',
} = userData as Record<string, string>;
const directory = type !== types.none ? `${type}/` : ``;
const data = {
isUnknownComponent: type === types.none,
isCmsComponent: type === types.blocks,
componentName: `${componentPrefix}${componentPrefix && '-'}${typePrefix}${id}-${name}`,
};
const actions: Actions = [
{
data,
type: 'addMany',
base: 'plop-templates/component',
templateFiles: 'plop-templates/component/*.*',
destination: `src/components/${directory}/{{dashCase componentName}}/`,
},
];
if (data.isCmsComponent) {
actions.push({
data,
type: 'append',
path: 'src/block-renderer/BlockRenderer.maps.ts',
pattern: /(\/\* PLOP_INJECT_TEMPLATE_IMPORT \*\/)/gi,
template: `import { {{camelCase componentName}}Template } from '@/components/{{type}}/{{dashCase componentName}}/{{pascalCase componentName}}.template';`,
});
actions.push({
data,
type: 'append',
path: 'src/block-renderer/BlockRenderer.maps.ts',
pattern: /(\/\* PLOP_INJECT_TEMPLATE \*\/)/gi,
template: ` '{{dashCase componentName}}': {{camelCase componentName}}Template,`,
});
actions.push({
data,
type: 'append',
path: 'src/block-renderer/BlockRenderer.components.ts',
pattern: /(\/\* PLOP_INJECT_COMPONENT \*\/)/gi,
template: ` lazy('{{dashCase componentName}}', () => import( /* webpackExports: "lazy" */ '@/components/{{type}}/{{dashCase componentName}}/{{pascalCase componentName}}'),),`,
});
}
return actions;
},
});
}