-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate-entry.js
232 lines (200 loc) · 5.36 KB
/
generate-entry.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
const inquirer = require("inquirer")
const fs = require("fs")
const path = require("path")
const entryNames = fs
.readdirSync("./src/entries", { withFileTypes: true })
.filter((file) => file.isDirectory())
.map((file) => file.name)
const logger = {
error(...message) {
console.error("\x1b[31m%s\x1b[0m", ...message)
},
success(...message) {
console.log("\x1b[32m%s\x1b[0m", ...message)
},
}
const questions = [
{
type: "input",
name: "title",
message: "Title of your work",
validate(value) {
if (value.length <= 255) {
return true
}
return "Your title must be less than 255 characters"
},
},
{
type: "input",
name: "slug",
message: "Your entry folder name (PascalCase)",
validate(value) {
if (value.length > 64) {
return "Component name must not exceed 64 characters"
}
if (!value.match(/^[A-Z][a-z]+(?:[A-Z][a-z]+)*$/)) {
return "Component name must be in PascalCase"
}
if (entryNames.find((name) => name === value)) {
return "Component name already used. Open src/entries to view all used component names."
}
return true
},
},
{
type: "input",
name: "description",
message: "Describe what you're making (optional)",
validate(value) {
if (value.length <= 516) {
return true
}
return "Your description must be less than 516 characters"
},
},
{
type: "input",
name: "author",
message: "Author (you can use any alias if you want)",
validate(value) {
if (value) {
return true
}
return "Author must not be empty."
},
},
{
type: "input",
name: "email",
message: "Your email address (optional)",
validate(value) {
const pass =
!value ||
value.match(
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
)
if (pass) {
return true
}
return "Please enter a valid email address"
},
},
{
type: "input",
name: "linkedIn",
message: "Your LinkedIn profile (optional)",
},
{
type: "input",
name: "website",
message: "Your website (optional)",
},
{
type: "confirm",
name: "confirm",
message: "Is everything correct?",
},
]
async function main() {
console.log("Create your entry for the Rehackt Challenge")
const { confirm, ...answers } = await inquirer.prompt(questions)
console.log("\nCreating folder and component...")
const entriesPath = path.resolve(__dirname, "src/entries")
const componentDirPath = `${entriesPath}/${answers.slug}`
if (fs.existsSync(componentDirPath)) {
logger.error(
`A folder named ${answers.slug} already exists. Kindly choose a new component name or delete the folder with the same name.`
)
return
}
try {
fs.promises.mkdir(componentDirPath)
} catch (error) {
logger.error("Cannot create directory.", error?.message)
return
}
let templateData = ""
try {
const template = await fs.promises.readFile(".component-template")
const templateBuffer = Buffer.from(template)
templateData = templateBuffer
.toString()
.replace(/SampleEntry/g, answers.slug)
} catch (error) {
logger.error(
"Cannot fetch template data. Make sure that .component-template exists in the root folder.",
error?.message
)
return
}
try {
await fs.promises.writeFile(
`${componentDirPath}/${answers.slug}.jsx`,
templateData
)
} catch (error) {
logger.error(`Cannot create file ${answers.slug}.jsx.`, error?.message)
return
}
console.log("Adding your entry...")
let entriesFileData
try {
entriesFileData = await fs.promises.readFile(`${entriesPath}/index.js`)
} catch (error) {
logger.error(`Cannot fetch entries/index.js`, error?.message)
return
}
const entriesBuffer = Buffer.from(entriesFileData)
const regex = /([\s\S]*)(const entries = )([\s\S]*)(export default entries)/gm
const entriesSubstrings = regex.exec(entriesBuffer)
const answersString = JSON.stringify(answers, null, 4).replace(
/("(\\[^]|[^\\"])*"(?!\s*:))|"((\\[^]|[^\\"])*)"(?=\s*:)/g,
"$1$3"
)
const entriesArray = `${entriesSubstrings[3].substring(
0,
entriesSubstrings[3].lastIndexOf("]")
)} ${answersString.substring(
0,
answersString.lastIndexOf("\n}")
)},\n component: lazy(() => import("./${answers.slug}/${
answers.slug
}")),\n },\n]\n\n`
const newEntriesData = Object.assign([], entriesSubstrings.splice(1), {
2: entriesArray,
5: "\n",
}).join("")
try {
await fs.promises.writeFile(
`${entriesPath}/index.js`,
newEntriesData,
(err) => {
if (err) {
return console.error(err)
}
}
)
} catch (error) {
logger.error("Cannot update entries/index.js", error?.message)
return
}
logger.success("\nEntry created successfully!\n")
const componentPath = path.resolve(
__dirname,
`src/entries/${answers.slug}/${answers.slug}.jsx`
)
console.log(
"You can now edit your component at",
"\x1b[42m\x1b[30m",
`${componentPath}`,
"\x1b[0m"
)
console.log(
"You can update your entry details anytime at",
"\x1b[44m\x1b[30m",
`${entriesPath}/index.js`,
"\x1b[0m"
)
}
main()