-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate.js
97 lines (88 loc) · 2.63 KB
/
create.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
const chalk = require('chalk');
const path = require('path')
const fs = require('fs')
const log = (message) => console.log(chalk.green(message));
const successLog = (message) => console.log(chalk.blue(message));
const errorLog = (message) => console.log(chalk.red(message));
const resolve = (...file) => path.resolve(__dirname, ...file)
// 导入模板
const {
vueTemplate,
entryTemplate
} = require('./src/template/index')
// 生成文件
const generateFile = (path, data) => {
if (fs.existsSync(path)) {
errorLog(`${path}文件已存在`)
return
}
return new Promise((res, rej) => {
fs.writeFile(path, data, 'utf8', err => {
if (err) {
errorLog(err.message)
rej(err)
} else {
res(true)
}
})
})
}
log('请输入想要创建的文件:')
let componentName = ''
process.stdin.on('data', async function (chunk){
// 组件名称
const inputName = String(chunk).trim().toString()
// Vue页面组件路径
const componentPath = resolve('./src/views', inputName)
// vue文件
const vueFile = resolve(componentPath, 'main.vue')
// 入口文件
const entryFile = resolve(componentPath, 'entry.js')
// 判断组件文件夹是否存在
const hasComponentExists = fs.existsSync(componentPath)
if (hasComponentExists) {
errorLog(`${inputName}页面组件已存在, 请重新输入`)
return
} else {
log(`正在生成component目录 ${componentPath}`)
await dotExistDirectoryCreate(componentPath)
}
try {
// 获取组件名
if (inputName.includes('/')) {
const inputNameArr = inputName.split('/')
componentName = inputNameArr[inputNameArr.length - 1]
} else {
componentName = inputName
}
log(`正在生成vue文件 ${vueFile}`)
await generateFile(vueFile, vueTemplate(componentName))
successLog('生成组件成功')
await generateFile(entryFile, entryTemplate(componentName))
} catch(err) {
errorLog(err.message)
}
process.stdin.emit('end')
})
process.stdin.on('end', () => {
log('exit')
process.exit()
})
function dotExistDirectoryCreate(directory) {
return new Promise((resolve) => {
mkdir(directory, function () {
resolve(true)
})
})
}
function mkdir(directory, callback) {
var exists = fs.existsSync(directory)
if (exists) {
callback()
} else {
mkdir(path.dirname(directory), function () {
fs.mkdirSync(directory)
callback()
})
}
}