-
-
Notifications
You must be signed in to change notification settings - Fork 975
/
widget-helpers.js
80 lines (67 loc) · 2.21 KB
/
widget-helpers.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
79
80
import { promises as fs } from "fs";
import path from "path";
import yaml from "js-yaml";
import checkAndCopyConfig from "utils/config/config";
const exemptWidgets = ["search"];
export async function widgetsFromConfig() {
checkAndCopyConfig("widgets.yaml");
const widgetsYaml = path.join(process.cwd(), "config", "widgets.yaml");
const fileContents = await fs.readFile(widgetsYaml, "utf8");
const widgets = yaml.load(fileContents);
if (!widgets) return [];
// map easy to write YAML objects into easy to consume JS arrays
const widgetsArray = widgets.map((group, index) => ({
type: Object.keys(group)[0],
options: {
index,
...group[Object.keys(group)[0]]
},
}));
return widgetsArray;
}
export async function cleanWidgetGroups(widgets) {
return widgets.map((widget, index) => {
const sanitizedOptions = widget.options;
const optionKeys = Object.keys(sanitizedOptions);
if (!exemptWidgets.includes(widget.type)) {
["url", "username", "password", "key"].forEach((pO) => {
if (optionKeys.includes(pO)) {
// allow URL in search
if (widget.type !== "search" && pO !== "key") {
delete sanitizedOptions[pO];
}
}
});
}
return {
type: widget.type,
options: {
index,
...sanitizedOptions
},
}
});
}
export async function getPrivateWidgetOptions(type, widgetIndex) {
const widgets = await widgetsFromConfig();
const privateOptions = widgets.map((widget) => {
const {
index,
url,
username,
password,
key
} = widget.options;
return {
type: widget.type,
options: {
index,
url,
username,
password,
key
},
}
});
return (type !== undefined && widgetIndex !== undefined) ? privateOptions.find(o => o.type === type && o.options.index === parseInt(widgetIndex, 10))?.options : privateOptions;
}