-
Notifications
You must be signed in to change notification settings - Fork 1
/
fauda.ts
43 lines (39 loc) · 1.18 KB
/
fauda.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { merge, reduceRight } from 'lodash'
import { JsonObject } from 'type-fest'
import { loadArgs, loadEnv, loadFile } from './loaders'
import { normalize } from './normalizer'
import { FaudaOptions } from './types'
import { loadSchema } from './utils/loadSchema'
function normalizeOptions(options: Partial<FaudaOptions>): FaudaOptions {
const defaultOptions: FaudaOptions = {
args: process.argv,
env: process.env,
cwd: process.cwd()
}
return { ...defaultOptions, ...options }
}
async function loadFromAll(
namespace: string,
{ args, cwd, env }: FaudaOptions
): Promise<JsonObject> {
const resolvedConfig = await Promise.all([
loadEnv(namespace, env),
loadArgs(args),
loadFile(namespace, cwd)
])
const mergedConfig = reduceRight(resolvedConfig, merge, {})
return mergedConfig
}
export async function fauda<Configuration>(
namespace: string,
schema: string | JsonObject,
options: Partial<FaudaOptions> = {}
): Promise<Configuration> {
const safeOptions = normalizeOptions(options)
const safeConfig = await normalize<Configuration>(
await loadFromAll(namespace, safeOptions),
await loadSchema(schema),
safeOptions.env
)
return safeConfig
}