forked from vercel/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plopfile.mjs
155 lines (145 loc) · 4.46 KB
/
plopfile.mjs
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
export default function (plop) {
const transformName = (str) => {
return str.toLowerCase().replace(/ /g, '-')
}
// create your generators here
plop.setGenerator('example', {
description: 'new example in repo',
prompts: [
{
type: 'input',
name: 'name',
message: 'Example name: ',
},
{
type: 'list',
name: 'exampleScopeFolder',
message: 'Scope (Example folder): ',
choices: [
{ name: 'App directory', value: 'app-directory' },
{ name: 'Edge Functions', value: 'edge-functions' },
{ name: 'Edge Middleware', value: 'edge-middleware' },
{ name: 'Solutions', value: 'solutions' },
{ name: 'Starter', value: 'starter' },
],
},
{
type: 'checkbox',
name: 'options',
message: 'What options do you like?',
choices: [
{
name: 'Basic example',
value: 'basic',
checked: true,
},
{
name: 'Next.js API Routes - Serverless Functions',
value: 'serverless-functions',
},
{
name: 'Next.js API Routes - Edge Functions',
value: 'edge-functions',
},
{ name: 'Next.js Edge Middleware', value: 'edge-middleware' },
],
},
],
actions: (data) => {
const plopExampleName = transformName(data.name)
const plopPath = `${data.exampleScopeFolder}/${plopExampleName}`
const filesToAlwaysCopyOver = [
'README.md',
'tsconfig.json',
'.eslintrc.json',
'.gitignore',
'next-env.d.ts',
'package.json',
'pages/index.tsx',
'pages/_app.tsx',
'postcss.config.js',
'tailwind.config.js',
'public/favicon.ico',
]
const actions = []
// Copy over basic files
filesToAlwaysCopyOver.forEach((file) => {
actions.push({
type: 'add',
path: `{{exampleScopeFolder}}/${plopExampleName}/${file}`,
templateFile: `plop-templates/example/${file}`,
})
})
// modify _app.tsx
actions.push({
type: 'modify',
path: `{{exampleScopeFolder}}/${plopExampleName}/pages/_app.tsx`,
pattern: /(-- PLOP PATH HERE --)/gi,
template: `${plopPath}`,
})
actions.push({
type: 'modify',
path: `{{exampleScopeFolder}}/${plopExampleName}/pages/_app.tsx`,
pattern: /(-- PLOP TITLE HERE --)/gi,
template: `${data.name}`,
})
if (data.options.includes('serverless-functions')) {
actions.push({
type: 'add',
path: `{{exampleScopeFolder}}/${plopExampleName}/pages/api/hello.ts`,
templateFile: `plop-templates/example/pages/api/hello.ts`,
})
}
if (data.options.includes('edge-functions')) {
actions.push({
type: 'add',
path: `{{exampleScopeFolder}}/${plopExampleName}/pages/api/hello.ts`,
templateFile: `plop-templates/example/pages/api/hello-edge.ts`,
})
}
if (data.options.includes('edge-middleware')) {
actions.push({
type: 'add',
path: `{{exampleScopeFolder}}/${plopExampleName}/middleware.ts`,
templateFile: `plop-templates/example/middleware.ts`,
})
}
return [
...actions,
// README.md
{
type: 'modify',
path: `{{exampleScopeFolder}}/${plopExampleName}/README.md`,
pattern: /(-- PLOP TITLE HERE --)/gi,
template: `${data.name}`,
},
{
type: 'modify',
path: `{{exampleScopeFolder}}/${plopExampleName}/README.md`,
pattern: /(-- PLOP EXAMPLE NAME HERE --)/gi,
template: `${plopExampleName}`,
},
{
type: 'modify',
path: `{{exampleScopeFolder}}/${plopExampleName}/README.md`,
pattern: /(-- PLOP PATH HERE --)/gi,
template: `${plopPath}`,
},
// package.json
{
type: 'modify',
path: `{{exampleScopeFolder}}/${plopExampleName}/package.json`,
pattern: /(-- PLOP EXAMPLE NAME HERE --)/gi,
template: `${plopExampleName}`,
},
// pages/index.tsx
{
type: 'modify',
path: `{{exampleScopeFolder}}/${plopExampleName}/pages/index.tsx`,
pattern: /(-- PLOP TITLE HERE --)/gi,
template: `${data.name}`,
},
]
},
})
}