Skip to content

Commit

Permalink
feat(config): ungrouped services
Browse files Browse the repository at this point in the history
  • Loading branch information
hywax committed Dec 28, 2023
1 parent 4d38c00 commit 4b2bab0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions components/Group.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="py-10">
<h2 class="text-2xl font-light py-2 px-4">
<h2 v-if="title" class="text-2xl font-light py-2 px-4">
{{ title }}
</h2>
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-1 lg:gap-2 lg:gap-y-4">
Expand All @@ -15,7 +15,7 @@
import type { BaseService } from '~/types'
export interface Props {
title: string
title?: string
items: BaseService[]
}
Expand Down
31 changes: 23 additions & 8 deletions server/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,38 @@ import crypto from 'node:crypto'
import yaml from 'yaml'
import type { BaseService, Config } from '~/types'

type draftService = Omit<BaseService, 'id'>

function determineServiceId(items: draftService[]): BaseService[] {
return items.map((item) => ({
id: crypto.randomUUID(),
...item
}))
}

export function getLocalConfig(): Config | null {
try {
if (!fs.existsSync('assets/config.yaml')) {
return null
}

const config = yaml.parse(fs.readFileSync('assets/config.yaml', 'utf8')) || {}
const services = Object
.entries<Omit<BaseService, 'id'>[]>((config.services || []))
.reduce<Config['services']>((acc, [title, items]) => {
acc.push({
const services: Config['services'] = []

if (Array.isArray(config.services)) {
services.push({
items: determineServiceId(config.services)
})
} else {
const entries = Object.entries<draftService[]>(config.services || [])

for (const [title, items] of entries) {
services.push({
title,
items: items.map((item) => ({ ...item, id: crypto.randomUUID() })),
items: determineServiceId(items)
})

return acc
}, [])
}
}

return {
...config,
Expand Down
2 changes: 1 addition & 1 deletion types/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BaseService } from '~/types/services'

export interface ServicesGroup {
title: string
title?: string
items: BaseService[]
}

Expand Down

0 comments on commit 4b2bab0

Please sign in to comment.