import { ScriptServer , JavaServer , RconConnection } from '@scriptserver/core' ; import defaultsDeep from 'lodash.defaultsdeep' ; import { join } from 'path' ; import fs from 'fs' ; export interface JsonConfig { initialized : boolean ; path : string ; } declare module '@scriptserver/core/dist/Config' { interface Config { json : JsonConfig ; } } declare module '@scriptserver/core/dist/JavaServer' { interface JavaServer { json : { get : < T = any > ( path : string , key ? : string ) => Promise < T > ; set : < T = any > ( path : string , key : string , value : T ) => Promise < T > ; } ; } } declare module '@scriptserver/core/dist/RconConnection' { interface RconConnection { json : { get : < T = any > ( path : string , key ? : string ) => Promise < T > ; set : < T = any > ( path : string , key : string , value : T ) => Promise < T > ; } ; } } declare module '@scriptserver/core/dist/ScriptServer' { interface ScriptServer { json : { get : < T = any > ( path : string , key ? : string ) => Promise < T > ; set : < T = any > ( path : string , key : string , value : T ) => Promise < T > ; } ; } } export const DEFAULT_JSON_CONFIG = { initialized : false , path : join ( process . cwd ( ) , './json' ) , } ; export function useJson ( server ) { if ( server . config ?. json ?. initialized ) { return ; } defaultsDeep ( server . config , { json : DEFAULT_JSON_CONFIG } ) ; server . config . json . initialized = true ; if ( ! fs . existsSync ( server . config . json . path ) ) { fs . mkdirSync ( server . config . json . path ) ; } server . json = { get : async < T = any > ( path , key ? ) : Promise < T > => { const filePath = join ( server . config . json . path , path . toLowerCase ( ) + '.json' ) ; return readJson ( filePath ) . then ( data => ( key ? data [ key ] : data ) ) ; } , set : async < T = any > ( path , key , value ) : Promise < T > => { const filePath = join ( server . config . json . path , path . toLowerCase ( ) + '.json' ) ; return readJson ( filePath ) . then ( data => { data [ key ] = value ; return writeJson ( filePath , data ) ; } ) ; } , } ; if ( server instanceof ScriptServer ) { server . javaServer . json = server . json ; server . rconConnection . json = server . json ; } } function readJson < T = any > ( path ) { return new Promise ( resolve => { fs . readFile ( path , 'utf8' , ( err , data ) => { if ( err ) resolve ( { } as any ) ; else resolve ( JSON . parse ( data ) ) ; } ) ; } ) ; } function writeJson < T = any > ( path , data ) { return new Promise ( ( resolve , reject ) => { fs . writeFile ( path , JSON . stringify ( data , null , 4 ) , err => { if ( err ) reject ( err ) ; else resolve ( data ) ; } ) ; } ) ; }