|
| 1 | +import * as path from 'path'; |
| 2 | +import { IMinAuthPlugin, IMinAuthPluginFactory } from './pluginType'; |
| 3 | +import * as R from 'fp-ts/Record'; |
| 4 | +import { pipe } from 'fp-ts/function'; |
| 5 | +import { TaskEither } from 'fp-ts/TaskEither'; |
| 6 | +import * as T from 'fp-ts/Task'; |
| 7 | +import * as TE from 'fp-ts/TaskEither'; |
| 8 | +import * as E from 'fp-ts/Either'; |
| 9 | +import * as Str from 'fp-ts/string'; |
| 10 | +import * as S from 'fp-ts/Semigroup'; |
| 11 | +import { z } from 'zod'; |
| 12 | +import env from 'env-var'; |
| 13 | +import { existsSync } from 'fs'; |
| 14 | +import fs from 'fs/promises'; |
| 15 | +import { |
| 16 | + fromFailablePromise, |
| 17 | + liftZodParseResult |
| 18 | +} from '../../utils/TaskEither'; |
| 19 | + |
| 20 | +export const configurationSchema = z.object({ |
| 21 | + pluginDir: z.string().optional(), |
| 22 | + plugins: z.record( |
| 23 | + z.object({ |
| 24 | + path: z.string().optional(), |
| 25 | + config: z.unknown() |
| 26 | + }) |
| 27 | + ) |
| 28 | +}); |
| 29 | + |
| 30 | +export type Configuration = z.infer<typeof configurationSchema>; |
| 31 | + |
| 32 | +export const _readConfiguration = |
| 33 | + <T>(parseConfiguration: (s: string) => z.SafeParseReturnType<T, T>) => |
| 34 | + (cfgPath?: string): TaskEither<string, T> => |
| 35 | + pipe( |
| 36 | + TE.Do, |
| 37 | + TE.bind('finalCfgPath', () => |
| 38 | + TE.fromIOEither(() => { |
| 39 | + const finalCfgPath = path.resolve( |
| 40 | + cfgPath ?? |
| 41 | + env.get('MINAUTH_CONFIG').default('config.yaml').asString() |
| 42 | + ); |
| 43 | + |
| 44 | + console.log(`reading configuration from ${finalCfgPath}`); |
| 45 | + |
| 46 | + return existsSync(finalCfgPath) |
| 47 | + ? E.right(finalCfgPath) |
| 48 | + : E.left('configuration file does not exists'); |
| 49 | + }) |
| 50 | + ), |
| 51 | + TE.bind('cfgFileContent', ({ finalCfgPath }) => |
| 52 | + fromFailablePromise<string>(() => fs.readFile(finalCfgPath, 'utf-8')) |
| 53 | + ), |
| 54 | + TE.chain(({ cfgFileContent }) => |
| 55 | + liftZodParseResult(parseConfiguration(cfgFileContent)) |
| 56 | + ) |
| 57 | + ); |
| 58 | + |
| 59 | +export const readConfiguration = _readConfiguration( |
| 60 | + configurationSchema.safeParse |
| 61 | +); |
| 62 | + |
| 63 | +// |
| 64 | + |
| 65 | +export type UntypedPlugin = IMinAuthPlugin<unknown, unknown>; |
| 66 | + |
| 67 | +export type UntypedPluginFactory = IMinAuthPluginFactory< |
| 68 | + UntypedPlugin, |
| 69 | + unknown, |
| 70 | + unknown, |
| 71 | + unknown |
| 72 | +>; |
| 73 | + |
| 74 | +export type UntypedPluginModule = { default: UntypedPluginFactory }; |
| 75 | + |
| 76 | +const importPluginModule = ( |
| 77 | + pluginModulePath: string |
| 78 | +): TaskEither<string, UntypedPluginModule> => |
| 79 | + fromFailablePromise(() => import(pluginModulePath)); |
| 80 | + |
| 81 | +const validatePluginCfg = ( |
| 82 | + cfg: unknown, |
| 83 | + factory: UntypedPluginFactory |
| 84 | +): TaskEither<string, unknown> => |
| 85 | + liftZodParseResult(factory.configurationSchema.safeParse(cfg)); |
| 86 | + |
| 87 | +const initializePlugin = ( |
| 88 | + pluginModulePath: string, |
| 89 | + pluginCfg: unknown |
| 90 | +): TaskEither<string, UntypedPlugin> => |
| 91 | + pipe( |
| 92 | + TE.Do, |
| 93 | + TE.bind('pluginModule', () => importPluginModule(pluginModulePath)), |
| 94 | + TE.let('pluginFactory', ({ pluginModule }) => pluginModule.default), |
| 95 | + TE.bind('typedPluginCfg', ({ pluginFactory }) => |
| 96 | + validatePluginCfg(pluginCfg, pluginFactory) |
| 97 | + ), |
| 98 | + TE.chain(({ pluginFactory, typedPluginCfg }) => |
| 99 | + pluginFactory.initialize(typedPluginCfg) |
| 100 | + ) |
| 101 | + ); |
| 102 | + |
| 103 | +export const initializePlugins = ( |
| 104 | + cfg: Configuration |
| 105 | +): TaskEither<string, Record<string, UntypedPlugin>> => { |
| 106 | + const resolvePluginModulePath = |
| 107 | + (name: string, optionalPath?: string) => () => { |
| 108 | + const dir = |
| 109 | + cfg.pluginDir === undefined |
| 110 | + ? process.cwd() |
| 111 | + : path.resolve(cfg.pluginDir); |
| 112 | + |
| 113 | + return optionalPath === undefined |
| 114 | + ? path.join(dir, name) |
| 115 | + : path.resolve(optionalPath); |
| 116 | + }; |
| 117 | + |
| 118 | + const resolveModulePathAndInitializePlugin = ( |
| 119 | + pluginName: string, |
| 120 | + pluginCfg: { |
| 121 | + path?: string | undefined; |
| 122 | + config?: unknown; |
| 123 | + } |
| 124 | + ): TaskEither<string, UntypedPlugin> => |
| 125 | + pipe( |
| 126 | + TE.fromIO(resolvePluginModulePath(pluginName, pluginCfg.path)), |
| 127 | + TE.chain((modulePath: string) => |
| 128 | + initializePlugin(modulePath, pluginCfg.config ?? {}) |
| 129 | + ) |
| 130 | + ); |
| 131 | + |
| 132 | + const Applicative = TE.getApplicativeTaskValidation( |
| 133 | + T.ApplyPar, |
| 134 | + pipe(Str.Semigroup, S.intercalate(', ')) |
| 135 | + ); |
| 136 | + |
| 137 | + return R.traverseWithIndex(Applicative)(resolveModulePathAndInitializePlugin)( |
| 138 | + cfg.plugins |
| 139 | + ); |
| 140 | +}; |
0 commit comments