-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator-script.js
65 lines (58 loc) · 1.6 KB
/
generator-script.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
/** @format */
const path = require('path')
const fse = require('fs-extra')
const prompts = require('prompts')
const _ = require('lodash')
const {template} = _
_.templateSettings.interpolate = /<%=([\s\S]+?)%>/g
async function generateComponentFile({fullPath, dayNumber, componentTitle}) {
const templateString = (
await fse.readFile(__dirname + '/components/day.template')
).toString()
const fileString = template(templateString)({
dayNumber,
componentTitle,
})
await fse.writeFile(fullPath + '/day-' + dayNumber + '.jsx', fileString)
}
async function updateIndexFile({fullPath, dayNumber}) {
const templateString = (
await fse.readFile(__dirname + '/pages/index.jsx')
).toString()
const fileString = templateString.replace(
`const daysDone = ${parseInt(dayNumber) - 1}`,
`const daysDone = ${parseInt(dayNumber)}`
)
await fse.writeFile(fullPath + '/index.jsx', fileString)
}
async function generateBoilerPlate({baseDirectory, response}) {
const {dayNumber, componentTitle} = response
const fullPath = path.resolve(baseDirectory)
await fse.ensureDir(fullPath)
await Promise.all([
generateComponentFile({fullPath, dayNumber, componentTitle}),
updateIndexFile({fullPath, dayNumber}),
])
}
async function generator() {
const detailsNeeded = [
{
type: 'text',
name: 'dayNumber',
message: 'Enter Day Number',
},
{
type: 'text',
name: 'componentTitle',
message: 'Enter Component Title',
},
]
const response = await prompts(detailsNeeded)
await generateBoilerPlate({
baseDirectory: process.cwd() + `/pages/`,
response,
})
}
if (require.main === module) {
generator()
}