-
Notifications
You must be signed in to change notification settings - Fork 30
/
add-component.js
executable file
·54 lines (47 loc) · 1.25 KB
/
add-component.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
const fs = require('fs')
const path = require('path')
const inquirer = require('inquirer')
const askQuestions = () => {
const questions = [
{
type: 'list',
name: 'FOLDERTYPE',
message: 'What are we creating?',
choices: ['component', 'pattern', 'style', 'page'],
},
{
name: 'FOLDERNAME',
type: 'input',
message: 'What is the name of the component/pattern/style/page?',
},
]
return inquirer.prompt(questions)
}
const createFile = (filePath, name) => {
const fileTypes = [`_${name}.scss`, `_${name}.hbs`, 'index.hbs', `${name}.js`, `${name}.json`]
fileTypes.forEach((element) => {
fs.writeFileSync(`${filePath}/${element}`, '')
})
}
const createDir = (type, name) => {
const currentDir = path.resolve(process.cwd())
const filePath = `${currentDir}/src/${type}s/${name}`
if (!fs.existsSync(filePath)) {
fs.mkdir(filePath, (err) => {
if (err) {
console.log(err.message)
} else {
console.log(`${filePath} created!`)
createFile(filePath, name)
}
})
} else {
createFile(filePath, name)
}
}
const run = async () => {
const answers = await askQuestions()
const { FOLDERTYPE, FOLDERNAME } = answers
createDir(FOLDERTYPE, FOLDERNAME)
}
run()