Skip to content

Commit

Permalink
feat(json): implement readSync
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-agius4 committed Apr 1, 2017
1 parent 1173e7e commit 2370120
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ npm install @speedy/json-extends --save


### Usage

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

Extends supports also aliased paths, as shown in the example.

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

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

TypeScript
```ts
import { json } from "@speedy/json-extends";

Expand All @@ -50,4 +49,18 @@ JSON file
"no-dash": true
}
}
```
```

#### json.readSync(filePath, [namedExtends]) ⇒ `T`
Synchronous version of `json.read()`.

TypeScript
```ts
import { json } from "@speedy/json-extends";

const maps = {
"@speedy/commit-msg-hook:latest": "./node_modules/config/config.json"
};

const content = json.readSync("local-config.json", maps);
```
33 changes: 27 additions & 6 deletions src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export namespace json {
*
* @template T
* @param {string} filePath path to a JSON file.
* @param {{ [id: string]: string }} [extendsMap] A key value pair of maps for JSON files.
* @param {{ [id: string]: string }} [namedExtends] A key value pair of maps for JSON files.
*
* @example
* import { json } from "@speedy/json-extends";
Expand All @@ -24,7 +24,29 @@ export namespace json {
*
* @returns {Promise<T>}
*/
export async function read<T>(filePath: string, extendsMap?: { [id: string]: string }): Promise<T> {
export async function read<T>(filePath: string, namedExtends?: { [id: string]: string }): Promise<T> {
return readSync<T>(filePath, namedExtends);
}

/**
* Retrieve a JSON file Synchronously. Supports `extends` with one or many existing JSON files.
*
* @template T
* @param {string} filePath path to a JSON file.
* @param {{ [id: string]: string }} [namedExtends] A key value pair of maps for JSON files.
*
* @example
* import { json } from "@speedy/json-extends";
*
* const maps = {
* "@speedy/commit-msg-hook:latest": "./node_modules/config/config.json"
* };
*
* const content = json.readSync("local-config.json", maps);
*
* @returns {T}
*/
export function readSync<T>(filePath: string, namedExtends?: { [id: string]: string }): T {
let content = JSON.parse(fs.readFileSync(filePath, "utf-8")) as T & { extends?: string | string[] };

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

for (let path of configExtends) {
if (extendsMap) {
const extendsKey = extendsMap[path];
if (namedExtends) {
const extendsKey = namedExtends[path];

if (extendsKey) {
path = extendsKey;
}
}

content = _.merge({}, await read(path, extendsMap), content);
content = _.merge({}, read(path, namedExtends), content);
}

return content;
}

}

0 comments on commit 2370120

Please sign in to comment.