-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-data.js
More file actions
36 lines (33 loc) · 1.34 KB
/
Copy pathextract-data.js
File metadata and controls
36 lines (33 loc) · 1.34 KB
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
/* 零依赖:从 index.html 抽出 DATA / REDEMPTIONS / I18N 三个纯字面量,供 test.js / maintain.js 共用。
这些是纯数据/文案字面量(I18N 含箭头函数,但无外部调用),用 Function 求值安全。 */
const fs = require("fs");
const path = require("path");
const html = fs.readFileSync(path.join(__dirname, "index.html"), "utf8");
// 按花括号配平截取 `const NAME = { ... }` 的对象字面量
function extractLiteral(src, name) {
const start = src.indexOf("const " + name + " =");
if (start < 0) throw new Error("找不到 " + name);
const open = src.indexOf("{", start);
let depth = 0, inStr = null, esc = false;
for (let i = open; i < src.length; i++) {
const ch = src[i];
if (inStr) {
if (esc) { esc = false; }
else if (ch === "\\") esc = true;
else if (ch === inStr) inStr = null;
continue;
}
if (ch === '"' || ch === "'" || ch === "`") { inStr = ch; continue; }
if (ch === "{") depth++;
else if (ch === "}") { depth--; if (depth === 0) return src.slice(open, i + 1); }
}
throw new Error(name + " 花括号未配平");
}
const evalLiteral = name => new Function("return (" + extractLiteral(html, name) + ")")();
module.exports = {
html,
extractLiteral,
DATA: evalLiteral("DATA"),
REDEMPTIONS: evalLiteral("REDEMPTIONS"),
I18N: evalLiteral("I18N"),
};