-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·152 lines (118 loc) · 3.85 KB
/
index.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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const dot = require('dot')
const program = require('commander')
const _ = require('lodash')
const insertLine = require('insert-line')
const readline = require('linebyline')
const curDir = __dirname//获取当前目录
//初始化commander
program
.allowUnknownOption()
.version('0.0.1')
.usage('template <cmd> [input]')
//定义模板参数
let templateParam = {}
//添加自定义命令
program
.command('page')
.description('输入页面名称')
.action(function (pageName) {
if (_.isString(pageName)) {
processPage(pageName)
} else {
program.help()
}
})
//处理page
processPage = (pageName) => {
templateParam.name = pageName + 'Page'
const templateFileName = 'page.js'
loadTemplateCode(templateFileName)
.then(template => {
let source = instantiateTemplateCode(templateParam, template)
let fileName = './src/page/' + templateParam.name + '.js';
fs.stat(fileName, (err, stat) => {
if (stat && stat.isFile()) {
console.log('文件已经存在');
} else {
writeSource(fileName, source)
.then(() => {
console.log('创建成功')
processFile(pageName)
})
}
})
})
.catch(error => {
console.log(error);
})
}
//处理文件
processFile = (pageName) => {
let insertFirstLine=0
let insertSecondLine=0
let routerSettingFilePath = './src/constants/routerSetting.js'
let routerPathFilePath='./src/constants/RouterPaths.js'
let importStr = 'import ' + pageName + ' from ' + '\'../page/' + pageName + 'Page\''
let routerStr = ' '+pageName + ':{' + 'screen:' + pageName + '},'
let pathStr=' '+pageName+':\''+pageName+'\','
let settingRl = readline(routerSettingFilePath)
settingRl.on('line', (line, lineCount, byteCount) => {
// console.log('line:' + line, ' lineCount' + lineCount)
if (line.indexOf('insert-import') != -1) {
insertFirstLine=lineCount
}
if (line.indexOf('insert-router') != -1) {
insertSecondLine=lineCount
}
if(insertFirstLine!==0&&insertSecondLine!==0){
insertLine(routerSettingFilePath).content(importStr).at(insertFirstLine).then(err=>{
insertLine(routerSettingFilePath).content(routerStr).at(insertSecondLine+1).then(err=>{})
})
}
})
let pathRl=readline(routerPathFilePath)
pathRl.on('line', (line, lineCount, byteCount) => {
if (line.indexOf('insert-path') != -1) {
insertLine(routerPathFilePath).content(pathStr).at(lineCount).then(err=>{})
}
})
}
// 写入代码到当前目录下
writeSource = (fileName, source) => {
return new Promise((resolve, reject) => {
fs.writeFile(fileName, source, err => {
if (err) {
reject(err);
} else {
resolve(fileName);
}
})
});
}
//实例化模板代码
instantiateTemplateCode = (templateParam, templateSource) => {
dot.templateSettings.strip = false;
let template = dot.template(templateSource);
return template(templateParam);
}
//加载模板代码
loadTemplateCode = (templateFileName) => {
return new Promise((resolve, reject) => {
fs.readFile(path.join(curDir + '/template', templateFileName), 'utf-8', (err, data) => {
if (err) {
reject(err)
} else {
resolve(data)
}
})
})
}
// 没有参数时显示帮助信息
if (!process.argv[2]) {
program.help();
console.log();
}
program.parse(process.argv)