-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathglossary.js
More file actions
71 lines (61 loc) · 1.65 KB
/
glossary.js
File metadata and controls
71 lines (61 loc) · 1.65 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
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
"use strict";
const fs = require("node:fs");
const glossary = require("./glossary.json");
const CONTENT_PATH_PREFIX = "./src/content";
const LOG_KEY = {
ERROR: "ERROR",
GLOSSARY: "GLOSSARY",
};
const docExtensions = ["md", "mdx"];
const log = (cmd, msg) => {
const color = ((cmd) => {
switch (cmd) {
case LOG_KEY.ERROR:
return 31;
case LOG_KEY.GLOSSARY:
return 34;
default:
return 33;
}
})(cmd);
const data = `\u001B[${color}m[${cmd}]\u001B[0m ${msg}`;
console.info(data);
};
const getTargetPaths = (cmd) =>
docExtensions.map(
(extension) => `${CONTENT_PATH_PREFIX}/${cmd}.${extension}`,
);
const getSplitLineData = (targetPath) => {
const fileData = fs.readFileSync(targetPath, "utf8");
const splitLines = fileData.toString().split("\n");
return splitLines;
};
const checkLine = (line, index) => {
try {
const tokens = line.split(" ").filter(Boolean);
for (const token of tokens) {
if (!glossary[token] || token === glossary[token]) continue;
log(LOG_KEY.GLOSSARY, `${index + 1}: ${token} -> ${glossary[token]}`);
}
} catch (err) {
log(LOG_KEY.ERROR, err.toString());
}
};
for (const [index, cmd] of process.argv.entries()) {
if (index < 2) continue;
log("CMD", cmd);
const targetPaths = getTargetPaths(cmd);
// Parallel start
for (const targetPath of targetPaths) {
log("READ START", targetPath);
try {
const splitLines = getSplitLineData(targetPath);
for (const [index, line] of splitLines.entries()) {
checkLine(line, index);
}
} catch (err) {
log(LOG_KEY.ERROR, "File not exist");
throw err;
}
}
}