-
Notifications
You must be signed in to change notification settings - Fork 27.8k
/
Copy pathconfig.ts
131 lines (122 loc) · 3.33 KB
/
config.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
import { type NodePlopAPI } from 'node-plop'
import path from 'path'
import * as helpers from './helpers'
interface TestResponse {
appDir: string
type: 'e2e' | 'production' | 'development' | 'unit'
}
interface ErrorResponse {
name: string
title: string
why: string
fix: string
}
function validateNonEmptyString(field: string) {
return function (value: string) {
if (/.+/.test(value)) {
return true
}
return `${field} is required`
}
}
export default function generator(plop: NodePlopAPI): void {
// make our custom helpers available for use in templates as handlebars helpers
helpers.init(plop)
plop.setGenerator('test', {
description: 'Create a new test',
prompts: [
{
type: 'confirm',
name: 'appDir',
message: 'Is this test for the app directory?',
default: true,
},
{
type: 'input',
name: 'name',
message: 'Test name',
validate: validateNonEmptyString('test name'),
},
{
type: 'list',
name: 'type',
message: 'Test type',
choices: [
{
name: 'e2e - Test "next dev" and "next build && next start"',
value: 'e2e',
},
{
name: 'production - Test "next build && next start"',
value: 'production',
},
{ name: 'development - Test "next dev"', value: 'development' },
{ name: 'unit - Test individual files', value: 'unit' },
],
},
],
actions: function (answers) {
const { appDir, type } = answers as TestResponse
const testRoot = path.join(plop.getDestBasePath(), 'test')
const appDirPath = appDir ? 'app-dir/' : ''
let templatePath = path.join(
testRoot,
type === 'unit' ? 'unit' : 'e2e',
appDirPath,
'test-template'
)
let targetPath = path.join(testRoot, type, appDirPath)
return [
{
type: 'addMany',
templateFiles: `${templatePath}/**/*`,
base: templatePath,
destination: targetPath,
},
]
},
})
plop.setGenerator('error', {
description: 'Create a new error document',
prompts: [
{
name: 'name',
type: 'input',
message: 'Url path with dashes. E.g. circular-structure',
validate: validateNonEmptyString('path'),
},
{
name: 'title',
type: 'input',
message: 'Title for the error. E.g. Circular Structure',
validate: validateNonEmptyString('title'),
},
{
name: 'why',
type: 'input',
message: 'What caused the error to happen?',
validate: validateNonEmptyString('why'),
},
{
name: 'fix',
type: 'input',
message: 'What are the possible ways to fix it?',
validate: validateNonEmptyString('fix'),
},
],
actions: function (answers) {
const { name } = answers as ErrorResponse
const errorsRoot = path.join(plop.getDestBasePath(), 'errors')
return [
{
type: 'add',
path: path.join(errorsRoot, `{{ toFileName name }}.mdx`),
templateFile: path.join(errorsRoot, `template.txt`),
},
`Url for the error: https://nextjs.org/docs/messages/${helpers.toFileName(
name
)}`,
]
},
})
}