2022-09-26 12:04:37 +03:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
import { promises as fs } from "fs";
|
|
|
|
import path from "path";
|
|
|
|
|
|
|
|
import yaml from "js-yaml";
|
|
|
|
|
2022-10-09 09:55:36 +00:00
|
|
|
import checkAndCopyConfig, { getSettings } from "utils/config/config";
|
2022-09-26 15:25:10 +03:00
|
|
|
import { servicesFromConfig, servicesFromDocker, cleanServiceGroups } from "utils/config/service-helpers";
|
2022-10-11 20:26:07 -07:00
|
|
|
import { cleanWidgetGroups, widgetsFromConfig } from "utils/config/widget-helpers";
|
2022-09-26 12:04:37 +03:00
|
|
|
|
|
|
|
export async function bookmarksResponse() {
|
|
|
|
checkAndCopyConfig("bookmarks.yaml");
|
|
|
|
|
|
|
|
const bookmarksYaml = path.join(process.cwd(), "config", "bookmarks.yaml");
|
|
|
|
const fileContents = await fs.readFile(bookmarksYaml, "utf8");
|
|
|
|
const bookmarks = yaml.load(fileContents);
|
|
|
|
|
2022-09-29 11:40:09 +03:00
|
|
|
if (!bookmarks) return [];
|
|
|
|
|
2022-09-26 12:04:37 +03:00
|
|
|
// map easy to write YAML objects into easy to consume JS arrays
|
|
|
|
const bookmarksArray = bookmarks.map((group) => ({
|
|
|
|
name: Object.keys(group)[0],
|
|
|
|
bookmarks: group[Object.keys(group)[0]].map((entries) => ({
|
|
|
|
name: Object.keys(entries)[0],
|
|
|
|
...entries[Object.keys(entries)[0]][0],
|
|
|
|
})),
|
|
|
|
}));
|
|
|
|
|
|
|
|
return bookmarksArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function widgetsResponse() {
|
2022-10-11 20:26:07 -07:00
|
|
|
let configuredWidgets;
|
2022-09-26 12:04:37 +03:00
|
|
|
|
2022-10-11 20:26:07 -07:00
|
|
|
try {
|
|
|
|
configuredWidgets = cleanWidgetGroups(await widgetsFromConfig());
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to load widgets, please check widgets.yaml for errors or remove example entries.");
|
|
|
|
if (e) console.error(e);
|
|
|
|
configuredWidgets = [];
|
|
|
|
}
|
2022-09-26 12:04:37 +03:00
|
|
|
|
2022-10-11 20:26:07 -07:00
|
|
|
return configuredWidgets;
|
2022-09-26 12:04:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function servicesResponse() {
|
|
|
|
let discoveredServices;
|
|
|
|
let configuredServices;
|
2022-10-09 09:55:36 +00:00
|
|
|
let initialSettings;
|
2022-09-26 12:04:37 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
discoveredServices = cleanServiceGroups(await servicesFromDocker());
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to discover services, please check docker.yaml for errors or remove example entries.");
|
2022-12-22 21:16:52 -08:00
|
|
|
if (e) console.error(e.toString());
|
2022-09-26 12:04:37 +03:00
|
|
|
discoveredServices = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
configuredServices = cleanServiceGroups(await servicesFromConfig());
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to load services.yaml, please check for errors");
|
2022-12-22 21:16:52 -08:00
|
|
|
if (e) console.error(e.toString());
|
2022-09-26 12:04:37 +03:00
|
|
|
configuredServices = [];
|
|
|
|
}
|
|
|
|
|
2022-10-09 09:55:36 +00:00
|
|
|
try {
|
|
|
|
initialSettings = await getSettings();
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to load settings.yaml, please check for errors");
|
2022-12-22 21:16:52 -08:00
|
|
|
if (e) console.error(e.toString());
|
2022-10-09 09:55:36 +00:00
|
|
|
initialSettings = {};
|
|
|
|
}
|
|
|
|
|
2022-09-26 12:04:37 +03:00
|
|
|
const mergedGroupsNames = [
|
|
|
|
...new Set([discoveredServices.map((group) => group.name), configuredServices.map((group) => group.name)].flat()),
|
|
|
|
];
|
|
|
|
|
2022-11-21 00:29:20 -08:00
|
|
|
const sortedGroups = [];
|
|
|
|
const unsortedGroups = [];
|
2022-10-27 02:19:42 -07:00
|
|
|
const definedLayouts = initialSettings.layout ? Object.keys(initialSettings.layout) : null;
|
2022-09-26 12:04:37 +03:00
|
|
|
|
|
|
|
mergedGroupsNames.forEach((groupName) => {
|
|
|
|
const discoveredGroup = discoveredServices.find((group) => group.name === groupName) || { services: [] };
|
|
|
|
const configuredGroup = configuredServices.find((group) => group.name === groupName) || { services: [] };
|
|
|
|
|
|
|
|
const mergedGroup = {
|
|
|
|
name: groupName,
|
|
|
|
services: [...discoveredGroup.services, ...configuredGroup.services].filter((service) => service),
|
|
|
|
};
|
|
|
|
|
2022-10-27 02:19:42 -07:00
|
|
|
if (definedLayouts) {
|
|
|
|
const layoutIndex = definedLayouts.findIndex(layout => layout === mergedGroup.name);
|
2022-11-21 00:29:20 -08:00
|
|
|
if (layoutIndex > -1) sortedGroups[layoutIndex] = mergedGroup;
|
|
|
|
else unsortedGroups.push(mergedGroup);
|
2022-10-27 02:19:42 -07:00
|
|
|
} else {
|
2022-11-21 00:29:20 -08:00
|
|
|
unsortedGroups.push(mergedGroup);
|
2022-10-09 09:55:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-21 00:29:20 -08:00
|
|
|
return [...sortedGroups.filter(g => g), ...unsortedGroups];
|
2022-09-26 12:04:37 +03:00
|
|
|
}
|