forked from vercel/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plopfile.mjs
141 lines (132 loc) · 3.96 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
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: 'Solutions', value: 'solutions' },
{ name: 'Edge Functions', value: 'edge-functions' },
{ name: 'App directory', value: 'app-directory' },
],
},
{
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: Hello world',
value: 'next-api-pages',
},
{ name: 'Next.js Middleware Function', value: '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('next-api-pages')) {
actions.push({
type: 'add',
path: `{{exampleScopeFolder}}/${plopExampleName}/pages/api/hello.ts`,
templateFile: `plop-templates/example/pages/api/hello.ts`,
})
}
if (data.options.includes('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}`,
},
]
},
})
}