1
1
const fs = require ( 'fs-extra' )
2
+ // eslint-disable-next-line import/no-extraneous-dependencies
2
3
const path = require ( 'path' )
3
4
const inquirer = require ( 'inquirer' )
4
5
const ejs = require ( 'ejs' )
@@ -7,6 +8,9 @@ const yaml = require('js-yaml')
7
8
const dirTree = require ( 'directory-tree' )
8
9
const validatePName = require ( 'validate-npm-package-name' )
9
10
const semver = require ( 'semver' )
11
+ // const came = require('./lib/util')
12
+
13
+ const came = str => `${ str } ` . replace ( / - \D / g, match => match . charAt ( 1 ) . toUpperCase ( ) )
10
14
11
15
const questions = [ ]
12
16
@@ -29,7 +33,7 @@ questions.push({
29
33
return
30
34
}
31
35
32
- const filePath = path . resolve ( __dirname , `../src/plugins /${ value } ` )
36
+ const filePath = path . resolve ( __dirname , `../src/plugin /${ value } ` )
33
37
if ( fs . existsSync ( filePath ) ) {
34
38
done ( '项目中已存在该插件, 请更换其他插件名' )
35
39
return
@@ -84,89 +88,94 @@ questions.push({
84
88
const cachePath = path . resolve ( __dirname , './.cache' )
85
89
const cachePluginPath = path . resolve ( __dirname , './.cache/plugin' )
86
90
const pluginTmpPath = path . resolve ( __dirname , './template/plugin' )
87
- const pluginViewsPath = path . resolve ( __dirname , './template/plugin/views ' )
91
+ const pluginViewsPath = path . resolve ( __dirname , './template/plugin/view ' )
88
92
const pluginStrPos = __dirname . length + '/template/' . length
89
- const pluginsPath = path . resolve ( __dirname , '../src/plugins ' )
93
+ const pluginsPath = path . resolve ( __dirname , '../src/plugin ' )
90
94
91
95
// 检测是否有插件文件夹
92
96
if ( ! fs . existsSync ( pluginsPath ) ) {
93
97
fs . mkdirSync ( pluginsPath )
94
98
}
95
99
96
- inquirer . prompt ( questions ) . then ( ( answers ) => {
97
- const result = answers
98
- result . camelCaseName = result . name
99
- . split ( '-' )
100
- . map ( str => ( str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) ) )
101
- . join ( '' )
102
- return result
103
- } ) . then ( ( answers ) => {
104
- const config = { ...answers }
105
-
106
- // 创建缓存文件夹 .cache
107
- if ( ! fs . existsSync ( cachePath ) ) {
108
- fs . mkdirSync ( cachePath )
109
- }
110
- // 清空 plugin 文件夹
111
- if ( fs . existsSync ( cachePluginPath ) ) {
112
- fs . removeSync ( cachePluginPath )
113
- }
114
- fs . mkdirSync ( cachePluginPath )
115
-
116
- dirTree ( pluginTmpPath , { } , ( item ) => {
117
- // 忽略隐藏文件
118
- if ( item . extension === '' || item . name [ 0 ] === '.' ) {
119
- return
100
+ inquirer
101
+ . prompt ( questions )
102
+ . then ( answers => {
103
+ const result = answers
104
+ result . camelCaseName = came ( result . name )
105
+ return result
106
+ } )
107
+ . then ( answers => {
108
+ const config = { ...answers }
109
+
110
+ // 创建缓存文件夹 .cache
111
+ if ( ! fs . existsSync ( cachePath ) ) {
112
+ fs . mkdirSync ( cachePath )
113
+ }
114
+ // 清空 plugin 文件夹
115
+ if ( fs . existsSync ( cachePluginPath ) ) {
116
+ fs . removeSync ( cachePluginPath )
120
117
}
121
- // 处理模板文件
122
- if ( item . extension === '.ejs' ) {
123
- const template = fs . readFileSync ( item . path , 'utf8' )
124
- const fileConfig = { ...config }
125
- // 舞台 view 文件配置处理
126
- if ( item . path . slice ( pluginStrPos ) . split ( path . sep ) [ 1 ] === 'views' && item . name . slice ( - 8 ) === '.vue.ejs' ) {
127
- const viewConfig = { }
128
- viewConfig . icon = 'iconfont icon-demo'
129
- viewConfig . name = fileConfig . camelCaseName + item . name . slice ( 0 , - 8 )
130
- viewConfig . route = path . join ( config . name , path . relative ( pluginViewsPath , item . path ) ) . split ( path . sep ) . join ( '/' )
131
- viewConfig . route = `/${ viewConfig . route . slice ( 0 , - 8 ) } `
132
- viewConfig . order = null
133
- viewConfig . inNav = true
134
- viewConfig . title = '舞台页'
135
- viewConfig . type = 'view'
136
- viewConfig . auths = {
137
- role : null ,
138
- right : null ,
118
+ fs . mkdirSync ( cachePluginPath )
119
+
120
+ dirTree ( pluginTmpPath , { } , item => {
121
+ // 忽略隐藏文件
122
+ if ( item . extension === '' || item . name [ 0 ] === '.' ) {
123
+ return
124
+ }
125
+ // 处理模板文件
126
+ if ( item . extension === '.ejs' ) {
127
+ const template = fs . readFileSync ( item . path , 'utf8' )
128
+ const fileConfig = { ...config }
129
+ // 舞台 view 文件配置处理
130
+ if ( item . path . slice ( pluginStrPos ) . split ( path . sep ) [ 1 ] === 'view' && item . name . slice ( - 8 ) === '.vue.ejs' ) {
131
+ const viewConfig = { }
132
+ viewConfig . icon = 'iconfont icon-demo'
133
+ viewConfig . name = fileConfig . camelCaseName + item . name . slice ( 0 , - 8 )
134
+ viewConfig . route = path
135
+ . join ( config . name , path . relative ( pluginViewsPath , item . path ) )
136
+ . split ( path . sep )
137
+ . join ( '/' )
138
+ viewConfig . route = `/${ viewConfig . route . slice ( 0 , - 8 ) } `
139
+ viewConfig . order = null
140
+ viewConfig . inNav = true
141
+ viewConfig . title = '舞台页'
142
+ viewConfig . type = 'view'
143
+ viewConfig . auths = {
144
+ role : null ,
145
+ permission : null ,
146
+ }
147
+ viewConfig . needLogin = true
148
+ fileConfig . configYml = yaml . safeDump ( viewConfig )
139
149
}
140
- viewConfig . needLogin = true
141
- fileConfig . configYml = yaml . safeDump ( viewConfig )
150
+ const result = ejs . render ( template , fileConfig )
151
+ const targetPath1 = path . resolve ( cachePluginPath , path . relative ( pluginTmpPath , item . path ) . slice ( 0 , - 4 ) )
152
+ fs . outputFileSync ( targetPath1 , result )
153
+ return
142
154
}
143
- const result = ejs . render ( template , fileConfig )
144
- const targetPath1 = path . resolve ( cachePluginPath , path . relative ( pluginTmpPath , item . path )
145
- . slice ( 0 , - 4 ) )
146
- fs . outputFileSync ( targetPath1 , result )
147
- return
148
- }
149
- // 拷贝其他文件
150
- const targetPath1 = path . resolve ( cachePluginPath , path . relative ( pluginTmpPath , item . path ) )
151
- fs . copySync ( item . path , targetPath1 )
152
- } )
155
+ // 拷贝其他文件
156
+ const targetPath1 = path . resolve ( cachePluginPath , path . relative ( pluginTmpPath , item . path ) )
157
+ fs . copySync ( item . path , targetPath1 )
158
+ } )
153
159
154
- return config
155
- } ) . then ( ( answers ) => {
156
- // 复制 .cache 到 plugin
157
- const sourcePath = path . resolve ( __dirname , './.cache/plugin' )
158
- const targetPath = path . resolve ( __dirname , `../src/plugins/${ answers . camelCaseName } ` )
159
- fs . copySync ( sourcePath , targetPath )
160
-
161
- console . log ( chalk . green ( `创建插件 ${ answers . name } : ${ targetPath } ` ) )
162
- // eslint-disable-next-line
163
- } ) . then ( ( ) => {
164
- // eslint-disable-next-line
165
- require ( './plugin-get-config.js' ) ;
166
- // eslint-disable-next-line
167
- } ) . catch ( ( err ) => {
168
- // eslint-disable-next-line
169
- console . log ( chalk . red ( '创建插件失败' ) )
170
- console . error ( err )
171
- process . exit ( 1 )
172
- } )
160
+ return config
161
+ } )
162
+ . then ( answers => {
163
+ // 复制 .cache 到 plugin
164
+ const sourcePath = path . resolve ( __dirname , './.cache/plugin' )
165
+ const targetPath = path . resolve ( __dirname , `../src/plugin/${ answers . name } ` )
166
+ fs . copySync ( sourcePath , targetPath )
167
+
168
+ console . log ( chalk . green ( `创建插件 ${ answers . name } : ${ targetPath } ` ) )
169
+ // eslint-disable-next-line
170
+ } )
171
+ . then ( ( ) => {
172
+ // eslint-disable-next-line
173
+ require ( './plugin-get-config.js' )
174
+ // eslint-disable-next-line
175
+ } )
176
+ . catch ( err => {
177
+ // eslint-disable-next-line
178
+ console . log ( chalk . red ( '创建插件失败' ) )
179
+ console . error ( err )
180
+ process . exit ( 1 )
181
+ } )
0 commit comments