♻️ Add compability for legacy config in config loader

This commit is contained in:
Manuel Ruwe
2022-12-31 23:31:41 +01:00
parent 2eb5cdfafc
commit 75f6029057
10 changed files with 219 additions and 115 deletions

View File

@@ -1,8 +1,11 @@
import fs from 'fs';
import { v4 as uuidv4 } from 'uuid';
import { AppType } from '../../types/app';
import { AreaType } from '../../types/area';
import { CategoryType } from '../../types/category';
import { ConfigType } from '../../types/config';
import { Config } from '../types';
import { Config, serviceItem } from '../types';
export function migrateConfig(config: Config, name: string): ConfigType {
export function migrateConfig(config: Config): ConfigType {
const newConfig: ConfigType = {
schemaVersion: 1,
configProperties: {
@@ -41,45 +44,86 @@ export function migrateConfig(config: Config, name: string): ConfigType {
],
};
newConfig.apps = config.services.map((s, idx) => ({
name: s.name,
id: s.id,
url: s.url,
behaviour: {
isOpeningNewTab: s.newTab ?? true,
externalUrl: s.openedUrl ?? '',
},
network: {
enabledStatusChecker: s.ping ?? true,
okStatus: s.status?.map((str) => parseInt(str, 10)) ?? [200],
},
appearance: {
iconUrl: s.icon,
},
integration: {
type: null,
properties: [],
},
area: {
type: 'wrapper',
properties: {
id: 'default',
},
},
shape: {
location: {
x: (idx * 3) % 18,
y: Math.floor(idx / 6) * 3,
},
size: {
width: 3,
height: 3,
},
},
}));
// Overrite the file ./data/configs/${name}.json
// with the new config format
fs.writeFileSync(`./data/configs/${name}.json`, JSON.stringify(newConfig, null, 2));
config.services.forEach((service, index) => {
const { category: categoryName } = service;
if (!categoryName) {
newConfig.apps.push(
migrateService(service, index, {
type: 'wrapper',
properties: {
id: 'default',
},
})
);
return;
}
const category = getConfigAndCreateIfNotExsists(newConfig, categoryName);
if (!category) {
return;
}
newConfig.apps.push(
migrateService(service, index, { type: 'category', properties: { id: category.id } })
);
});
return newConfig;
}
const getConfigAndCreateIfNotExsists = (
config: ConfigType,
categoryName: string
): CategoryType | null => {
const foundCategory = config.categories.find((c) => c.name === categoryName);
if (foundCategory) {
return foundCategory;
}
const category: CategoryType = {
id: uuidv4(),
name: categoryName,
position: 0,
};
config.categories.push(category);
return category;
};
const migrateService = (
oldService: serviceItem,
serviceIndex: number,
areaType: AreaType
): AppType => ({
id: uuidv4(),
name: oldService.name,
url: oldService.url,
behaviour: {
isOpeningNewTab: oldService.newTab ?? true,
externalUrl: oldService.openedUrl ?? '',
},
network: {
enabledStatusChecker: oldService.ping ?? true,
okStatus: oldService.status?.map((str) => parseInt(str, 10)) ?? [200],
},
appearance: {
iconUrl: oldService.icon,
},
integration: {
type: null,
properties: [],
},
area: areaType,
shape: {
location: {
x: (serviceIndex * 3) % 18,
y: Math.floor(serviceIndex / 6) * 3,
},
size: {
width: 3,
height: 3,
},
},
});