MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeProvider

Function makeProvider

packages/cli/src/internal/configFile.ts:23–61  ·  view source on GitHub ↗
(fileName: string, options?: {
  readonly formats?: ReadonlyArray<ConfigFile.Kind>
  readonly searchPaths?: ReadonlyArray<string>
})

Source from the content-addressed store, hash-verified

21
22/** @internal */
23export const makeProvider = (fileName: string, options?: {
24 readonly formats?: ReadonlyArray<ConfigFile.Kind>
25 readonly searchPaths?: ReadonlyArray<string>
26}): Effect.Effect<ConfigProvider.ConfigProvider, ConfigFile.ConfigFileError, Path.Path | FileSystem.FileSystem> =>
27 Effect.gen(function*() {
28 if (fileName === ".." || fileName.includes("/") || fileName.includes("\\")) {
29 return yield* Effect.fail(ConfigFileError("fileName must not contain path separators or '..' segments"))
30 }
31 const path = yield* Path.Path
32 const fs = yield* FileSystem.FileSystem
33 const searchPaths = options?.searchPaths && options.searchPaths.length ? options.searchPaths : ["."]
34 const extensions = options?.formats && options.formats.length
35 ? options.formats.flatMap((_) => fileExtensions[_])
36 : allFileExtensions
37 const filePaths = yield* Effect.filter(
38 searchPaths.flatMap(
39 (searchPath) => extensions.map((ext) => path.join(searchPath, `${fileName}.${ext}`))
40 ),
41 (path) => Effect.orElseSucceed(fs.exists(path), () => false)
42 )
43 const providers = yield* Effect.forEach(filePaths, (path) =>
44 pipe(
45 fs.readFileString(path),
46 Effect.mapError((_) => ConfigFileError(`Could not read file (${path})`)),
47 Effect.flatMap((content) =>
48 Effect.mapError(
49 InternalFiles.parse(path, content),
50 (message) => ConfigFileError(message)
51 )
52 ),
53 Effect.map((data) => ConfigProvider.fromJson(data))
54 ))
55
56 if (providers.length === 0) {
57 return ConfigProvider.fromMap(new Map())
58 }
59
60 return providers.reduce((acc, provider) => ConfigProvider.orElse(acc, () => provider))
61 })
62
63/** @internal */
64export const layer = (fileName: string, options?: {

Callers 1

layerFunction · 0.85

Calls 6

parseMethod · 0.80
ConfigFileErrorFunction · 0.70
pipeFunction · 0.70
failMethod · 0.65
mapMethod · 0.65
joinMethod · 0.65

Tested by

no test coverage detected