-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplopfile.ts
178 lines (154 loc) · 4.23 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { NodePlopAPI, ActionType } from 'plop'
const capitalize = (str: string) => {
return str.charAt(0).toUpperCase() + str.slice(1)
}
const camelCase = (str: string) => {
return str.replace(/[-_](\w)/g, (_, c) => c.toUpperCase())
}
function getPrompts(gen: string) {
const base = [
{
type: 'input',
name: `${gen}Name`,
message: `Enter ${gen} name in kebab-case:`,
validate: (value?: string) => {
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}:`,
},
]
const outDirPrompt = {
type: 'list',
name: 'outDir',
message: `Where should this ${gen} live?`,
default: defaultOutDirs[gen as keyof typeof defaultOutDirs],
choices: workspaces,
validate: (value?: string) => {
if (!value) {
return `outDir is required`
}
return true
},
}
if (gen === 'component') {
return [
...base,
{
type: 'input',
name: 'consoleTheme',
message: `Theme class object from @mochi-ui/theme for import in this ${gen}:`,
validate: (value?: string) => {
if (!value) {
return `Theme class object name is required`
}
return true
},
},
outDirPrompt,
{
type: 'list',
name: 'isAppendCore',
message: `Is this ${gen} need appended to Core package?`,
default: 'true',
choices: ['false', 'true'],
},
]
}
return [...base, outDirPrompt]
}
function getAppendComponentToCore(): ActionType[] {
return [
{
path: 'packages/core/src/index.ts',
template: "export * from '@mochi-ui/{{componentName}}'",
pattern:
/(\/\/ Plop adding component indicator - do not remove this line)/g,
type: 'append',
abortOnFail: true,
},
// Append to core packages
{
type: 'append',
path: './packages/core/package.json',
// With space as intended for matching file format
template: ` "@mochi-ui/{{componentName}}": "workspace:*",`,
// Add dependency right below dependencies
pattern: '"dependencies": {',
abortOnFail: true,
},
]
}
// Input workspaces for creating components/hooks/utils...
const workspaces = ['components', 'web3']
// Generator input must matching within root plop folders
const generators = ['component']
const defaultOutDirs = {
//
component: 'components',
web3: 'web3',
}
module.exports = function main(plop: NodePlopAPI) {
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: getPrompts(gen),
actions(answers) {
let actions: ActionType[] = []
if (!answers) {
return actions
}
const { description, outDir, consoleTheme, isAppendCore } = answers
const generatorName = answers[`${gen}Name`] ?? ''
let data = {
[`${gen}Name`]: generatorName,
description,
outDir,
}
if (gen === 'component') {
data = {
...data,
consoleTheme,
}
if (isAppendCore === 'true') {
actions = actions.concat(getAppendComponentToCore())
}
}
actions.push({
type: 'addMany',
templateFiles: `plop/${gen}/**`,
destination: `./packages/{{outDir}}/{{dashCase ${gen}Name}}`,
globOptions: { dot: true },
base: `plop/${gen}`,
data,
abortOnFail: true,
})
return actions
},
})
})
}