-
Notifications
You must be signed in to change notification settings - Fork 35
/
glossary.js
68 lines (59 loc) · 1.57 KB
/
glossary.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
const fs = require('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 = `\x1b[${color}m[${cmd}]\x1b[0m ${msg}`;
console.info(data);
};
const getTargetPaths = (cmd) => {
return docExtensions.map((extension) => {
return `${CONTENT_PATH_PREFIX}/${cmd}.${extension}`;
});
};
const getSplitLineData = (targetPath) => {
const fileData = fs.readFileSync(targetPath, 'utf-8');
const splitLines = fileData.toString().split('\n');
return splitLines;
};
const checkLine = (line, index) => {
try {
const tokens = line.split(' ').filter((text) => text);
tokens.forEach((token) => {
if (!glossary[token] || token === glossary[token]) return;
log(LOG_KEY.GLOSSARY, `${index + 1}: ${token} -> ${glossary[token]}`);
});
} catch (e) {
log(LOG_KEY.ERROR, e.toString());
}
};
process.argv.forEach((cmd, index) => {
if (index < 2) return;
log('CMD', cmd);
const targetPaths = getTargetPaths(cmd);
// Parallel start
targetPaths.forEach((targetPath) => {
log('READ START', targetPath);
try {
const splitLines = getSplitLineData(targetPath);
splitLines.forEach(checkLine);
} catch (e) {
log(LOG_KEY.ERROR, 'File not exist');
throw e;
}
});
});