Skip to content

Commit

Permalink
chore: config file to storage
Browse files Browse the repository at this point in the history
  • Loading branch information
hywax committed Dec 31, 2023
1 parent 2865104 commit cc092bd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
8 changes: 8 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ export default defineNuxtConfig({
colorMode: {
classSuffix: '',
},
nitro: {
storage: {
data: {
driver: 'fs',
base: './data'
}
}
}
})
2 changes: 1 addition & 1 deletion server/plugins/1.config-loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default defineNitroPlugin(async (nitroApp) => {
const localConfig = getLocalConfig()
const localConfig = await getLocalConfig()

if (!localConfig) {
console.error('Config not loaded!')
Expand Down
11 changes: 7 additions & 4 deletions server/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from 'node:fs'
import crypto from 'node:crypto'
import yaml from 'yaml'
import type { BaseService, Config } from '~/types'
Expand All @@ -12,13 +11,17 @@ function determineServiceId(items: draftService[]): BaseService[] {
}))
}

export function getLocalConfig(): Config | null {
export async function getLocalConfig(): Promise<Config | null> {
const storage = useStorage('data')
const file = 'config.yml'

try {
if (!fs.existsSync('assets/config.yaml')) {
if (!await storage.hasItem(file)) {
return null
}

const config = yaml.parse(fs.readFileSync('assets/config.yaml', 'utf8')) || {}
const raw = await storage.getItem<string>(file)
const config = yaml.parse(raw || '') || {}
const services: Config['services'] = []

if (Array.isArray(config.services)) {
Expand Down

0 comments on commit cc092bd

Please sign in to comment.