Skip to content

Commit 2370120

Browse files
committed
feat(json): implement readSync
1 parent 1173e7e commit 2370120

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

README.md

+22-9
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ npm install @speedy/json-extends --save
1414

1515

1616
### Usage
17-
18-
#### read(filePath, extendsMap) ⇒ <code>Promise.&lt;T&gt;</code>
17+
#### json.read(filePath, [namedExtends]) ⇒ `Promise<T>`
1918
Retrieve a JSON file. Supports `extends` with one or many existing JSON files.
2019

21-
Extends supports also aliased paths, as shown in the example.
22-
23-
| Param | Type | Required | Description |
24-
|------------|---------------------------|----------|------------------------------------------|
25-
| filePath | `string` | true | path to a JSON file. |
26-
| extendsMap | `{[id: string]: string }` | false | A key value pair of maps for JSON files. |
20+
Extends supports also Named Extends paths, as shown in the example.
2721

22+
| Param | Type | Required | Description |
23+
|--------------|---------------------------|----------|------------------------------------------|
24+
| filePath | `string` | true | path to a JSON file. |
25+
| namedExtends | `{[id: string]: string }` | false | A key value pair of named extends paths |
2826

27+
TypeScript
2928
```ts
3029
import { json } from "@speedy/json-extends";
3130

@@ -50,4 +49,18 @@ JSON file
5049
"no-dash": true
5150
}
5251
}
53-
```
52+
```
53+
54+
#### json.readSync(filePath, [namedExtends]) ⇒ `T`
55+
Synchronous version of `json.read()`.
56+
57+
TypeScript
58+
```ts
59+
import { json } from "@speedy/json-extends";
60+
61+
const maps = {
62+
"@speedy/commit-msg-hook:latest": "./node_modules/config/config.json"
63+
};
64+
65+
const content = json.readSync("local-config.json", maps);
66+
```

src/json.ts

+27-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export namespace json {
88
*
99
* @template T
1010
* @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.
1212
*
1313
* @example
1414
* import { json } from "@speedy/json-extends";
@@ -24,7 +24,29 @@ export namespace json {
2424
*
2525
* @returns {Promise<T>}
2626
*/
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 {
2850
let content = JSON.parse(fs.readFileSync(filePath, "utf-8")) as T & { extends?: string | string[] };
2951

3052
if (_.isEmpty(content.extends)) {
@@ -34,18 +56,17 @@ export namespace json {
3456
const configExtends = _.castArray<string>(content.extends);
3557

3658
for (let path of configExtends) {
37-
if (extendsMap) {
38-
const extendsKey = extendsMap[path];
59+
if (namedExtends) {
60+
const extendsKey = namedExtends[path];
3961

4062
if (extendsKey) {
4163
path = extendsKey;
4264
}
4365
}
4466

45-
content = _.merge({}, await read(path, extendsMap), content);
67+
content = _.merge({}, read(path, namedExtends), content);
4668
}
4769

4870
return content;
4971
}
50-
5172
}

0 commit comments

Comments
 (0)