-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict-check.ts
67 lines (55 loc) · 1.87 KB
/
dict-check.ts
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
import dictionary from "./apps/web/lib/dictionary";
import clc from "cli-color";
interface Dictionary {
[key: string]: DictionaryValue;
}
interface DictionaryValue {
[key: string]: DictionaryValue | string;
}
interface retVal {
trace: string[];
missing: string[];
}
const hasMissingProperties = (obj: DictionaryValue): string[] =>
["en", "pl", "fr"].filter((key) => !(key in obj));
const hasChildren = (obj: DictionaryValue): boolean => {
return Object.values(obj).some((value) => typeof value === "object");
};
const checkDict = (obj: Dictionary, trace: string[] = []): retVal[] =>
Object.entries(obj).reduce((acc: retVal[], [key, value]) => {
const currentTrace = [...trace, key];
if (hasChildren(value as DictionaryValue)) {
return acc.concat(checkDict(value as Dictionary, currentTrace));
}
const missingProperties = hasMissingProperties(value as DictionaryValue);
if (missingProperties.length > 0) {
return acc.concat({ trace: currentTrace, missing: missingProperties });
}
return acc;
}, []);
const validate = () => {
console.log("Checking dictionary...");
const res = checkDict(dictionary);
if (res.length) {
const errorMsg =
`${`The following ${
res.length > 1 ? `${clc.yellow("keys")} are` : `${clc.yellow("key")} is`
} causing build to fail ❌`}` +
`\n\n${res
.map(
(parent) =>
`• ${parent.trace.map((val, idx) =>
idx === parent.trace.length - 1
? clc.yellow(val)
: `${clc.blueBright(val)} => `
)} is ${clc.red.underline("missing")} [${parent.missing.join(
" "
)}] ${parent.missing.length > 1 ? "keys" : "key"} \n`
)
.join("")}`.replaceAll(",", "");
throw new Error(errorMsg);
} else {
console.log(`${clc.greenBright("All clear")} ✅`);
}
};
validate();