import { includesCaseInsensitive } from "./utility" ; import { existsSync , readJsonSync , outputJsonSync } from "fs-extra" ; import { get , set , map , pipe , filter , flatMap , uniq , toUpper } from "lodash/fp" ; import { parse , resolve } from "path" ; export function getDefaultsFromSchema ( schema ) { return Object . keys ( schema ) . reduce ( ( acc , value ) => { return { ... acc , [ value ] : schema [ value ] . default } ; } , { } ) ; } interface IUser { user : string ; password ? : string ; askForPassword ? : boolean ; walletConnectString ? : string ; default ? : boolean ; disabled ? : boolean ; schemas ? : string [ ] ; } export interface IConnectionConfig extends IUser { env : string ; connectString : string ; } import { properties as dbConfSchema } from "../schemas/dbconfig-schema.json" ; export class DBConfig { defaults : Object ; fileBase : string ; object : Object ; constructor ( fileBase ) { this . defaults = getDefaultsFromSchema ( dbConfSchema ) ; this . fileBase = fileBase || "./dbconfig.json" ; this . object = null ; this . load ( ) ; } createFile ( ) { return outputJsonSync ( this . fileBase , this . defaults ) ; } load ( ) { try { this . object = readJsonSync ( this . fileBase ) ; } catch ( e ) { console . log ( `Cannot find ${ this . fileBase } file...` ) ; this . object = this . defaults ; } } get ( field ) { return get ( field ) ( this . object ) ; } set ( field , value ) { this . object = set ( field ) ( value ) ( this . object ) ; return outputJsonSync ( this . fileBase , this . object ) ; } _getAllUsersByEnv = ( env ) => ( data ) : IUser [ ] => { return pipe ( get ( env ) , get ( "users" ) , filter ( ( v ) => ! v . disabled ) ) ( data ) ; } ; param { string } env returns { string [ ] } getSchemas = ( env = "DEV" ) => { return pipe ( this . _getAllUsersByEnv ( env ) , flatMap ( ( v ) => ( v . schemas ? v . schemas : [ v . user ] ) ) , map ( toUpper ) , uniq ) ( this . object ) ; } ; param { string } env param { string } user returns { IConnectionConfig } getConfiguration = ( env , user ? ) : IConnectionConfig => { if ( ! env ) { throw Error ( `No env parameter.` ) ; } if ( ! this . object [ env ] ) { throw Error ( `Cannot find ${ env } environment in dbconfig.json.` ) ; } const head = { env , connectString : this . object [ env ] . connectString } ; let byEnv = this . _getAllUsersByEnv ( env ) ( this . object ) ; if ( ! byEnv ) { throw Error ( `dbconfig.json: Invalid structure. Cannot find "${ env }" env.` ) ; } if ( byEnv . length === 0 ) { throw Error ( `dbconfig.json: No user for "${ env }" env.` ) ; } if ( byEnv . length === 1 ) { return { ... head , ... byEnv [ 0 ] } ; } let byUser = user ? filter ( ( v ) => v . user . toUpperCase ( ) === user . toUpperCase ( ) || ( v . schemas && includesCaseInsensitive ( v . schemas , user ) ) ) ( byEnv ) : byEnv ; if ( byUser . length === 1 ) { return { ... head , ... byUser [ 0 ] } ; } let byDefault = filter ( { default : true } as IUser ) ( byEnv ) ; if ( byDefault . length === 1 ) { return { ... head , ... byDefault [ 0 ] } ; } else { throw Error ( `dbconfig.json: No match for user "${ user }" in "${ env }". Add/Enable missing user or set default user for env ("default": true).` ) ; } } ; } export const dbConfig = new DBConfig ( process . env . ORADEW_DB_CONFIG_PATH ) ; /**
 * Workspace configuration
 *
 * * Base config applies to all environemnts.
 * But can be exteneded with env specific configs
 *
 * Config file extending sequence: defaults <== (BASE) <== (DEV, TEST, UAT, ...)
 *
 * (base): ./oradewrc.json
 *
 * Env specific configs:
 * DEV: ./oradewrc.dev.json (optional)
 * TEST: ./oradewrc.test.json (optional)
 * UAT: ./oradewrc.uat.json (optional)
 * ...
 * customEnv: ./oradewrc.customEnv.json (optional)
 */ import { properties as wsConfSchema } from "../schemas/oradewrc-schema.json" ; export class WorkspaceConfig { defaults : object ; filePathBase : string ; object : object ; constructor ( fileBase ? ) { this . defaults = getDefaultsFromSchema ( wsConfSchema ) ; this . filePathBase = fileBase || "./oradewrc.json" ; this . object = { } ; this . object [ "BASE" ] = null ; this . getConfigObject ( "BASE" ) ; } getFileEnv ( env = "BASE" ) { if ( env === "BASE" ) { return this . filePathBase ; } let parsed = parse ( this . filePathBase ) ; return resolve ( parsed . dir , `${ parsed . name }.${ env }${ parsed . ext }` ) ; } readFile ( env ) { let filename = this . getFileEnv ( env ) ; let res = existsSync ( filename ) ? readJsonSync ( filename , { encoding : "utf8" } ) : { } ; return res ; } getConfigObject ( env ) { if ( ! this . object [ "BASE" ] ) { const objectBase = this . readFile ( "BASE" ) ; this . object [ "BASE" ] = { ... this . defaults , ... objectBase } ; } if ( ! this . object [ env ] ) { const object = this . readFile ( env ) ; this . object [ env ] = { ... this . object [ "BASE" ] , ... object } ; } } get ( param ) { let field , env ; if ( typeof param === "object" ) { ( { field , env } = param ) ; } else { field = param ; env = "BASE" ; } this . getConfigObject ( env ) ; return field ? this . object [ env ] [ field ] : this . object [ env ] ; } set ( field , value , env = "BASE" ) { if ( this . object [ env ] ) { this . object [ env ] [ field ] = value ; } if ( env === "BASE" ) { let _base = this . object [ "BASE" ] ; this . object = { } ; this . object [ "BASE" ] = _base ; } const filename = this . getFileEnv ( env ) ; const object = this . readFile ( env ) ; return outputJsonSync ( filename , { ... object , ... { [ field ] : value } , } ) ; } } export const workspaceConfig = new WorkspaceConfig ( process . env . ORADEW_WS_CONFIG_PATH ) ;