This repository was archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscaffold.ts
473 lines (393 loc) · 12.2 KB
/
scaffold.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import { join } from 'path'
import * as sinkStatic from '@adonisjs/sink'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import {
models,
authControllers,
userControllers,
enums,
exceptions,
middleware,
services,
authValidators,
userValidators,
contracts,
migrations,
providers,
events,
routes,
sharedViews,
sharedJs,
sharedCss,
themeFiles,
vueFiles,
} from './appFiles'
import { ScaffoldOptions } from './types'
function getStub(options: ScaffoldOptions, ...relativePaths: string[]) {
return join(__dirname, `templates/${options.stack}`, ...relativePaths)
}
export function makeModels(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
options: ScaffoldOptions
) {
const modelsDirectory = app.resolveNamespaceDirectory('models') || 'app/Models'
models.forEach((model) => {
const modelPath = join(modelsDirectory, `${model}.ts`)
const modelTemplate = new sink.files.MustacheFile(
projectRoot,
modelPath,
getStub(options, `app/models/${model}.txt`)
)
modelTemplate.overwrite = true
modelTemplate.commit()
sink.logger.action('create').succeeded(modelPath)
})
}
export function makeControllers(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
options: ScaffoldOptions
) {
const authNameSpace = 'app/Controllers/Http/Auth'
const userNamespace = 'app/Controllers/Http/User'
const controllersAuthDirectory = app.resolveNamespaceDirectory('controllers') || authNameSpace
const controllersUserDirectory = app.resolveNamespaceDirectory('controllers') || userNamespace
authControllers.forEach((controller) => {
const controllerPath = join(controllersAuthDirectory, `${controller}.ts`)
const controllerTemplate = new sink.files.MustacheFile(
projectRoot,
controllerPath,
getStub(options, `${authNameSpace}/${controller}.txt`)
)
controllerTemplate.overwrite = true
controllerTemplate.commit()
sink.logger.action('create').succeeded(controllerPath)
})
userControllers.forEach((controller) => {
const controllerPath = join(controllersUserDirectory, `${controller}.ts`)
const controllerTemplate = new sink.files.MustacheFile(
projectRoot,
controllerPath,
getStub(options, `${userNamespace}/${controller}.txt`)
)
controllerTemplate.overwrite = true
controllerTemplate.commit()
sink.logger.action('create').succeeded(controllerPath)
})
}
export function makeEnums(projectRoot: string, sink: typeof sinkStatic, options: ScaffoldOptions) {
const enumsDirectory = 'app/Enums'
enums.forEach((enumName) => {
const enumPath = join(enumsDirectory, `${enumName}.ts`)
const enumTemplate = new sink.files.MustacheFile(
projectRoot,
enumPath,
getStub(options, `${enumsDirectory}/${enumName}.txt`)
)
enumTemplate.overwrite = true
enumTemplate.commit()
sink.logger.action('create').succeeded(enumPath)
})
}
export function makeException(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
options: ScaffoldOptions
) {
const exceptionsNameSpace = 'app/Exceptions'
const exceptionsPath = app.resolveNamespaceDirectory('exceptions') || exceptionsNameSpace
exceptions.forEach((exception) => {
const exceptionPath = join(exceptionsPath, `${exception}.ts`)
const exceptionTemplate = new sink.files.MustacheFile(
projectRoot,
exceptionPath,
getStub(options, `${exceptionsNameSpace}/${exception}.txt`)
)
exceptionTemplate.overwrite = true
exceptionTemplate.commit()
sink.logger.action('create').succeeded(exceptionPath)
})
}
export function makeMiddleware(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
options: ScaffoldOptions
) {
const middlewareNameSpace = 'app/Middleware'
const middlewarePath = app.resolveNamespaceDirectory('middleware') || middlewareNameSpace
middleware.forEach((middleware) => {
if (options.stack === 'vue' && middleware === 'ShareProfile') return
const path = join(middlewarePath, `${middleware}.ts`)
const middlewareTemplate = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${middlewareNameSpace}/${middleware}.txt`)
)
middlewareTemplate.overwrite = true
middlewareTemplate.commit()
sink.logger.action('create').succeeded(path)
})
}
export function makeServiceProviders(
projectRoot: string,
sink: typeof sinkStatic,
options: ScaffoldOptions
) {
const providerPath = 'app/Providers'
services.forEach((service) => {
const path = join(providerPath, `${service}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${providerPath}/${service}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
}
export function makeValidators(
projectRoot: string,
sink: typeof sinkStatic,
options: ScaffoldOptions
) {
const authNameSpace = 'app/Validators/Auth'
const userNamespace = 'app/Validators/User'
authValidators.forEach((validator) => {
const path = join(authNameSpace, `${validator}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${authNameSpace}/${validator}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
userValidators.forEach((validator) => {
const path = join(userNamespace, `${validator}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${userNamespace}/${validator}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
}
export function makeContracts(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
options: ScaffoldOptions
) {
const contractsNameSpace = 'contracts'
const middlewarePath = app.resolveNamespaceDirectory('contracts') || contractsNameSpace
contracts.forEach((contract) => {
const path = join(middlewarePath, `${contract}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${middlewarePath}/${contract}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
}
export function makeMigrations(
projectRoot: string,
sink: typeof sinkStatic,
options: ScaffoldOptions
) {
const migrationsNameSpace = 'database/migrations'
const migrationsPath = migrationsNameSpace
migrations.forEach((migration) => {
const path = join(migrationsPath, `${migration}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${migrationsNameSpace}/${migration}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
}
export function makeProviders(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
options: ScaffoldOptions
) {
const providersNameSpace = 'providers'
const providersPath = app.resolveNamespaceDirectory('providers') || providersNameSpace
providers.forEach((provider) => {
const path = join(providersPath, `${provider}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${providersNameSpace}/${provider}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
}
export function makeEvents(projectRoot: string, sink: typeof sinkStatic, options: ScaffoldOptions) {
const eventsNameSpace = 'start/events'
const eventsPath = eventsNameSpace
events.forEach((event) => {
const path = join(eventsPath, `${event}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${eventsNameSpace}/${event}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
}
export function makeRoutes(projectRoot: string, sink: typeof sinkStatic, options: ScaffoldOptions) {
const routesNameSpace = 'start/routes'
const routesPath = routesNameSpace
routes.forEach((route) => {
const path = join(routesPath, `${route}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${routesNameSpace}/${route}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
}
const getAllViews = (options: ScaffoldOptions) => {
const partials = [
'partials/head',
'partials/header',
'partials/flashMessages',
'partials/profile/avatar',
'partials/profile/delete',
'partials/profile/details',
'partials/profile/password',
'partials/profile/sessions',
'partials/api-tokens/create-token',
'partials/api-tokens/list-tokens',
]
const layouts = ['layouts/auth', 'layouts/main']
const dashboard = ['dashboard/index', 'dashboard/api-tokens', 'dashboard/profile/edit']
const components = [
'components/ui/dropdown',
'components/ui/logo',
'components/ui/navbar',
'components/composites/breadcrumbs',
'components/composites/profileNavigation',
]
const auth = [
'auth/login',
'auth/register',
'auth/verification',
'auth/confirm-password',
'auth/password-reset',
'auth/password-reset-request',
'auth/password-reset-expired',
'auth/password-reset-invalid',
]
if (options.stack === 'edge') {
return [...partials, ...layouts, ...dashboard, ...components, ...auth, ...sharedViews]
}
return [...sharedViews, 'partials/meta', 'app']
}
const getAllCSS = (options: ScaffoldOptions) => {
if (options.stack === 'vue') {
return [...sharedCss]
}
return [...sharedCss]
}
const getAllJs = (options: ScaffoldOptions) => {
if (options.stack === 'vue') {
return [...sharedJs, ...themeFiles]
}
return [...sharedJs]
}
const makeViews = (projectRoot: string, sink: typeof sinkStatic, options: ScaffoldOptions) => {
const viewsNameSpace = 'resources/views'
const viewsPath = viewsNameSpace
getAllViews(options).forEach((view) => {
const path = join(viewsPath, `${view}.edge`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${viewsNameSpace}/${view}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
}
const makeCss = (projectRoot: string, sink: typeof sinkStatic, options: ScaffoldOptions) => {
const cssNameSpace = 'resources/css'
const cssPath = cssNameSpace
getAllCSS(options).forEach((css) => {
const path = join(cssPath, `${css}.css`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${cssNameSpace}/${css}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
}
const makeJs = (projectRoot: string, sink: typeof sinkStatic, options: ScaffoldOptions) => {
const jsNameSpace = 'resources/js'
const jsPath = jsNameSpace
getAllJs(options).forEach((js) => {
const path = join(jsPath, `${js}.js`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${jsNameSpace}/${js}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
}
const makeVueFiles = (projectRoot: string, sink: typeof sinkStatic, options: ScaffoldOptions) => {
const vueNameSpace = 'resources/js'
const vuePath = vueNameSpace
vueFiles.forEach((vue) => {
const path = join(vuePath, `${vue}.vue`)
const template = new sink.files.MustacheFile(
projectRoot,
path,
getStub(options, `${vueNameSpace}/${vue}.txt`)
)
template.overwrite = true
template.commit()
sink.logger.action('create').succeeded(path)
})
}
export function makeResources(
projectRoot: string,
sink: typeof sinkStatic,
options: ScaffoldOptions
) {
makeViews(projectRoot, sink, options)
makeCss(projectRoot, sink, options)
makeJs(projectRoot, sink, options)
if (options.stack === 'vue') {
makeVueFiles(projectRoot, sink, options)
}
}