@@ -13,9 +13,10 @@ import PackageJsonConfigReader, {
13
13
14
14
15
15
export interface IConfigManager {
16
- has ( key : string ) : boolean ;
17
- get ( key ?: string ) : any ;
18
- set ( key : string , value : any ) : Promise < boolean > ;
16
+ has ( key : string ) : Promise < boolean > ;
17
+ get ( key ?: string ) : Promise < Config | any > ;
18
+ set ( option : Config | string , value ?: any ) : Promise < boolean > ;
19
+ remove ( ...options : string [ ] ) : Promise < boolean > ;
19
20
}
20
21
21
22
/**
@@ -39,14 +40,15 @@ async function ConfigManagerFactory(
39
40
}
40
41
41
42
// Get the configuration contents
42
- const config : Config = await configReader . fetch ( ) ;
43
+ let config : Config = await configReader . fetch ( ) ;
43
44
44
45
45
46
/**
46
47
* Check if key exists in config
47
48
* @param key Configuration option key
48
49
*/
49
- function has ( key : string ) : boolean {
50
+ async function has ( key : string ) : Promise < boolean > {
51
+ config = await configReader . fetch ( ) ;
50
52
return config . hasOwnProperty ( key ) ;
51
53
}
52
54
@@ -55,7 +57,9 @@ async function ConfigManagerFactory(
55
57
* Retrieve value from configuration
56
58
* @param key Configuration option key
57
59
*/
58
- function get ( key ?: string ) : Config | any {
60
+ async function get ( key ?: string ) : Promise < Config | any > {
61
+ config = await configReader . fetch ( ) ;
62
+
59
63
if ( typeof key === 'undefined' ) {
60
64
return config ;
61
65
}
@@ -65,20 +69,42 @@ async function ConfigManagerFactory(
65
69
66
70
67
71
/**
68
- * Set new value for configuration option
69
- * @param key Configuration option key
70
- * @param value New value to be set
72
+ * Set new value(s) in configuration
73
+ * @param option Configuration object or object key
74
+ * @param value New value to be set if option is an object key ("string")
71
75
*/
72
- async function set ( key : string , value : any ) : Promise < boolean > {
73
- config [ key ] = value ;
74
- const saved = await configReader . update ( config ) ;
76
+ async function set ( option : Config | string , value ?: any ) : Promise < boolean > {
77
+ // If we're passing in a "key" and a "value"
78
+ if ( typeof option === 'string' && typeof value !== 'undefined' ) {
79
+ config [ option ] = value ;
80
+ // If we're passing in an object of key/value pairs
81
+ } else if ( typeof option === 'object' && Object . keys ( option ) . length > 0 ) {
82
+ config = { ...config , ...option } ;
83
+ } else {
84
+ return false ;
85
+ }
86
+
87
+ const saved : boolean = await configReader . update ( config ) ;
75
88
return saved ;
76
89
}
77
90
91
+
92
+ /**
93
+ * Removes one or more options from config
94
+ * @param option Option name or array of option names
95
+ */
96
+ async function remove ( ...options : string [ ] ) : Promise < boolean > {
97
+ options . forEach ( item => delete config [ item ] ) ;
98
+ const saved : boolean = await configReader . update ( config ) ;
99
+ return saved ;
100
+ }
101
+
102
+
78
103
// Returning config manager public API
79
104
return Object . freeze ( {
80
105
get,
81
106
has,
107
+ remove,
82
108
set,
83
109
} ) ;
84
110
}
0 commit comments