@@ -12,7 +12,7 @@ import { z } from "zod";
1212import env from 'env-var' ;
1313import { existsSync } from 'fs' ;
1414import fs from 'fs/promises' ;
15- import { fromFailablePromise } from "../../utils/TaskEither" ;
15+ import { fromFailablePromise , liftZodParseResult } from "../../utils/TaskEither" ;
1616
1717export const configurationSchema = z . object ( {
1818 pluginDir : z . string ( ) . optional ( ) ,
@@ -25,37 +25,37 @@ export const configurationSchema = z.object({
2525
2626export type Configuration = z . infer < typeof configurationSchema > ;
2727
28+ export const _readConfiguration =
29+ < T > ( parseConfiguration : ( s : string ) => z . SafeParseReturnType < T , T > ) =>
30+ ( cfgPath ?: string ) :
31+ TaskEither < string , T > =>
32+ pipe (
33+ TE . Do ,
34+ TE . bind ( 'finalCfgPath' ,
35+ ( _ ) => TE . fromIOEither ( ( ) => {
36+ const finalCfgPath =
37+ path . resolve ( cfgPath ?? env . get ( 'MINAUTH_CONFIG' )
38+ . default ( "config.yaml" )
39+ . asString ( ) )
40+
41+ console . log ( `reading configuration from ${ finalCfgPath } ` ) ;
42+
43+ return existsSync ( finalCfgPath ) ?
44+ E . right ( finalCfgPath ) :
45+ E . left ( 'configuration file does not exists' ) ;
46+ } )
47+ ) ,
48+ TE . bind ( 'cfgFileContent' , ( { finalCfgPath } ) =>
49+ fromFailablePromise < string > ( ( ) => fs . readFile ( finalCfgPath , 'utf-8' ) )
50+ ) ,
51+ TE . chain (
52+ ( { cfgFileContent } ) =>
53+ liftZodParseResult ( parseConfiguration ( cfgFileContent ) )
54+ )
55+ ) ;
56+
2857export const readConfiguration =
29- ( cfgPath ?: string ) :
30- TaskEither < string , Configuration > =>
31- pipe (
32- TE . Do ,
33- TE . bind ( 'finalCfgPath' ,
34- ( _ ) => TE . fromIOEither ( ( ) => {
35- const finalCfgPath =
36- path . resolve ( cfgPath ?? env . get ( 'MINAUTH_CONFIG' )
37- . default ( "config.yaml" )
38- . asString ( ) )
39-
40- console . log ( `reading configuration from ${ finalCfgPath } ` ) ;
41-
42- return existsSync ( finalCfgPath ) ?
43- E . right ( finalCfgPath ) :
44- E . left ( 'configuration file does not exists' ) ;
45- } )
46- ) ,
47- TE . bind ( 'cfgFileContent' , ( { finalCfgPath } ) =>
48- fromFailablePromise < string > ( ( ) => fs . readFile ( finalCfgPath , 'utf-8' ) )
49- ) ,
50- TE . chain (
51- ( { cfgFileContent } ) => {
52- const parseResult = configurationSchema . safeParse ( cfgFileContent ) ;
53- const ret = parseResult . success ?
54- E . right ( parseResult . data as Configuration ) :
55- E . left ( `error while parsing configuration: ${ parseResult . error } ` ) ;
56- return TE . fromEither ( ret ) ;
57- } )
58- )
58+ _readConfiguration ( configurationSchema . safeParse )
5959
6060//
6161
@@ -73,7 +73,7 @@ const importPluginModule =
7373
7474const validatePluginCfg =
7575 ( cfg : any , factory : UntypedPluginFactory ) : TaskEither < string , any > =>
76- fromFailablePromise ( ( ) => factory . configurationSchema . parseAsync ( cfg ) ) ;
76+ liftZodParseResult ( factory . configurationSchema . safeParse ( cfg ) ) ;
7777
7878const initializePlugin =
7979 ( pluginModulePath : string , pluginCfg : any ) : TaskEither < string , UntypedPlugin > =>
@@ -87,7 +87,7 @@ const initializePlugin =
8787 validatePluginCfg ( pluginCfg , pluginFactory ) ) ,
8888 TE . chain ( ( { pluginFactory, typedPluginCfg } ) =>
8989 pluginFactory . initialize ( typedPluginCfg ) )
90- )
90+ ) ;
9191
9292export const initializePlugins =
9393 ( cfg : Configuration ) : TaskEither < string , Record < string , UntypedPlugin > > => {
@@ -101,16 +101,18 @@ export const initializePlugins =
101101 return optionalPath === undefined ?
102102 path . join ( dir , name ) :
103103 path . resolve ( optionalPath )
104- }
104+ } ;
105105
106106 const resolveModulePathAndInitializePlugin =
107107 ( pluginName : string , pluginCfg : {
108108 path ?: string | undefined ;
109109 config ?: any ;
110110 } ) : TaskEither < string , UntypedPlugin > =>
111- TE . chain
112- ( ( modulePath : string ) => initializePlugin ( modulePath , pluginCfg ?? { } ) )
113- ( TE . fromIO ( resolvePluginModulePath ( pluginName , pluginCfg . path ) ) )
111+ pipe (
112+ TE . fromIO ( resolvePluginModulePath ( pluginName , pluginCfg . path ) ) ,
113+ TE . chain
114+ ( ( modulePath : string ) => initializePlugin ( modulePath , pluginCfg . config ?? { } ) )
115+ ) ;
114116
115117 const Applicative =
116118 TE . getApplicativeTaskValidation ( T . ApplyPar ,
0 commit comments