11import camelCase from 'camelcase' ;
2- import finder , { Package } from 'find-package-json' ;
2+ import finder from 'find-package-json' ;
33import debug from 'debug' ;
44
5+ const log = debug ( 'MongoMS:ResolveConfig' ) ;
6+
57const ENV_CONFIG_PREFIX = 'MONGOMS_' ;
68const defaultValues = new Map < string , string > ( ) ;
79
8- function getPackageJson ( directory : string ) : Package | undefined {
9- const pjIterator = finder ( directory ) ;
10- return pjIterator . next ( ) . value ;
11- }
12-
10+ /**
11+ * Set an Default value for an specific key
12+ * Mostly only used internally (for the "global-x.x" packages)
13+ * @param key The Key the default value should be assigned to
14+ * @param value The Value what the default should be
15+ */
1316export function setDefaultValue ( key : string , value : string ) : void {
1417 defaultValues . set ( key , value ) ;
1518}
1619
17- let packageJson : Package | undefined ;
18- export function reInitializePackageJson ( directory ?: string ) : void {
19- packageJson = getPackageJson ( directory || process . cwd ( ) ) ;
20+ let packageJsonConfig : {
21+ [ key : string ] : string ;
22+ } = { } ;
23+ /**
24+ * Find the nearest package.json for the provided directory
25+ * @param directory Set an custom directory to search the config in (default: process.cwd())
26+ */
27+ export function findPackageJson ( directory ?: string ) : void {
28+ const finderIterator = finder ( directory || process . cwd ( ) ) . next ( ) ;
29+ log ( `Using package.json at "${ finderIterator . filename } "` ) ;
30+ packageJsonConfig = finderIterator . value ?. config ?. mongodbMemoryServer ?? { } ;
2031}
21- reInitializePackageJson ( ) ;
32+ export const reInitializePackageJson = findPackageJson ; // TODO: remove this line on next minor version
33+ findPackageJson ( ) ;
2234
2335/**
2436 * Resolve "variableName" with a prefix of "ENV_CONFIG_PREFIX"
@@ -27,7 +39,7 @@ reInitializePackageJson();
2739export default function resolveConfig ( variableName : string ) : string | undefined {
2840 return (
2941 process . env [ `${ ENV_CONFIG_PREFIX } ${ variableName } ` ] ??
30- packageJson ?. config ?. mongodbMemoryServer ?. [ camelCase ( variableName ) ] ??
42+ packageJsonConfig ?. [ camelCase ( variableName ) ] ??
3143 defaultValues . get ( variableName )
3244 ) ;
3345}
@@ -36,11 +48,11 @@ export default function resolveConfig(variableName: string): string | undefined
3648 * Convert "1, on, yes, true" to true (otherwise false)
3749 * @param env The String / Environment Variable to check
3850 */
39- export function envToBool ( env : string ) : boolean {
51+ export function envToBool ( env : string = '' ) : boolean {
4052 return [ '1' , 'on' , 'yes' , 'true' ] . indexOf ( env . toLowerCase ( ) ) !== - 1 ;
4153}
4254
43- // enable debug "MONGOMS_DEBUG" is true
44- if ( envToBool ( resolveConfig ( 'DEBUG' ) ?? '' ) ) {
55+ // enable debug if "MONGOMS_DEBUG" is true
56+ if ( envToBool ( resolveConfig ( 'DEBUG' ) ) ) {
4557 debug . enable ( 'MongoMS:*' ) ;
4658}
0 commit comments