-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
78 lines (62 loc) · 1.81 KB
/
index.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
const path = require('node:path');
const fs = require('node:fs');
function getModuleInfo(sysmodule, locale) {
const localePath = path.join(__dirname, 'data', sysmodule, `${locale.replace('-', '_')}.json`);
const defaultPath = path.join(__dirname, 'data', sysmodule, 'en_US.json');
let content;
if (fs.existsSync(localePath)) {
content = fs.readFileSync(localePath, {
encoding: 'utf8'
});
} else if (fs.existsSync(defaultPath)) {
content = fs.readFileSync(defaultPath, {
encoding: 'utf8'
});
} else {
return null;
}
return JSON.parse(content);
}
function getErrorInfo(sysmodule, code, locale) {
const moduleInfo = getModuleInfo(sysmodule, locale);
if (!moduleInfo) {
return null;
}
const localePath = path.join(__dirname, 'data', sysmodule, code, `${locale.replace('-', '_')}.json`);
const defaultPath = path.join(__dirname, 'data', sysmodule, code, 'en_US.json');
let content;
if (fs.existsSync(localePath)) {
content = fs.readFileSync(localePath, {
encoding: 'utf8'
});
} else if (fs.existsSync(defaultPath)) {
content = fs.readFileSync(defaultPath, {
encoding: 'utf8'
});
} else {
return null;
}
const errorInfo = JSON.parse(content)[sysmodule][code];
errorInfo.module = moduleInfo[sysmodule];
return errorInfo;
}
function getAllErrors() {
const errors = [];
const modules = fs.readdirSync(path.join(__dirname, 'data'), { withFileTypes: true });
for (const moduleDirent of modules) {
if (moduleDirent.isDirectory()) {
const errorCodes = fs.readdirSync(path.join(__dirname, 'data', moduleDirent.name), { withFileTypes: true });
for (const errorDirent of errorCodes) {
if (errorDirent.isDirectory()) {
errors.push(`${moduleDirent.name}-${errorDirent.name}`);
}
}
}
}
return errors;
}
module.exports = {
getModuleInfo,
getErrorInfo,
getAllErrors
};