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

41 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-12-31 16:07:05 +01:00
import Consola from 'consola';
2023-03-30 21:46:59 +02:00
import { v4 as uuidv4 } from 'uuid';
2023-07-21 18:08:40 +09:00
2023-01-08 15:48:39 +09:00
import { BackendConfigType, ConfigType } from '../../types/config';
2022-12-11 14:11:25 +01:00
import { configExists } from './configExists';
import { getFallbackConfig } from './getFallbackConfig';
import { readConfig } from './readConfig';
2023-03-30 21:46:59 +02:00
import { writeConfig } from './writeConfig';
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
2022-12-11 14:11:25 +01:00
export const getConfig = (name: string): BackendConfigType => {
2023-01-08 15:48:39 +09:00
if (!configExists(name)) return getFallbackConfig() as unknown as ConfigType;
2022-12-22 11:29:51 +09:00
// Else if config exists but contains no "schema_version" property
// then it is an old config file and we should try to migrate it
// to the new format.
2022-12-24 17:18:16 +09:00
const config = readConfig(name);
2022-12-31 16:07:05 +01:00
2023-03-30 21:46:59 +02:00
let backendConfig = config as BackendConfigType;
if (backendConfig.widgets.some((widget) => !uuidRegex.test(widget.id))) {
backendConfig = {
...backendConfig,
widgets: backendConfig.widgets.map((widget) => ({
...widget,
2023-03-30 23:35:29 +02:00
id: uuidRegex.test(widget.id) ? widget.id : uuidv4(),
type: !uuidRegex.test(widget.id) ? widget.id : widget.type,
2023-03-30 21:46:59 +02:00
})),
};
Consola.log(
'Migrating config file to multiple widgets...',
backendConfig.configProperties.name
);
writeConfig(backendConfig);
}
return backendConfig;
2022-12-11 14:11:25 +01:00
};