import "missing-native-js-functions" ; import { ConfigValue , ConfigEntity , DefaultConfigOptions } from "../entities/Config" ; import path from "path" ; import fs from "fs" ; var config ; var pairs ; export const Config = { init : async function init ( ) { if ( config ) return config ; pairs = await ConfigEntity . find ( ) ; config = pairsToConfig ( pairs ) ; config = ( config || { } ) . merge ( DefaultConfigOptions ) ; return this . set ( config ) ; } , get : function get ( ) { return config ; } , set : function set ( val ) { if ( ! config || ! val ) return ; config = val . merge ( config ) ; return applyConfig ( config ) ; } , } ; function applyConfig ( val ) { async function apply ( obj , key = "" ) { if ( typeof obj === "object" && obj !== null ) return Promise . all ( Object . keys ( obj ) . map ( ( k ) => apply ( obj [ k ] , key ? `${ key }_${ k }` : k ) ) ) ; let pair = pairs . find ( ( x ) => x . key === key ) ; if ( ! pair ) pair = new ConfigEntity ( ) ; pair . key = key ; pair . value = obj ; return pair . save ( ) ; } return apply ( val ) ; } function pairsToConfig ( pairs ) { var value = { } ; pairs . forEach ( ( p ) => { const keys = p . key . split ( "_" ) ; let obj = value ; let prev = "" ; let prevObj = obj ; let i = 0 ; for ( const key of keys ) { if ( ! isNaN ( Number ( key ) ) && ! prevObj [ prev ] ?. length ) prevObj [ prev ] = obj = [ ] ; if ( i ++ === keys . length - 1 ) obj [ key ] = p . value ; else if ( ! obj [ key ] ) obj [ key ] = { } ; prev = key ; prevObj = obj ; obj = obj [ key ] ; } } ) ; return value as ConfigValue ; }