Files
Homarr/src/tools/config/migrateConfig.ts

136 lines
3.0 KiB
TypeScript
Raw Normal View History

import { v4 as uuidv4 } from 'uuid';
import { AppType } from '../../types/app';
import { AreaType } from '../../types/area';
import { CategoryType } from '../../types/category';
2022-12-22 11:29:51 +09:00
import { ConfigType } from '../../types/config';
import { Config, serviceItem } from '../types';
2022-12-22 11:29:51 +09:00
export function migrateConfig(config: Config): ConfigType {
2022-12-22 11:29:51 +09:00
const newConfig: ConfigType = {
2022-12-24 17:18:16 +09:00
schemaVersion: 1,
2022-12-22 11:29:51 +09:00
configProperties: {
name: config.name ?? 'default',
},
categories: [],
widgets: [],
apps: [],
settings: {
common: {
searchEngine: {
type: 'google',
properties: {
enabled: true,
openInNewTab: true,
},
},
defaultConfig: 'default',
},
customization: {
colors: {},
layout: {
enabledDocker: false,
enabledLeftSidebar: false,
enabledPing: false,
enabledRightSidebar: false,
enabledSearchbar: true,
},
},
},
wrappers: [
{
id: 'default',
position: 1,
},
],
};
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 } })
);
});
2022-12-22 11:29:51 +09:00
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;
};
2023-01-06 22:46:07 +01:00
const getShapeForColumnCount = (index: number, columnCount: number) => ({
location: {
x: index % columnCount,
y: Math.floor(index / columnCount),
},
size: {
width: 1,
height: 1,
},
});
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: {
2023-01-06 22:46:07 +01:00
lg: getShapeForColumnCount(serviceIndex, 12),
md: getShapeForColumnCount(serviceIndex, 6),
sm: getShapeForColumnCount(serviceIndex, 3),
},
});