init repo

This commit is contained in:
2025-09-11 13:41:58 +02:00
commit b4bb3e3eed
13 changed files with 178 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
import logger from 'log'
import fs from 'node:fs/promises'
import type {Ok} from './helpers'
const log = logger('config:env')
const FILE_EXT = "__FILE"
// Read from env or file
export async function read_env(key: string): Promise<Ok<string | undefined>> {
const path = process.env[key + FILE_EXT]
if (path !== undefined) return from_file(path)
else return {ok: true, data: process.env[key]}
}
async function from_file(path: string): Promise<Ok<string>> {
log.debug("Read a key from a file")
log.trace("Path :", path)
let content = await get_file_content(path)
if (!content.ok) return content
content.data = content.data.trim()
return content
}
async function get_file_content(path: string) : Promise<Ok<string>> {
log.debug('Read file content')
log.trace('Path :', path)
try {
const data = await fs.readFile(path, {encoding: 'utf-8'})
return {ok: true, data}
} catch (e) {
log.warn('Failed to read file', path)
log.debug('Error :', e)
return {ok: false}
}
}
+1
View File
@@ -0,0 +1 @@
export type Ok<T> = {ok: true, data: T} | {ok: false}
+17
View File
@@ -0,0 +1,17 @@
import logger from 'log'
import {Ok} from './helpers'
// TODO : re-export types used to describe schema
const log = logger('config')
export async function parse<S>(schema: S): Promise<Ok<unknown>> {
log.info("Parse configuration from env")
// TODO : Read config from env
// TODO : maybe double check config
return {ok: false}
}