diff --git a/packages/node-fs/src/read-json.ts b/packages/node-fs/src/read-json.ts index 37efe3e8..84321e8e 100644 --- a/packages/node-fs/src/read-json.ts +++ b/packages/node-fs/src/read-json.ts @@ -1,8 +1,8 @@ import {logger} from './common'; import {parseJson} from './json'; -import {readFile} from './read-file'; +import {readFile, readFileSync} from './read-file'; -import type {MaybePromise} from '@alwatr/type-helper'; +import type {Dictionary, MaybePromise} from '@alwatr/type-helper'; /** * Enhanced read json file (async). @@ -14,7 +14,7 @@ import type {MaybePromise} from '@alwatr/type-helper'; * const fileContent = await readJson('./file.json'); * ``` */ -export function readJson(path: string): Promise; +export function readJson(path: string): Promise; /** * Enhanced read json file (sync). * @@ -26,7 +26,7 @@ export function readJson(path: string): Promise; * const fileContent = readJson('./file.json', true); * ``` */ -export function readJson(path: string, sync: true): unknown; +export function readJson(path: string, sync: true): T; /** * Enhanced read json file. * @@ -38,7 +38,7 @@ export function readJson(path: string, sync: true): unknown; * const fileContent = await readJson('./file.json', sync); * ``` */ -export function readJson(path: string, sync: boolean): MaybePromise; +export function readJson(path: string, sync: boolean): MaybePromise; /** * Enhanced read json file. * @@ -50,11 +50,12 @@ export function readJson(path: string, sync: boolean): MaybePromise; * const fileContent = await readJson('./file.json'); * ``` */ -export function readJson(path: string, sync = false): MaybePromise { +export function readJson(path: string, sync = false): MaybePromise { logger.logMethodArgs?.('readJson', {path: path.slice(-32), sync}); if (sync === true) { - return parseJson(readFile(path, true)); + return parseJson(readFileSync(path)); + } + else { + return readFile(path).then((content) => parseJson(content)); } - // else, async mode - return readFile(path).then((content) => parseJson(content)); }