-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.js
91 lines (68 loc) · 2.08 KB
/
base.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
const fs = require('fs');
const fse = require('fs-extra');
const glob = require('glob');
const path = require('path');
const readline = require('readline');
const os = require('os');
const func = require('./utils/func');
module.exports = function(argv, location){
/**
* location处理: list & card
* list示例:
* location: list locationUpper: LIST locationUpperFirst: List
*/
var location = location.toLowerCase();
var locationUpper = location.toUpperCase();
var locationUpperFirst = func.UpperFirstLetter(location);
/**
* 模板内容替换准备
*/
const REPLACE = {
type: /{TYPE}/g,
upperType:/{UPPERTYPE}/g,
name:/{NAME}/g,
}
var baseTemplate;
var lowerType,
upperType,
lowerName;
var typeAry = ['head', 'body'];
typeAry.map(function(item){
if(argv[item]){
baseTemplate = func.UpperFirstLetter(item);
lowerType = argv[item];
return
}
})
lowerName = argv.name.toLowerCase();
lowerType = lowerType.toLowerCase();
upperType = func.UpperFirstLetter(lowerType);
const OUT = {
name: lowerName,
type: lowerType,
upperType: upperType,
}
// console.log(OUT);
/**
* 模板替换
*/
const baseFilePath = path.join(__dirname, 'templates',
`${locationUpper}`, `{NAME}${locationUpperFirst}${baseTemplate}{TYPE}Action.js`);
const destFilePath = path.join(process.cwd(),`${OUT.name}${locationUpperFirst}${baseTemplate}${OUT.upperType}Action.js`);
var fRead = fs.createReadStream(baseFilePath);
var fWrite = fs.createWriteStream(destFilePath);
var rl = readline.createInterface({
input: fRead,
terminal: true
});
rl.on('line', (line) => {
var newLine = line
.replace(REPLACE.type, OUT.type)
.replace(REPLACE.upperType, OUT.upperType)
+ os.EOL;
fWrite.write(newLine);
});
rl.on('close', () => {
console.log(path.basename(destFilePath),'模板文件已经创建到:',destFilePath);
})
}