get rid of log
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
[test]
|
||||
preload = "./test/init.ts"
|
||||
@@ -9,7 +9,6 @@
|
||||
},
|
||||
|
||||
"dependencies": {
|
||||
"log": "git+https://git.pband.ch/typescript/log.git",
|
||||
"yup": "^1.7.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -20,7 +19,6 @@
|
||||
"eslint": "^9.35.0",
|
||||
"globals": "^16.4.0",
|
||||
"jiti": "^2.5.1",
|
||||
"logger-console": "git+https://git.pband.ch/typescript/logger-console.git",
|
||||
"prettier": "^3.6.2",
|
||||
"tmp": "^0.2.5",
|
||||
"typescript": "^5.9.2",
|
||||
|
||||
+7
-9
@@ -1,20 +1,18 @@
|
||||
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) {
|
||||
log.debug(`Read ${key} from a file`)
|
||||
log.trace('Path :', path)
|
||||
console.debug(`Read ${key} from a file`)
|
||||
console.trace('Path :', path)
|
||||
return from_file(path)
|
||||
} else {
|
||||
log.debug(`Read ${key} from env`)
|
||||
console.debug(`Read ${key} from env`)
|
||||
return {ok: true, data: process.env[key]}
|
||||
}
|
||||
}
|
||||
@@ -27,15 +25,15 @@ export async function from_file(path: string): Promise<Ok<string>> {
|
||||
return content
|
||||
}
|
||||
export async function get_file_content(path: string): Promise<Ok<string>> {
|
||||
log.debug('Read file content')
|
||||
log.trace('Path :', path)
|
||||
console.debug('Read file content')
|
||||
console.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)
|
||||
console.warn('Failed to read file', path)
|
||||
console.debug('Error :', e)
|
||||
return {ok: false}
|
||||
}
|
||||
}
|
||||
|
||||
+8
-11
@@ -1,15 +1,12 @@
|
||||
import * as yup from 'yup'
|
||||
import logger from 'log'
|
||||
|
||||
import {read_env} from './env'
|
||||
import type {Ok} from './helpers'
|
||||
|
||||
const log = logger('config:parsing')
|
||||
|
||||
export async function parse<S extends yup.Maybe<yup.AnyObject>>(schema: yup.ObjectSchema<S>): Promise<Ok<S>> {
|
||||
log.info('Parse from env')
|
||||
console.log('Parse from env')
|
||||
|
||||
log.trace('Start parsing')
|
||||
console.trace('Start parsing')
|
||||
const config = await object(schema)
|
||||
if (!config.ok) return config
|
||||
|
||||
@@ -30,7 +27,7 @@ export async function object<S extends yup.Maybe<yup.AnyObject>>(
|
||||
schema: yup.ObjectSchema<S>,
|
||||
base_name: string = ''
|
||||
): Promise<Ok<S>> {
|
||||
log.debug('Object')
|
||||
console.debug('Object')
|
||||
if (base_name.length !== 0) base_name = base_name + '_'
|
||||
|
||||
// @ts-expect-error Ugly hack with type S
|
||||
@@ -41,7 +38,7 @@ export async function object<S extends yup.Maybe<yup.AnyObject>>(
|
||||
const sub_scheme = schema.fields[key] as yup.AnySchema
|
||||
|
||||
const type = sub_scheme.describe().type
|
||||
log.debug(`Parse ${sub_key} with type ${type}`)
|
||||
console.debug(`Parse ${sub_key} with type ${type}`)
|
||||
|
||||
// If it is an object, do not try to read from env
|
||||
let value: Ok<S>
|
||||
@@ -59,7 +56,7 @@ export async function object<S extends yup.Maybe<yup.AnyObject>>(
|
||||
if (def !== undefined) value = {ok: true, data: def}
|
||||
else if (sub_scheme.spec.optional) continue
|
||||
else {
|
||||
log.warn('Missing value for', key)
|
||||
console.warn('Missing value for', key)
|
||||
return {}
|
||||
}
|
||||
} else {
|
||||
@@ -79,11 +76,11 @@ export async function object<S extends yup.Maybe<yup.AnyObject>>(
|
||||
}
|
||||
|
||||
export async function generic<S>(value: string | string[], scheme: yup.AnySchema<S>): Promise<Ok<S>> {
|
||||
log.debug('Generic', scheme.describe().type)
|
||||
console.debug('Generic', scheme.describe().type)
|
||||
|
||||
if (!(await scheme.isValid(value))) {
|
||||
log.warn('Invalid value')
|
||||
log.debug('Value:', value)
|
||||
console.warn('Invalid value')
|
||||
console.debug('Value:', value)
|
||||
return {}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@ describe('File IO', () => {
|
||||
const res = await env.from_file(file.name)
|
||||
expect(res).toEqual({ok: true, data: 'coucou'})
|
||||
})
|
||||
test.todo('Fails safe if no access to read')
|
||||
test.todo('Fails safe if no access to read', () => {})
|
||||
})
|
||||
describe('Folder', () => {
|
||||
let folder: DirResult
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import {Console} from 'logger-console'
|
||||
import {Level, writers} from 'log'
|
||||
|
||||
const logger = new Console({minLevel: Level.DEBUG, with_color: true})
|
||||
writers.set('console', logger)
|
||||
Reference in New Issue
Block a user