Adds a more realistic config for development.

This commit is contained in:
2023-08-15 10:02:30 +02:00
parent a439faa3fe
commit e4d1b83108
9 changed files with 70 additions and 42 deletions

View File

@@ -3,7 +3,6 @@
export let brand: BrandConfig = {};
export let color = 'var(--text)';
console.log(brand);
function masked() {
return brand.logo != undefined && brand.asmask == true;

View File

@@ -7,6 +7,8 @@
export let data: any;
const component = getServiceComponent(service.type || '');
console.log(service);
</script>
<div class="card">

View File

@@ -17,6 +17,8 @@ export interface ServiceConfig extends SectionConfig {
url: string;
target?: string;
type?: string;
tag: string;
keywords: string;
[x: string]: unknown;
}

View File

@@ -1,5 +1,11 @@
import { describe, expect, it } from 'vitest';
import { defaultConfig, stripPrivateFields, serverConfig, clientConfig, merge } from './config';
import {
defaultConfig,
stripPrivateFields,
serverConfig,
clientConfig,
mergeConfig
} from './config';
import type { Config } from '$lib/config';
describe('Config', () => {
@@ -9,7 +15,7 @@ describe('Config', () => {
});
it('should be able to merge with POJO', () => {
const merged: Config = merge<Config>(defaultConfig, {
const merged: Config = mergeConfig(defaultConfig, {
subtitle: "Homer's favorite neighboor",
colors: {
light: { notInDefault: 'foo' }
@@ -20,7 +26,8 @@ describe('Config', () => {
items: [
{
title: 'anoy homer',
customKey: 'all the time'
customKey: 'all the time',
keywords: 'a b cc'
}
]
}
@@ -39,6 +46,7 @@ describe('Config', () => {
expect(merged.services[0].items).toHaveLength(1);
expect(merged.services[0].items[0].title).toEqual('anoy homer');
expect(merged.services[0].items[0].customKey).toEqual('all the time');
expect(merged.services[0].items[0].keywords).toEqual('a b cc');
});
it('should be able to strip custom keys', () => {

View File

@@ -17,7 +17,9 @@ const requiredService: Required<ServiceConfig> = {
asmask: false,
url: '',
target: '',
type: ''
type: '',
tag: '',
keywords: [],
};
const requiredServiceGroup: Required<ServiceGroupConfig> = {
@@ -55,7 +57,7 @@ export const requiredConfig: Required<Config> = {
type SPOJO = Record<string, unknown>;
export function merge<Type extends SPOJO>(a: Type, b: SPOJO): Type {
function merge<Type extends SPOJO>(a: Type, b: SPOJO): Type {
const res: Type = { ...a };
for (const [key, value] of Object.entries(b)) {
if (
@@ -80,6 +82,11 @@ export function merge<Type extends SPOJO>(a: Type, b: SPOJO): Type {
return res;
}
export function mergeConfig(a: Config, b: SPOJO): Config {
return merge<Config>(a,b);
}
const defaultLightConfig: ColorConfig = {};
defaultLightConfig['highlight-primary'] = '#3367d6';
defaultLightConfig['highlight-secondary'] = '#4285f4';
@@ -118,11 +125,16 @@ export const defaultConfig: Config = {
dark: defaultDarkConfig
},
links: [],
services: []
};
function strip<Type extends SPOJO>(toStrip: Type, reference: Type): Type {
const res: Type = { ...toStrip };
if ( reference == undefined ) {
return res;
}
const referenceNames = Object.entries(reference).map(([key, value]) => key);
for (const [key, value] of Object.entries(res)) {
@@ -145,6 +157,7 @@ function strip<Type extends SPOJO>(toStrip: Type, reference: Type): Type {
continue;
}
// it is a child object, we strip it further
const childReference: SPOJO = reference[key] as SPOJO;
(res as SPOJO)[key] = strip(value as SPOJO, childReference);
@@ -171,7 +184,7 @@ export function clientConfig(): Config {
return _clientConfig;
}
let _serverConfig = merge<Config>(defaultConfig, configData);
let _serverConfig = mergeConfig(defaultConfig, configData);
let _clientConfig = stripPrivateFields(_serverConfig);
@@ -181,7 +194,7 @@ export function watchDymamicConfig() {
const reloadConfig = async () => {
try {
const dynamicConfig = yml.load(await readFile(__filepath, 'utf8'));
_serverConfig = merge<Config>(defaultConfig, dynamicConfig);
_serverConfig = mergeConfig(defaultConfig, dynamicConfig);
_clientConfig = stripPrivateFields(_serverConfig);
} catch (err) {
console.error('could not read or parse config: ' + err);

View File

@@ -1,5 +1,5 @@
<script lang="ts">
export const data = undefined;
export let data;
</script>
<p>pihole status</p>
pihole status

View File

@@ -1,5 +1,5 @@
<script lang="ts">
export const data = undefined;
export let data;
</script>
<p>Prowlarr content</p>
Prowlarr content