-
Notifications
You must be signed in to change notification settings - Fork 18
/
plopfile.js
140 lines (131 loc) · 4.68 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
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
const { execSync } = require('node:child_process')
function addComponentFiles(name, templatePrefix) {
const path = `src/components/${name}`
const templ = `scripts/templates/${templatePrefix}`
return [
{ type: 'add', path: `${path}/index.ts`, templateFile: `${templ}.index.ts.hbs` },
{ type: 'add', path: `${path}/${name}.controller.tsx`, templateFile: `${templ}.controller.tsx.hbs` },
{ type: 'add', path: `${path}/${name}.module.scss`, templateFile: `${templ}.module.scss.hbs` },
{ type: 'add', path: `${path}/${name}.stories.tsx`, templateFile: `${templ}.stories.tsx.hbs` },
{ type: 'add', path: `${path}/${name}.view.tsx`, templateFile: `${templ}.view.tsx.hbs` }
]
}
module.exports = function (
/** @type {import('plop').NodePlopAPI} */
plop
) {
const pascalCase = plop.getHelper('pascalCase')
const camelCase = plop.getHelper('camelCase')
const kebabCase = plop.getHelper('kebabCase')
plop.setGenerator('component', {
description: 'create an src/component/{name}/{name}.* component files',
prompts: [
{
type: 'input',
name: 'name',
message: 'Type the component name:'
},
{
type: 'confirm',
name: 'forwardRef',
message: 'Do you want to add forwardRef?',
default: true
},
{
type: 'confirm',
name: 'transitionPresence',
message: 'Include transitionPresence animations?',
default: false
},
{
type: 'confirm',
name: 'imperativeHandle',
message: 'Include useImperativeHandle() hook?',
default: false
}
],
actions: [...addComponentFiles('{{titleCase name}}', 'component')]
})
plop.setGenerator('page', {
description: 'create an src/component/Page{name}/Page{name}.* component files',
prompts: [
{ type: 'input', name: 'name', message: 'Type the page name:' },
{
type: 'confirm',
name: 'generateRoute',
message: 'Generate src/pages/ route?',
default: true
}
],
actions: [
...addComponentFiles('Page{{titleCase name}}', 'page'),
{
type: 'modify',
path: 'src/data/content.json',
transform: (template, data) => {
const content = JSON.parse(template)
content.pages[camelCase(data.name)] = {
head: { title: pascalCase(data.name), description: '' },
body: { title: pascalCase(data.name) }
}
return JSON.stringify(content, null, 2)
}
},
{
type: 'add',
path: 'src/pages/{{kebabCase name}}.ts',
templateFile: 'scripts/templates/route.ts.hbs',
skip: (data) => (data.generateRoute ? false : 'Skipped route generation')
}
]
})
plop.setGenerator('route', {
description: 'create a src/pages route',
prompts: [{ type: 'input', name: 'name', message: 'Type the route name:' }],
actions: [{ type: 'add', path: 'src/pages/{{kebabCase name}}.ts', templateFile: 'scripts/templates/route.ts.hbs' }]
})
plop.setGenerator('slice', {
description: 'create a src/store slice',
prompts: [{ type: 'input', name: 'name', message: 'Type the slice name:' }],
actions: [
{ type: 'add', path: 'src/store/{{kebabCase name}}.slice.ts', templateFile: 'scripts/templates/slice.ts.hbs' },
{
type: 'modify',
path: 'src/store/store.ts',
transform: (template, data) => {
const lines = template.split('\n').map((line) => {
if (line.startsWith('export type AppState =')) {
return line.replace(
'export type AppState =',
`export type AppState = ${pascalCase(data.name)}SliceState & `
)
}
if (line.startsWith(' }))')) {
return line.replace(' }))', `, ...${pascalCase(data.name)}Slice(...props)\n }))`)
}
return line
})
lines.splice(
lines.findIndex((line) => line.startsWith('export')),
0,
`import type { ${pascalCase(data.name)}SliceState } from './${kebabCase(
data.name
)}.slice'\nimport { ${pascalCase(data.name)}Slice } from './${kebabCase(data.name)}.slice'\n`
)
return lines.join('\n')
}
},
function customAction() {
execSync('npm run autofix:store', { stdio: 'inherit' })
return ''
}
]
})
plop.setGenerator('api', {
description: 'create an src/pages/api route',
prompts: [{ type: 'input', name: 'name', message: 'Type the api name:' }],
actions: [
{ type: 'add', path: 'src/pages/api/{{kebabCase name}}.ts', templateFile: 'scripts/templates/api.ts.hbs' }
]
})
}