Adds ColorConfig to Config
This commit is contained in:
@@ -4,6 +4,11 @@ logo: 'icon-any.svg'
|
|||||||
icon: 'fa fa-cloud'
|
icon: 'fa fa-cloud'
|
||||||
asmask: false
|
asmask: false
|
||||||
|
|
||||||
|
colors:
|
||||||
|
dark:
|
||||||
|
test: red
|
||||||
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
- title: '/Cloud'
|
- title: '/Cloud'
|
||||||
subtitle: 'Private Cloud Utilities'
|
subtitle: 'Private Cloud Utilities'
|
||||||
|
|||||||
@@ -23,8 +23,17 @@ export interface ServiceGroupConfig extends SectionConfig {
|
|||||||
[x: string]: unknown;
|
[x: string]: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ColorConfig {
|
||||||
|
test: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface Config extends SectionConfig {
|
export interface Config extends SectionConfig {
|
||||||
services: Array<ServiceGroupConfig>;
|
services: Array<ServiceGroupConfig>;
|
||||||
|
|
||||||
|
colors: {
|
||||||
|
light: ColorConfig;
|
||||||
|
dark: ColorConfig;
|
||||||
|
};
|
||||||
|
|
||||||
[x: string]: unknown;
|
[x: string]: unknown;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,6 @@
|
|||||||
import { describe, expect, it } from 'vitest';
|
import { describe, expect, it } from 'vitest';
|
||||||
import {
|
import { defaultConfig, stripPrivateFields, serverConfig, clientConfig, merge } from './config';
|
||||||
defaultConfig,
|
import type { Config } from '$lib/config';
|
||||||
mergeConfig,
|
|
||||||
type Config,
|
|
||||||
stripPrivateFields,
|
|
||||||
serverConfig,
|
|
||||||
clientConfig
|
|
||||||
} from './config';
|
|
||||||
|
|
||||||
describe('Config', () => {
|
describe('Config', () => {
|
||||||
it('should be export a build time server and client config store', () => {
|
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', () => {
|
it('should be able to merge with POJO', () => {
|
||||||
const merged = mergeConfig(defaultConfig, {
|
const merged: Config = merge<Config>(defaultConfig, {
|
||||||
subtitle: "Homer's favorite neighboor",
|
subtitle: "Homer's favorite neighboor",
|
||||||
|
colors: {
|
||||||
|
light: {
|
||||||
|
test: 'red'
|
||||||
|
}
|
||||||
|
},
|
||||||
services: [
|
services: [
|
||||||
{
|
{
|
||||||
title: 'favorite occupations',
|
title: 'favorite occupations',
|
||||||
@@ -28,10 +27,12 @@ describe('Config', () => {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
});
|
}) as Config;
|
||||||
|
|
||||||
expect(merged.title).toEqual('Flanders');
|
expect(merged.title).toEqual('Flanders');
|
||||||
expect(merged.services).toHaveLength(1);
|
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].title).toEqual('favorite occupations');
|
||||||
expect(merged.services[0].items).toHaveLength(1);
|
expect(merged.services[0].items).toHaveLength(1);
|
||||||
expect(merged.services[0].items[0].title).toEqual('anoy homer');
|
expect(merged.services[0].items[0].title).toEqual('anoy homer');
|
||||||
@@ -42,6 +43,10 @@ describe('Config', () => {
|
|||||||
const custom: Config = {
|
const custom: Config = {
|
||||||
title: 'custom',
|
title: 'custom',
|
||||||
secret: { secret: 'some secret' },
|
secret: { secret: 'some secret' },
|
||||||
|
colors: {
|
||||||
|
light: {},
|
||||||
|
dark: {}
|
||||||
|
},
|
||||||
services: [
|
services: [
|
||||||
{
|
{
|
||||||
title: 'top services',
|
title: 'top services',
|
||||||
|
|||||||
@@ -30,21 +30,59 @@ export const requiredConfig: Required<Config> = {
|
|||||||
logo: '',
|
logo: '',
|
||||||
icon: '',
|
icon: '',
|
||||||
asmask: false,
|
asmask: false,
|
||||||
|
colors: {
|
||||||
|
light: {
|
||||||
|
test: ''
|
||||||
|
},
|
||||||
|
dark: {
|
||||||
|
test: ''
|
||||||
|
}
|
||||||
|
},
|
||||||
services: [requiredServiceGroup]
|
services: [requiredServiceGroup]
|
||||||
};
|
};
|
||||||
|
|
||||||
export function mergeConfig(a: Config, b: any): Config {
|
type SPOJO = Record<string, unknown>;
|
||||||
return { ...a, ...b };
|
|
||||||
|
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 = {
|
export const defaultConfig: Config = {
|
||||||
title: 'Flanders',
|
title: 'Flanders',
|
||||||
|
colors: {
|
||||||
|
light: {
|
||||||
|
test: ''
|
||||||
|
},
|
||||||
|
dark: {
|
||||||
|
test: ''
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
services: []
|
services: []
|
||||||
};
|
};
|
||||||
|
|
||||||
type SPOJO = Record<string, unknown>;
|
|
||||||
|
|
||||||
function strip<Type extends SPOJO>(toStrip: Type, reference: Type): Type {
|
function strip<Type extends SPOJO>(toStrip: Type, reference: Type): Type {
|
||||||
const res: Type = { ...toStrip };
|
const res: Type = { ...toStrip };
|
||||||
const referenceNames = Object.entries(reference).map(([key, value]) => key);
|
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
|
// it is a child object, we strip it further
|
||||||
const stripped: SPOJO = {};
|
const childReference: SPOJO = reference[key] as SPOJO;
|
||||||
const childReference: SPOJO = (reference[key] as Array<SPOJO>)[0];
|
(res as SPOJO)[key] = strip(value as SPOJO, childReference);
|
||||||
stripped[key] = strip(value as SPOJO, childReference);
|
|
||||||
(res as SPOJO)[key] = stripped;
|
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -82,7 +118,7 @@ export function stripPrivateFields(config: Config): Config {
|
|||||||
return strip(config, requiredConfig);
|
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) =>
|
export const clientConfig: Readable<Config> = derived(serverConfig, ($config) =>
|
||||||
stripPrivateFields($config)
|
stripPrivateFields($config)
|
||||||
@@ -94,7 +130,7 @@ export function watchDymamicConfig() {
|
|||||||
const reloadConfig = async () => {
|
const reloadConfig = async () => {
|
||||||
try {
|
try {
|
||||||
const dynamicConfig = yml.load(await readFile(__filepath, 'utf8'));
|
const dynamicConfig = yml.load(await readFile(__filepath, 'utf8'));
|
||||||
serverConfig.set(mergeConfig(defaultConfig, dynamicConfig));
|
serverConfig.set(merge<Config>(defaultConfig, dynamicConfig));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('could not read or parse config: ' + err);
|
console.error('could not read or parse config: ' + err);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user