-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplopfile.js
More file actions
111 lines (94 loc) · 2.59 KB
/
Copy pathplopfile.js
File metadata and controls
111 lines (94 loc) · 2.59 KB
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
/**
* Part of this code is taken from @chakra-ui/react package ❤️
*/
const capitalize = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1)
}
const camelCase = (str) => {
return str.replace(/[-_](\w)/g, (_, c) => c.toUpperCase())
}
const workspaces = ['components', 'hooks']
const generators = ['component', 'hook']
const defaultOutDirs = {
component: 'components',
hook: 'hooks'
}
/**
* @param {import("plop").NodePlopAPI} plop
*/
module.exports = function main(plop) {
plop.setHelper('capitalize', (text) => {
return capitalize(camelCase(text))
})
plop.setHelper('camelCase', (text) => {
return camelCase(text)
})
generators.forEach((gen) => {
plop.setGenerator(gen, {
description: `Generates a ${gen}`,
prompts: [
{
type: 'input',
name: `${gen}Name`,
message: `Enter ${gen} name:`,
validate: (value) => {
if (!value) {
return `${gen} name is required`
}
// check is has a valid hook name "use-something"
if (gen === 'hook' && !value.startsWith('use-')) {
return "Hook name must start with 'use-'"
}
// check is case is correct
if (value !== value.toLowerCase()) {
return `${gen} name must be in lowercase`
}
// cannot have spaces
if (value.includes(' ')) {
return `${gen} name cannot have spaces`
}
return true
}
},
{
type: 'input',
name: 'description',
message: `The description of this ${gen}:`
},
{
type: 'list',
name: 'outDir',
message: `where should this ${gen} live?`,
default: defaultOutDirs[gen],
choices: workspaces,
validate: (value) => {
if (!value) {
return `outDir is required`
}
return true
}
}
],
actions(answers) {
const actions = []
if (!answers) return actions
const { description, outDir } = answers
const generatorName = answers[`${gen}Name`] ?? ''
const data = {
[`${gen}Name`]: generatorName,
description,
outDir
}
actions.push({
type: 'addMany',
templateFiles: `plop/${gen}/**`,
destination: `./packages/{{outDir}}/{{dashCase ${gen}Name}}`,
base: `plop/${gen}`,
data,
abortOnFail: true
})
return actions
}
})
})
}