-
Notifications
You must be signed in to change notification settings - Fork 0
/
plopfile.js
254 lines (242 loc) · 6.89 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import prettier from "prettier"
import { execSync } from "child_process"
import fs from "fs"
const getPrettierTransform = (parser) => (template, data, cfg) => {
console.log({ template, data, cfg })
return prettier.format(template, { semi: false, parser })
}
export default (
/** @type {import('plop').NodePlopAPI} */
plop
) => {
plop.setActionType("install-deps", async (answers, config, plop) => {
if (answers.runInstall || answers.runInstall !== undefined) {
if (config.devDependencies && config.devDependencies.length > 0) {
const execLine = `yarn add --dev ${config.devDependencies.join(" ")}`
console.log(`Running "${execLine}"`)
execSync(execLine, { shell: true })
}
if (config.dependencies && config.dependencies.length > 0) {
const execLine = `yarn add ${config.dependencies.join(" ")}`
console.log(`Running "${execLine}"`)
execSync(execLine, { shell: true })
}
}
})
plop.setGenerator("ava-config", {
description: "Create ava.config.js",
prompts: [],
actions: () => {
const packageJSON = JSON.parse(fs.readFileSync("./package.json"))
const isESM = packageJSON.type === "module"
return [
{
type: "add",
path: "./ava.config.js",
templateFile: "./plop-templates/ava.config.js.hbs",
data: { isESM },
transform: getPrettierTransform("babel"),
},
]
},
})
plop.setGenerator("github-ci-test", {
description: "Create Github CI Test",
prompts: [
{
type: "confirm",
name: "startPostgres",
message: "Start postgres in workflow?",
default: false,
},
],
actions: [
{
type: "add",
path: "./.github/workflows/npm-test.yml",
templateFile: "./plop-templates/github-test.yml.hbs",
transform: getPrettierTransform("yaml"),
},
],
})
plop.setGenerator("github-ci-release", {
description: "Create Github CI Release System with Semantic Release",
prompts: [
{
name: "internal",
type: "confirm",
message: "Is this an internal module (@seamapi/*)?",
default: true,
},
{
name: "runInstall",
type: "confirm",
message: "Do you want to install semantic-release dependency?",
default: true,
},
],
actions: () => {
const packageJSON = JSON.parse(fs.readFileSync("./package.json"))
const isESM = packageJSON.type === "module"
return [
{
type: "add",
path: "./.github/workflows/npm-semantic-release.yml",
templateFile: "./plop-templates/github-release.yml.hbs",
transform: getPrettierTransform("yaml"),
},
{
type: "add",
path: "./release.config.js",
templateFile: "./plop-templates/release-config.js.hbs",
data: { isESM },
transform: getPrettierTransform("babel"),
},
{
type: "install-deps",
devDependencies: [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/npm",
"@semantic-release/git",
],
},
// add repository
{
type: "modify",
path: "./package.json",
transform: async (template, data, cfg) => {
// TODO check that "repository" is set
// get the correct value using `git remote -v`
// if no remote, guess based on package.json["name"] e.g.
// [email protected]:seamapi/package-name.git
return template
},
},
]
},
})
plop.setGenerator("github-ci-vercel-deploy", {
description: "Setup Vercel Deployments",
prompts: [
{
name: "migrate",
type: "confirm",
message:
"Do you want to migrate the production database before deploy?",
default: false,
},
],
actions: [
{
type: "add",
path: "./.github/vercel-deploy.yml",
templateFile: "./plop-templates/vercel-deploy.yml.hbs",
transform: getPrettierTransform("yaml"),
},
],
})
plop.setGenerator("prettierrc", {
description: "Create Prettier Config & Install Prettier",
prompts: [
{
name: "runInstall",
type: "confirm",
message: "Do you want to install prettier dependency?",
default: true,
},
],
actions: [
{
type: "add",
path: "./.prettierrc",
template: `{ "semi": false }`,
transform: getPrettierTransform("json"),
},
{
type: "install-deps",
devDependencies: ["prettier"],
},
],
})
plop.setGenerator("gitignore", {
description: "Create .gitignore file",
prompts: [],
actions: [
{
type: "add",
path: "./.gitignore",
templateFile: "./plop-templates/gitignore.hbs",
},
],
})
plop.setGenerator("node-pg-migrate", {
description: "Initialize node-pg-migrate in project",
prompts: [
{
name: "runInstall",
type: "confirm",
message: "Do you want to install the dependencies?",
default: true,
},
],
actions: [
{
type: "add",
path: "src/scripts/reset-db.ts",
template: `import resetDb from "lib/reset-db"\n\nresetDb()`,
},
{
type: "add",
path: "src/scripts/migrate-db.ts",
template: `import migrateDb from "lib/migrate-db"\n\nmigrateDb()`,
},
{
type: "add",
path: "src/lib/db/migrate-db.ts",
templateFile: "./plop-templates/migrate-db.ts.hbs",
},
{
type: "add",
path: "src/lib/db/reset-db.ts",
templateFile: "./plop-templates/reset-db.ts.hbs",
},
{
type: "modify",
path: "./package.json",
transform: (template, data, cfg) => {
console.log({ template, data, cfg })
const pkg = JSON.parse(template)
if (!pkg.scripts) pkg.scripts = {}
pkg.scripts["db:create-migration"] =
"node-pg-migrate --migration-file-language ts -m src/db/migrations create"
pkg.scripts["db:migrate"] = "esr src/scripts/migrate-db.ts"
pkg.scripts["db:reset"] = "esr src/scripts/reset-db.ts"
return JSON.stringify(pkg, null, 2)
},
},
{
type: "install-deps",
devDependencies: [
"node-pg-migrate",
"pg-connection-from-env",
"chalk",
"debug",
],
dependencies: ["zapatos", "pg"],
},
],
})
plop.setGenerator("tsconfig", {
description: "Create TypeScript Config",
prompts: [],
actions: [
{
type: "add",
path: "./tsconfig.json",
templateFile: "./plop-templates/tsconfig.json.hbs",
transform: getPrettierTransform("json"),
},
],
})
}