forked from begyland/begynner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
80 lines (62 loc) · 2.3 KB
/
helpers.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
const fs = require('fs').promises
const chalk = require('chalk')
const makeDir = require('make-dir')
const Mustache = require('mustache')
const path = require('path')
exports.successLog = (text) => console.log(`${chalk.green('✅')} ${text}`)
exports.errorLog = (text) => console.log(`${chalk.red('❌')} ${text}`)
exports.infoLog = (text) => console.log(`\n\n${chalk.green(`${text}`)} `)
exports.rreaddir = async (filePath) => {
const dir = await fs.readdir(filePath)
const files = await Promise.all(
dir.map(async (relativePath) => {
const absolutePath = path.join(filePath, relativePath)
const stat = await fs.lstat(absolutePath)
return stat.isDirectory() ? rreaddir(absolutePath) : absolutePath
})
)
return files.flat()
}
exports.makeProject = async (data) => {
try {
const dirTemplateModel = path.resolve(__dirname, 'templates/_model')
const dirTemplate = path.resolve(__dirname, 'templates')
const { projectname, template } = data
const projectFolder = projectname
const templatePath = await this.rreaddir(`${dirTemplate}/${template}`)
const modelPath = await this.rreaddir(dirTemplateModel)
const filePath = [...templatePath, ...modelPath]
if (!filePath) {
this.errorLog('Template not found')
}
await this.createFolder(projectFolder)
await this.createFolder(`${projectFolder}/.github`)
await Promise.all(
filePath.map(async (relativePath) => {
const nameFile = relativePath.split('/').pop()
const renderMustache = ['readme.md', 'package.json', 'license.md'].find(
(file) => file === nameFile
)
const readFile = renderMustache
? Mustache.render(await fs.readFile(relativePath, 'utf8'), data)
: await fs.readFile(relativePath, 'utf8')
await this.createFile(projectFolder, nameFile, readFile)
})
)
this.infoLog(
`Congratulations project ${projectFolder} successfully created!`
)
return true
} catch (err) {
this.errorLog(err)
}
}
exports.createFolder = async (folder) => {
await makeDir(folder)
.then((path) => this.successLog(`${path} created!`))
}
exports.createFile = async (folder, file, content) => {
await fs
.writeFile(`${folder}/${file}`, content)
.then(() => this.successLog(`${folder}/${file} created!`))
}