@@ -8,7 +8,7 @@ export namespace json {
8
8
*
9
9
* @template T
10
10
* @param {string } filePath path to a JSON file.
11
- * @param {{ [id: string]: string } } [extendsMap ] A key value pair of maps for JSON files.
11
+ * @param {{ [id: string]: string } } [namedExtends ] A key value pair of maps for JSON files.
12
12
*
13
13
* @example
14
14
* import { json } from "@speedy/json-extends";
@@ -24,7 +24,29 @@ export namespace json {
24
24
*
25
25
* @returns {Promise<T> }
26
26
*/
27
- export async function read < T > ( filePath : string , extendsMap ?: { [ id : string ] : string } ) : Promise < T > {
27
+ export async function read < T > ( filePath : string , namedExtends ?: { [ id : string ] : string } ) : Promise < T > {
28
+ return readSync < T > ( filePath , namedExtends ) ;
29
+ }
30
+
31
+ /**
32
+ * Retrieve a JSON file Synchronously. Supports `extends` with one or many existing JSON files.
33
+ *
34
+ * @template T
35
+ * @param {string } filePath path to a JSON file.
36
+ * @param {{ [id: string]: string } } [namedExtends] A key value pair of maps for JSON files.
37
+ *
38
+ * @example
39
+ * import { json } from "@speedy/json-extends";
40
+ *
41
+ * const maps = {
42
+ * "@speedy /commit-msg-hook:latest": "./node_modules/config/config.json"
43
+ * };
44
+ *
45
+ * const content = json.readSync("local-config.json", maps);
46
+ *
47
+ * @returns {T }
48
+ */
49
+ export function readSync < T > ( filePath : string , namedExtends ?: { [ id : string ] : string } ) : T {
28
50
let content = JSON . parse ( fs . readFileSync ( filePath , "utf-8" ) ) as T & { extends ?: string | string [ ] } ;
29
51
30
52
if ( _ . isEmpty ( content . extends ) ) {
@@ -34,18 +56,17 @@ export namespace json {
34
56
const configExtends = _ . castArray < string > ( content . extends ) ;
35
57
36
58
for ( let path of configExtends ) {
37
- if ( extendsMap ) {
38
- const extendsKey = extendsMap [ path ] ;
59
+ if ( namedExtends ) {
60
+ const extendsKey = namedExtends [ path ] ;
39
61
40
62
if ( extendsKey ) {
41
63
path = extendsKey ;
42
64
}
43
65
}
44
66
45
- content = _ . merge ( { } , await read ( path , extendsMap ) , content ) ;
67
+ content = _ . merge ( { } , read ( path , namedExtends ) , content ) ;
46
68
}
47
69
48
70
return content ;
49
71
}
50
-
51
72
}
0 commit comments