-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
180 lines (172 loc) · 4.92 KB
/
index.ts
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import fs from "fs";
import path from "path";
import { CFG } from "./config";
import { XLSXDumper } from "./XLSXDumper";
import { Ruler } from "./Rule";
import { Capitalize, Exit } from "./cmm";
import { RSON } from "./rson";
/**
* 导出配置
*/
function dump() {
Ruler.initialize();
const dirPath = path.join(__dirname, CFG.TABLE);
const files = fs.readdirSync(dirPath);
for (const file of files) {
if (file.endsWith(".xlsx") && !file.startsWith("~$")) {
XLSXDumper(path.join(dirPath, file));
}
}
}
/**
* 合并配置
*/
function merge() {
mergeAllJSON();
mergeAllTS();
mergeAllDTS();
}
/**
* 合并所有JSON
*/
function mergeAllJSON() {
const all: Record<string, string> = {};
const dirPath = path.join(__dirname, CFG.JSON);
const files = fs.readdirSync(dirPath);
for (const file of files) {
if (file.endsWith(".json")) {
const json = JSON.parse(fs.readFileSync(path.join(dirPath, file), "utf-8"));
all[path.basename(file, ".json")] = json;
}
}
const MINIFIED_AT = path.join(__dirname, CFG.MINIFIED);
const JSON_AT = path.join(MINIFIED_AT, CFG.MERGE_AS + ".json");
const BIN_AT = path.join(MINIFIED_AT, CFG.MERGE_AS + ".bin");
if (!fs.existsSync(MINIFIED_AT)) {
fs.mkdirSync(path.dirname(MINIFIED_AT));
}
const json = JSON.stringify(all, null, 0);
const bin = RSON.encodeAsU8(all);
fs.writeFileSync(JSON_AT, json, "utf-8");
fs.writeFileSync(BIN_AT, bin, "utf-8");
}
/**
* 合并所有TS
*/
function mergeAllTS() {
const all = [];
const dirPath = path.join(__dirname, CFG.TS);
const files = fs.readdirSync(dirPath);
for (const file of files) {
if (file.endsWith(".ts")) {
const ts = fs.readFileSync(path.join(dirPath, file), "utf-8");
all.push(ts);
}
}
const MINIFIED_AT = path.join(__dirname, CFG.MINIFIED);
const TS_AT = path.join(MINIFIED_AT, CFG.MERGE_AS + ".ts");
if (!fs.existsSync(MINIFIED_AT)) {
fs.mkdirSync(path.dirname(MINIFIED_AT));
}
const ts = all.join("\n\n");
fs.writeFileSync(TS_AT, ts, "utf-8");
}
/**
* 合并所有DTS
*/
function mergeAllDTS() {
const all = [];
const dirPath = path.join(__dirname, "types");
const files = fs.readdirSync(dirPath);
const tables = [" type Tables = {"];
for (const file of files) {
if (file.endsWith(".d.ts")) {
const name = path.basename(file, ".d.ts");
tables.push(" " + name + ": ITbl" + Capitalize(name) + ";");
let dts = fs.readFileSync(path.join(dirPath, file), "utf-8");
dts = dts
.split("\n")
.map((line) => " " + line)
.join("\n");
all.push(dts);
}
}
tables.push(" }");
all.push(tables.join("\n"));
const MINIFIED_AT = path.join(__dirname, CFG.MINIFIED);
const TS_AT = path.join(MINIFIED_AT, CFG.MERGE_AS + ".d.ts");
if (!fs.existsSync(MINIFIED_AT)) {
fs.mkdirSync(MINIFIED_AT);
}
const name = Capitalize(CFG.MERGE_AS);
const dts = all.join("\n\n");
const content = [
`export = ${name};`,
`export as namespace ${CFG.MERGE_AS};\n`,
`declare namespace ${name} {`,
dts,
"}",
].join("\n");
fs.writeFileSync(TS_AT, content, "utf-8");
}
/**
* 解析BIN文件
* @param bin BIN文件路径
*/
function extractBin(bin: string) {
if (fs.existsSync(bin)) {
const data = fs.readFileSync(bin);
const json = RSON.decodeFromU8(new Uint8Array(data.buffer));
console.log(json);
} else {
Exit(-1, "请输入正确的BIN文件路径");
}
}
/**
* 清理所有文件
*/
function clear() {
const MINIFIED_AT = path.join(__dirname, CFG.MINIFIED);
const JSON_AT = path.join(__dirname, CFG.JSON);
const BIN_AT = path.join(__dirname, CFG.BIN);
const TS_AT = path.join(__dirname, CFG.TS);
const DTS_AT = path.join(__dirname, "types");
[MINIFIED_AT, JSON_AT, BIN_AT, TS_AT, DTS_AT].forEach((path) => {
if (fs.existsSync(path)) {
fs.rmSync(path, { recursive: true });
fs.mkdirSync(path);
}
});
}
/**
* 主程序
*/
const exe = process.argv[2];
switch (exe) {
case "-e":
case "--extract":
extractBin(process.argv[3]);
break;
case "-m":
case "--merge":
merge();
break;
case undefined:
case "-d":
case "--dump":
dump();
break;
case "-c":
case "--clear":
clear();
break;
case "-h":
case "--help":
default:
console.log("-c/--clear 清理所有文件");
console.log("-d/--dump 导出表格配置");
console.log("-e/--extract <bin> 解析BIN文件");
console.log("-m/--merge 合并所有输出");
console.log("-h/--help 查看帮助");
break;
}