@ms-cloudpack/json-utilities
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -16,14 +16,21 @@ /** | ||
* This does NOT log a message if the file doesn't exist. | ||
* (Ignored if `throwOnError` is true.) | ||
*/ | ||
verbose?: boolean; | ||
/** | ||
* If true, throw on a parse error. Otherwise (default), return undefined. | ||
* If the file doesn't exist, that is NOT an error. | ||
*/ | ||
throwOnError?: boolean; | ||
} | ||
/** | ||
* Reads JSON from a path and returns the object, or undefined if it does not exist or is unparsable. | ||
* Reads JSON from a path and returns the object, or undefined if it does not exist. | ||
* If the file exists but is unparsable, behavior depends on options (return undefined by default). | ||
*/ | ||
export declare function readJson<TData>(path: string, options?: ReadJsonOptions): Promise<TData | undefined>; | ||
/** | ||
* Synchronously reads JSON from a path and returns the object, or undefined if it does not exist | ||
* or is unparsable. | ||
* Synchronously reads JSON from a path and returns the object, or undefined if it does not exist. | ||
* If the file exists but is unparsable, behavior depends on options (return undefined by default). | ||
*/ | ||
export declare function readJsonSync<TData>(path: string, options?: ReadJsonOptions): TData | undefined; | ||
//# sourceMappingURL=readJson.d.ts.map |
@@ -6,3 +6,4 @@ import fs from 'fs'; | ||
/** | ||
* Reads JSON from a path and returns the object, or undefined if it does not exist or is unparsable. | ||
* Reads JSON from a path and returns the object, or undefined if it does not exist. | ||
* If the file exists but is unparsable, behavior depends on options (return undefined by default). | ||
*/ | ||
@@ -13,16 +14,21 @@ export async function readJson(path, options = {}) { | ||
} | ||
let contents; | ||
try { | ||
const contents = await fsPromises.readFile(path, 'utf8'); | ||
return parseJson({ contents, path, ...options }); | ||
contents = await fsPromises.readFile(path, 'utf8'); | ||
} | ||
catch (err) { | ||
if (options.verbose) { | ||
console.warn(`Error reading ${path}: ${err.message || err}`); | ||
const message = `Error reading ${path}: ${err.message || err}`; | ||
if (options.throwOnError) { | ||
throw new Error(message); | ||
} | ||
else if (options.verbose) { | ||
console.warn(message); | ||
} | ||
return undefined; | ||
} | ||
return undefined; | ||
return parseJson({ contents, path, ...options }); | ||
} | ||
/** | ||
* Synchronously reads JSON from a path and returns the object, or undefined if it does not exist | ||
* or is unparsable. | ||
* Synchronously reads JSON from a path and returns the object, or undefined if it does not exist. | ||
* If the file exists but is unparsable, behavior depends on options (return undefined by default). | ||
*/ | ||
@@ -33,15 +39,20 @@ export function readJsonSync(path, options = {}) { | ||
} | ||
let contents; | ||
try { | ||
const contents = fs.readFileSync(path, 'utf8'); | ||
return parseJson({ contents, path, ...options }); | ||
contents = fs.readFileSync(path, 'utf8'); | ||
} | ||
catch (err) { | ||
if (options.verbose) { | ||
console.warn(`Error reading ${path}: ${err.message || err}`); | ||
const message = `Error reading ${path}: ${err.message || err}`; | ||
if (options.throwOnError) { | ||
throw new Error(message); | ||
} | ||
else if (options.verbose) { | ||
console.warn(message); | ||
} | ||
return undefined; | ||
} | ||
return undefined; | ||
return parseJson({ contents, path, ...options }); | ||
} | ||
function parseJson(params) { | ||
const { contents, path, mode, verbose } = params; | ||
const { contents, path, mode, verbose, throwOnError } = params; | ||
try { | ||
@@ -54,5 +65,9 @@ if (mode === 'permissive') { | ||
catch (err) { | ||
if (verbose) { | ||
console.warn(`Error parsing JSON from ${path}: ${err.message || err}`); | ||
const message = `Error parsing JSON from ${path}: ${err.message || err}`; | ||
if (throwOnError) { | ||
throw new Error(message); | ||
} | ||
else if (verbose) { | ||
console.warn(message); | ||
} | ||
} | ||
@@ -59,0 +74,0 @@ return undefined; |
{ | ||
"name": "@ms-cloudpack/json-utilities", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Helpers for reading/writing json files.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20134
193