-
Notifications
You must be signed in to change notification settings - Fork 653
/
Copy pathplugin-new.js
181 lines (163 loc) · 5.13 KB
/
plugin-new.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
const fs = require('fs-extra')
// eslint-disable-next-line import/no-extraneous-dependencies
const path = require('path')
const inquirer = require('inquirer')
const ejs = require('ejs')
const chalk = require('chalk')
const yaml = require('js-yaml')
const dirTree = require('directory-tree')
const validatePName = require('validate-npm-package-name')
const semver = require('semver')
// const came = require('./lib/util')
const came = str => `${str}`.replace(/-\D/g, match => match.charAt(1).toUpperCase())
const questions = []
questions.push({
type: 'input',
name: 'name',
message: '请输入插件名, 多个单词以 "-" 连接, 如: user-permission\n',
validate(value) {
const done = this.async()
setTimeout(() => {
if (!value) {
done('不可为空')
return
}
const regex = /^[a-z][a-z0-9]*(-[a-z][a-z0-9]*)*/
if (!regex.test(value)) {
done('检测到格式错误, 多个单词以 "-" 连接, 如: user-permission')
return
}
const filePath = path.resolve(__dirname, `../src/plugin/${value}`)
if (fs.existsSync(filePath)) {
done('项目中已存在该插件, 请更换其他插件名')
return
}
const v = validatePName(`lc-plugin-${value}`)
if (!v.validForNewPackages) {
done(v.errors.join(', '))
return
}
done(null, true)
})
},
})
questions.push({
type: 'input',
name: 'title',
message: '请输入插件标题, 7个字内\n',
default: '插件标题',
})
questions.push({
type: 'input',
name: 'version',
message: '请输入版本号\n',
default: '1.0.0',
validate(value) {
const done = this.async()
if (!semver.valid(value)) {
done('版本号不符合规范')
return
}
done(null, true)
},
})
questions.push({
type: 'input',
name: 'description',
message: '简述插件处理的业务场景\n',
default: '',
})
questions.push({
type: 'input',
name: 'author',
message: '请输入作者\n',
default: '',
})
const cachePath = path.resolve(__dirname, './.cache')
const cachePluginPath = path.resolve(__dirname, './.cache/plugin')
const pluginTmpPath = path.resolve(__dirname, './template/plugin')
const pluginViewsPath = path.resolve(__dirname, './template/plugin/view')
const pluginStrPos = __dirname.length + '/template/'.length
const pluginsPath = path.resolve(__dirname, '../src/plugin')
// 检测是否有插件文件夹
if (!fs.existsSync(pluginsPath)) {
fs.mkdirSync(pluginsPath)
}
inquirer
.prompt(questions)
.then(answers => {
const result = answers
result.camelCaseName = came(result.name)
return result
})
.then(answers => {
const config = { ...answers }
// 创建缓存文件夹 .cache
if (!fs.existsSync(cachePath)) {
fs.mkdirSync(cachePath)
}
// 清空 plugin 文件夹
if (fs.existsSync(cachePluginPath)) {
fs.removeSync(cachePluginPath)
}
fs.mkdirSync(cachePluginPath)
dirTree(pluginTmpPath, {}, item => {
// 忽略隐藏文件
if (item.extension === '' || item.name[0] === '.') {
return
}
// 处理模板文件
if (item.extension === '.ejs') {
const template = fs.readFileSync(item.path, 'utf8')
const fileConfig = { ...config }
// 舞台 view 文件配置处理
if (item.path.slice(pluginStrPos).split(path.sep)[1] === 'view' && item.name.slice(-8) === '.vue.ejs') {
const viewConfig = {}
viewConfig.icon = 'iconfont icon-demo'
viewConfig.name = fileConfig.camelCaseName + item.name.slice(0, -8)
viewConfig.route = path
.join(config.name, path.relative(pluginViewsPath, item.path))
.split(path.sep)
.join('/')
viewConfig.route = `/${viewConfig.route.slice(0, -8)}`
viewConfig.order = null
viewConfig.inNav = true
viewConfig.title = '舞台页'
viewConfig.type = 'view'
viewConfig.auths = {
role: null,
permission: null,
}
viewConfig.needLogin = true
fileConfig.configYml = yaml.safeDump(viewConfig)
}
const result = ejs.render(template, fileConfig)
const targetPath1 = path.resolve(cachePluginPath, path.relative(pluginTmpPath, item.path).slice(0, -4))
fs.outputFileSync(targetPath1, result)
return
}
// 拷贝其他文件
const targetPath1 = path.resolve(cachePluginPath, path.relative(pluginTmpPath, item.path))
fs.copySync(item.path, targetPath1)
})
return config
})
.then(answers => {
// 复制 .cache 到 plugin
const sourcePath = path.resolve(__dirname, './.cache/plugin')
const targetPath = path.resolve(__dirname, `../src/plugin/${answers.name}`)
fs.copySync(sourcePath, targetPath)
console.log(chalk.green(`创建插件 ${answers.name}: ${targetPath}`))
// eslint-disable-next-line
})
.then(() => {
// eslint-disable-next-line
require('./plugin-get-config.js')
// eslint-disable-next-line
})
.catch(err => {
// eslint-disable-next-line
console.log(chalk.red('创建插件失败'))
console.error(err)
process.exit(1)
})