Adds ColorConfig to Config

This commit is contained in:
2023-08-14 09:34:10 +02:00
parent 4400eb5219
commit 1cd6e0ae1e
4 changed files with 75 additions and 20 deletions

View File

@@ -4,6 +4,11 @@ logo: 'icon-any.svg'
icon: 'fa fa-cloud'
asmask: false
colors:
dark:
test: red
services:
- title: '/Cloud'
subtitle: 'Private Cloud Utilities'

View File

@@ -23,8 +23,17 @@ export interface ServiceGroupConfig extends SectionConfig {
[x: string]: unknown;
}
export interface ColorConfig {
test: string;
}
export interface Config extends SectionConfig {
services: Array<ServiceGroupConfig>;
colors: {
light: ColorConfig;
dark: ColorConfig;
};
[x: string]: unknown;
}

View File

@@ -1,12 +1,6 @@
import { describe, expect, it } from 'vitest';
import {
defaultConfig,
mergeConfig,
type Config,
stripPrivateFields,
serverConfig,
clientConfig
} from './config';
import { defaultConfig, stripPrivateFields, serverConfig, clientConfig, merge } from './config';
import type { Config } from '$lib/config';
describe('Config', () => {
it('should be export a build time server and client config store', () => {
@@ -15,8 +9,13 @@ describe('Config', () => {
});
it('should be able to merge with POJO', () => {
const merged = mergeConfig(defaultConfig, {
const merged: Config = merge<Config>(defaultConfig, {
subtitle: "Homer's favorite neighboor",
colors: {
light: {
test: 'red'
}
},
services: [
{
title: 'favorite occupations',
@@ -28,10 +27,12 @@ describe('Config', () => {
]
}
]
});
}) as Config;
expect(merged.title).toEqual('Flanders');
expect(merged.services).toHaveLength(1);
expect(merged.colors.light).toEqual({ test: 'red' });
expect(merged.colors.dark).toEqual({});
expect(merged.services[0].title).toEqual('favorite occupations');
expect(merged.services[0].items).toHaveLength(1);
expect(merged.services[0].items[0].title).toEqual('anoy homer');
@@ -42,6 +43,10 @@ describe('Config', () => {
const custom: Config = {
title: 'custom',
secret: { secret: 'some secret' },
colors: {
light: {},
dark: {}
},
services: [
{
title: 'top services',

View File

@@ -30,21 +30,59 @@ export const requiredConfig: Required<Config> = {
logo: '',
icon: '',
asmask: false,
colors: {
light: {
test: ''
},
dark: {
test: ''
}
},
services: [requiredServiceGroup]
};
export function mergeConfig(a: Config, b: any): Config {
return { ...a, ...b };
type SPOJO = Record<string, unknown>;
export function merge<Type extends SPOJO>(a: Type, b: SPOJO): Type {
const res: Type = { ...a };
for (const [key, value] of Object.entries(b)) {
console.log(key, value, typeof value, typeof res[key]);
if (
typeof value != 'object' ||
res[key] == undefined ||
typeof res[key] != typeof res[key]
) {
(res as SPOJO)[key] = value;
continue;
}
if (value instanceof Array) {
const merged = [];
merged.push(...(res[key] as Array<unknown>));
merged.push(...value);
(res as SPOJO)[key] = merged;
continue;
}
(res as SPOJO)[key] = merge(res[key] as SPOJO, value as SPOJO);
}
return res;
}
export const defaultConfig: Config = {
title: 'Flanders',
colors: {
light: {
test: ''
},
dark: {
test: ''
}
},
services: []
};
type SPOJO = Record<string, unknown>;
function strip<Type extends SPOJO>(toStrip: Type, reference: Type): Type {
const res: Type = { ...toStrip };
const referenceNames = Object.entries(reference).map(([key, value]) => key);
@@ -70,10 +108,8 @@ function strip<Type extends SPOJO>(toStrip: Type, reference: Type): Type {
}
// it is a child object, we strip it further
const stripped: SPOJO = {};
const childReference: SPOJO = (reference[key] as Array<SPOJO>)[0];
stripped[key] = strip(value as SPOJO, childReference);
(res as SPOJO)[key] = stripped;
const childReference: SPOJO = reference[key] as SPOJO;
(res as SPOJO)[key] = strip(value as SPOJO, childReference);
}
return res;
}
@@ -82,7 +118,7 @@ export function stripPrivateFields(config: Config): Config {
return strip(config, requiredConfig);
}
export const serverConfig: Writable<Config> = writable(mergeConfig(defaultConfig, configData));
export const serverConfig: Writable<Config> = writable(merge<Config>(defaultConfig, configData));
export const clientConfig: Readable<Config> = derived(serverConfig, ($config) =>
stripPrivateFields($config)
@@ -94,7 +130,7 @@ export function watchDymamicConfig() {
const reloadConfig = async () => {
try {
const dynamicConfig = yml.load(await readFile(__filepath, 'utf8'));
serverConfig.set(mergeConfig(defaultConfig, dynamicConfig));
serverConfig.set(merge<Config>(defaultConfig, dynamicConfig));
} catch (err) {
console.error('could not read or parse config: ' + err);
}