-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
oldskytree
committed
Nov 16, 2021
1 parent
37508fd
commit d2792ec
Showing
28 changed files
with
466 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ node_js: | |
- '6' | ||
- '8' | ||
script: | ||
- npm build | ||
- npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { root, section, map, option } from './core'; | ||
export { MissingOptionError, UnknownKeysError } from './errors'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import _ from 'lodash'; | ||
|
||
import type { LazyObject } from '../types/lazy'; | ||
|
||
export const isLazy = Symbol('isLazy'); | ||
|
||
export function buildLazyObject<T>(keys: Array<keyof T>, getKeyGetter: (key: keyof T) => () => (T[keyof T] | LazyObject<T[keyof T]>)): LazyObject<T> { | ||
const target = { | ||
[isLazy]: true | ||
} as LazyObject<T>; | ||
|
||
for (const key of keys) { | ||
defineLazy(target, key, getKeyGetter(key)); | ||
} | ||
|
||
return target; | ||
} | ||
|
||
export function forceParsing<T>(lazyObject: LazyObject<T>): T { | ||
return _.cloneDeep(lazyObject); | ||
} | ||
|
||
function defineLazy<T>(object: LazyObject<T>, key: keyof T, getter: () => T[keyof T] | LazyObject<T[keyof T]>): void { | ||
let defined = false; | ||
let value: T[keyof T]; | ||
|
||
Object.defineProperty(object, key, { | ||
get(): T[keyof T] { | ||
if (!defined) { | ||
defined = true; | ||
const val = getter(); | ||
|
||
if (isLazyObject(val)) { | ||
value = forceParsing(val); | ||
} else { | ||
value = val; | ||
} | ||
} | ||
|
||
return value; | ||
}, | ||
enumerable: true | ||
}); | ||
} | ||
|
||
function isLazyObject<T>(value: T): value is LazyObject<T> { | ||
return _.isObject(value) && hasOwnProperty(value, isLazy) && value[isLazy] === true; | ||
} | ||
|
||
function hasOwnProperty<T extends {}>(obj: T, prop: PropertyKey): obj is T & Record<typeof prop, unknown> { | ||
return obj.hasOwnProperty(prop); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "@tsconfig/recommended/tsconfig.json", | ||
"include": ["lib", "types"], | ||
"exclude": ["build", "test"], | ||
"compilerOptions": { | ||
"outDir": "build", | ||
"types": ["node"], | ||
"allowJs": true, | ||
"noImplicitReturns": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { LazyObject } from './lazy'; | ||
import type { MapParser } from './map'; | ||
import type { OptionParser } from './option'; | ||
import type { SectionParser } from './section'; | ||
|
||
export type ParsedConfig<T> = {[K in keyof T]: LazyObject<T[K]>}; | ||
|
||
export type RootParsedConfig<T> = ParsedConfig<{root: LazyObject<T>}>; | ||
|
||
export type Parser<T, R = any> = OptionParser<T, R> | SectionParser<T, R> | MapParser<T, R>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { isLazy } from "../lib/lazy"; | ||
|
||
export type LazyObject<T> = T & { | ||
[isLazy]: true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { DeepPartial } from './utils'; | ||
import type { RootPrefixes, ConfigParserArg } from './root'; | ||
|
||
export type LocatorArg<T> = RootPrefixes & ConfigParserArg<T>; | ||
|
||
export type Prefixes = Required<RootPrefixes> & { | ||
namePrefix: string; | ||
}; | ||
|
||
export type Node<T> = { | ||
name: string; | ||
parent: string; | ||
option?: DeepPartial<T>; | ||
envVar?: string; | ||
cliOption?: string; | ||
}; | ||
|
||
export interface Locator<T> extends Node<T> { | ||
nested: (key: keyof T) => Locator<T[keyof T]>; | ||
resetOption: <T>(newOption: T) => Locator<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { RootParsedConfig } from "./common"; | ||
import type { LazyObject } from "./lazy"; | ||
import type { Locator } from "./locator"; | ||
|
||
export type MapParser<T, R = any> = (locator: Locator<T>, config: RootParsedConfig<R>) => LazyObject<T>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { RootParsedConfig } from './common'; | ||
import type { LazyObject } from './lazy'; | ||
import type { Locator } from './locator'; | ||
|
||
export type OptionParserConfig<T, S, R = any, P = any> = { | ||
defaultValue: T | ((config: LazyObject<R>, currNode: LazyObject<R> | LazyObject<P>) => T); | ||
parseCli?: (input: string) => T; | ||
parseEnv?: (input: string) => T; | ||
validate?: (value: unknown, config: LazyObject<R>, currNode: LazyObject<R> | LazyObject<P>) => asserts value is T; | ||
map?(value: T, config: LazyObject<R>, currNode: LazyObject<R> | LazyObject<P>): S; | ||
}; | ||
|
||
export type OptionParser<T, R = any> = (locator: Locator<T>, config: RootParsedConfig<R>) => T; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { DeepPartial } from './utils'; | ||
import type { MapParser } from './map'; | ||
import type { SectionParser } from './section'; | ||
|
||
export type ConfigParserArg<T> = { | ||
options: DeepPartial<T>; | ||
env: NodeJS.ProcessEnv; | ||
argv: NodeJS.Process["argv"]; | ||
}; | ||
|
||
export type ConfigParser<T> = (arg: ConfigParserArg<T>) => T; | ||
|
||
export type RootPrefixes = { | ||
envPrefix?: string; | ||
cliPrefix?: string; | ||
}; | ||
|
||
export type RootParser<T> = SectionParser<T, T> | MapParser<T, T>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { Parser, RootParsedConfig } from './common'; | ||
import type { LazyObject } from './lazy'; | ||
import type { Locator } from './locator'; | ||
|
||
export type SectionProperties<T, R = any> = {[K in keyof T]: Parser<T[K], R>}; | ||
export type SectionParser<T, R = any> = (locator: Locator<T>, config: RootParsedConfig<R>) => LazyObject<T>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type DeepPartial<T> = { | ||
[K in keyof T]?: T[K] extends {} ? DeepPartial<T[K]> : T[K]; | ||
} |
This file was deleted.
Oops, something went wrong.