-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_ImportTOC.js
90 lines (79 loc) · 3.38 KB
/
static_ImportTOC.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
"use strict";
const fs = require("fs");
const glob = require("glob");
const readline = require("readline");
const jsbeautify = require("js-beautify").js;
const path = require("path");
let export_directory_toc = "../build/whxdata/";
let manual_directory_toc = "../GMS2-Robohelp-en/whxdata/";
let global_settings = require("../setting.json");
let json_global = require("../language/" + global_settings.group + "/global.json");
let regex = new RegExp("var toc \\= \\[\\{", "g");
glob(manual_directory_toc + "toc*.new.js", {}, (err, files) => {
for (let index = 0; index < files.length; index++) {
// 格式化所有目录文件
let toc_original = fs.readFileSync(files[index]).toString();
let toc_beautify = jsbeautify(toc_original, { indent_size: 4, space_in_empty_paren: true })
fs.writeFileSync(files[index], toc_beautify)
// 逐行读取内容
let toc_stream_read = fs.createReadStream(files[index])
let toc_filename = export_directory_toc + path.basename(files[index], ".js")
let toc_write_json = toc_filename + ".json"
let rl = readline.createInterface({
input: toc_stream_read
});
rl.on('line', (line) => {
// 古典方法删除不需要的行,足够无脑但真有用
switch (line.trim()) {
case "(function() {":
return "";
case "window.rh.model.publish(rh.consts('KEY_TEMP_DATA'), toc, {":
return "";
case "sync: true":
return "";
case "});":
return "";
case "})();":
return "";
}
// 保存处理过后的内容,且保存为JSON
fs.appendFileSync(toc_write_json, line.replace(regex, "[{").replace("}];", "}]"))
})
rl.on("close", ()=>{
if (fs.existsSync(toc_write_json)) {
let toc_json = require(toc_write_json)
for (let j = 0; j < toc_json.length; j++) {
// 匹配到转义字符
if (json_global[toc_json[j].name.replace("&", "&")]) {
if (toc_json[j].name !== json_global[toc_json[j].name.replace("&", "&")]) {
toc_json[j].name = json_global[toc_json[j].name.replace("&", "&")].replace("&", "&")
}
}
}
fs.writeFileSync(toc_write_json, JSON.stringify(toc_json, null, "\t"))
let toc_json_translated = require(toc_write_json)
let toc_write_js = toc_filename + ".js"
let toc_js_template = `
(function() {
var toc = ${JSON.stringify(toc_json_translated)};
window.rh.model.publish(rh.consts('KEY_TEMP_DATA'), toc, {
sync: true
});
})();
`
fs.writeFileSync(toc_write_js, toc_js_template)
}
})
}
});
glob(export_directory_toc + "toc*.new.json", {}, (err, files)=>{
if (err) {
console.log(err)
} else {
for (let index = 0; index < files.length; index++) {
if (fs.existsSync(files[index])) {
fs.rmSync(files[index])
}
}
}
});