-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplopfile.js
87 lines (82 loc) · 2.49 KB
/
plopfile.js
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
/** @arg {import('plop').NodePlopAPI} plop */
export default function (plop) {
plop.setHelper('dashToTitle', text => {
const titleCase = plop.getHelper('titleCase');
return titleCase(text.replace(/-/g, ' '));
});
plop.setGenerator('component', {
description: 'generate a new component',
prompts: [
{
type: 'input',
name: 'name',
message:
'Please enter your component name in kebab-case (e.g. button-group)',
default: 'component',
},
{
type: 'input',
name: 'prefix',
message:
'What is the prefix for your component? (e.g. my)',
default: '',
},
],
actions: function (data) {
const basename = data?.name;
if (
// Must only contain alphanumeric characters and dashes
!/[a-z0-9-]+/.test(basename) ||
// Must start with a letter
!/^[a-z]/.test(basename) ||
// Must not end in a dash
basename.endsWith('-')
) {
console.log(
'The name must only contain alphanumeric characters and dashes, start with a letter, and not end in a dash. Please try again.',
);
return [];
}
const BASE_PATH = `src/components/{{kebabCase name}}`;
data.tagName = data?.prefix ? `${data.prefix}-${data.name}` : data.name;
return [
{
type: 'add',
skipIfExists: true,
path: `${BASE_PATH}/{{kebabCase name}}.ts`,
templateFile: 'plop-templates/component.ts.hbs',
},
{
type: 'add',
skipIfExists: true,
path: `${BASE_PATH}/{{kebabCase name}}.styles.ts`,
templateFile: 'plop-templates/component.styles.ts.hbs',
},
{
type: 'add',
skipIfExists: true,
path: `${BASE_PATH}/{{kebabCase name}}.test.ts`,
templateFile: 'plop-templates/component.test.ts.hbs',
},
{
type: 'add',
skipIfExists: true,
path: `${BASE_PATH}/{{kebabCase name}}.mdx`,
templateFile: 'plop-templates/component.docs.hbs',
},
{
type: 'add',
skipIfExists: true,
path: `${BASE_PATH}/{{kebabCase name}}.stories.ts`,
templateFile: 'plop-templates/component.stories.ts.hbs',
},
{
type: 'add',
skipIfExists: true,
path: `${BASE_PATH}/index.ts`,
templateFile: 'plop-templates/component.definition.ts.hbs',
},
];
},
});
}