-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.js
155 lines (134 loc) · 4.72 KB
/
generator.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
const path = require('path');
const fs = require('fs');
const readLine = require('readline');
const FILE_SUFFIX_NAME = 'js'
const CSV_LIST_SEPARATOR = '|';
class Generator {
constructor(inputFile, outputDir) {
this.inputFile = inputFile;
this.outputDir = outputDir;
}
generate() {
this.obtainMapData().then(dataMap => {
this.generateConstantFile(dataMap);
this.generateEnumFile(dataMap);
});
}
generateConstantFile(dataMap) {
for (let attribute of this.attributeList) {
let className = attribute;
let filePath = this.obtainOutputFilePath(className);
let content = this.obtainConstantTemplate(attribute, dataMap);
this.generateFile(filePath, content);
}
}
generateEnumFile(dataMap) {
let className = 'error_enum';
let filePath = this.obtainOutputFilePath(className);
let content = this.obtainEnumTemplate(dataMap);
this.generateFile(filePath, content);
}
obtainEnumTemplate(dataMap) {
let builder = [];
builder.push("module.exports = Object.freeze({\n");
builder.push(this.obtainEnumDataList(dataMap));
builder.push("});\n");
return builder.join("");
}
obtainEnumDataList(dataMap) {
let builder = [];
let propertyList = this.obtainEnumPropertyList();
let nameList = Object.keys(dataMap);
for (let index = 0; index < nameList.length; index++) {
let separator = index < nameList.length - 1 ? "," : "";
let name = nameList[index];
let propertyArrString = propertyList.map(property => `${property}: "${dataMap[name][property]}"`).join(", ");
builder.push(`\t${name}: {${propertyArrString}}${separator}\n`);
}
return builder.join("");
}
obtainEnumPropertyList() {
let list = [];
for (let index = 1; index < this.attributeList.length; index++) {
let tag = this.attributeList[index];
list.push(tag);
}
return list;
}
obtainConstantTemplate(attribute, map) {
let builder = [];
builder.push("module.exports = Object.freeze({\n");
for (const [key, value] of Object.entries(map)) {
let format = `\t${key}: \"${value[attribute]}\",\n`;
builder.push(format);
}
builder.push("})\r\n");
return builder.join("");
}
async obtainMapData() {
let rowList = await this.parseFile(this.inputFile);
let dataList = rowList.map(row => this.splitRowData(row));
this.attributeList = dataList.shift();
let dataMap = {};
const attributeSize = this.attributeList.length;
for (let rowDataList of dataList) {
this.fillList(rowDataList, attributeSize);
let name = this.obtainKeyName(rowDataList[0]);
let itemMap = {};
dataMap[name] = itemMap;
for (let index = 0; index < attributeSize; index++) {
let attribute = this.attributeList[index];
itemMap[attribute] = rowDataList[index];
}
}
return dataMap
}
splitRowData(row) {
let rowList = row.split(CSV_LIST_SEPARATOR);
return rowList.map(data => data.trim());
}
fillList(list, size) {
if (list.length < size) {
for (let index = 0; index < size - list.length; index++) {
list.push("");
}
}
return list;
}
obtainKeyName(name) {
let regex = new RegExp("\\s+", "g");
return name.trim().replace(regex, "_").toUpperCase();
}
obtainOutputFilePath(name) {
this.preGenerateDir(outputDir);
return `${this.outputDir}${path.sep}${name}.${FILE_SUFFIX_NAME}`;
}
preGenerateDir(directory) {
fs.mkdir(directory, () => {});
}
generateFile(filePath, content) {
fs.writeFile(filePath, content, function (err) {
if (err) {
console.error(err);
}
})
}
parseFile(filePath) {
return new Promise(function (resolve, reject) {
try {
let list = [];
readLine.createInterface({
input: fs.createReadStream(filePath)
})
.on('line', (line) => list.push(line))
.on('close', () => resolve(list));
} catch (err) {
reject(err);
}
})
}
}
const currentPath = path.resolve('./');
const inputFile = currentPath + path.sep + 'error-code.csv';
const outputDir = currentPath + path.sep + 'output' + path.sep + 'javascript';
new Generator(inputFile, outputDir).generate();