1
1
const { promisify} = require ( 'util' ) ;
2
2
const path = require ( 'path' ) ;
3
+ const fs = require ( 'fs' ) ;
3
4
4
5
const program = require ( 'commander' ) ;
5
6
const downloadRepo = promisify ( require ( 'download-git-repo' ) ) ;
6
7
const open = require ( 'open' ) ;
7
8
8
9
const log = require ( '../utils/log' ) ;
9
10
const terminal = require ( '../utils/terminal' ) ;
10
- const { ejsCompile, writeFile} = require ( '../utils/file' ) ;
11
+ const { ejsCompile, writeFile, mkdirSync } = require ( '../utils/file' ) ;
11
12
const repoConfig = require ( '../config/repo_config' ) ;
12
13
13
14
const createProject = async ( project , otherArg ) => {
@@ -19,7 +20,7 @@ const createProject = async (project, otherArg) => {
19
20
20
21
// 3.执行终端命令npm install
21
22
// terminal.exec('npm install', {cwd: `./${project}`});
22
- const npm = project . platform === 'win32' ?'npm.cmd' :'npm' ;
23
+ const npm = process . platform === 'win32' ?'npm.cmd' :'npm' ;
23
24
await terminal . spawn ( npm , [ 'install' ] , { cwd :`./${ project } ` } ) ;
24
25
25
26
// 4.打开浏览器
@@ -29,19 +30,35 @@ const createProject = async (project, otherArg) => {
29
30
await terminal . spawn ( npm , [ 'run' , 'serve' ] , { cwd :`./${ project } ` } ) ;
30
31
}
31
32
32
- const addComponent = async ( name ) => {
33
- console . log ( name , program . dest ) ;
34
-
33
+ const handleEjsToFile = async ( name , dest , template , filename ) => {
35
34
// 1.获取模块引擎的路径
36
- const templatePath = path . resolve ( __dirname , '../ template/component.vue.ejs' ) ;
37
- const result = await ejsCompile ( templatePath , { name} ) ;
35
+ const templatePath = path . resolve ( __dirname , template ) ;
36
+ const result = await ejsCompile ( templatePath , { name, lowerName : name . toLowerCase ( ) } ) ;
38
37
39
38
// 2.写入文件中
40
- const targetPath = path . resolve ( program . dest , `${ name } .vue` ) ;
39
+ // 判断文件不存在,那么就创建文件
40
+ mkdirSync ( dest ) ;
41
+ const targetPath = path . resolve ( dest , filename ) ;
41
42
writeFile ( targetPath , result ) ;
42
43
}
43
44
45
+ const addComponent = async ( name , dest ) => {
46
+ handleEjsToFile ( name , dest , '../template/component.vue.ejs' , `${ name } .vue` ) ;
47
+ }
48
+
49
+ const addPage = async ( name , dest ) => {
50
+ addComponent ( name , dest ) ;
51
+ handleEjsToFile ( name , dest , '../template/vue-router.js.ejs' , 'router.js' )
52
+ }
53
+
54
+ const addStore = async ( name , dest ) => {
55
+ handleEjsToFile ( name , dest , '../template/vue-store.js.ejs' , 'index.js' )
56
+ handleEjsToFile ( name , dest , '../template/vue-types.js.ejs' , 'types.js' )
57
+ }
58
+
44
59
module . exports = {
45
60
createProject,
46
- addComponent
61
+ addComponent,
62
+ addPage,
63
+ addStore
47
64
}