@ms-cloudpack/json-utilities 0.1.16 → 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/readJson.js CHANGED
@@ -17,7 +17,7 @@ export async function readJson(path, options = {}) {
17
17
  catch (err) {
18
18
  const message = `Error reading ${path}: ${err.message || err}`;
19
19
  if (options.throwOnError) {
20
- throw new Error(message);
20
+ throw new Error(message, { cause: err });
21
21
  }
22
22
  else if (options.verbose) {
23
23
  console.warn(message);
@@ -41,7 +41,7 @@ export function readJsonSync(path, options = {}) {
41
41
  catch (err) {
42
42
  const message = `Error reading ${path}: ${err.message || err}`;
43
43
  if (options.throwOnError) {
44
- throw new Error(message);
44
+ throw new Error(message, { cause: err });
45
45
  }
46
46
  else if (options.verbose) {
47
47
  console.warn(message);
@@ -61,7 +61,7 @@ function parseJson(params) {
61
61
  catch (err) {
62
62
  const message = `Error parsing JSON from ${path}: ${err.message || err}`;
63
63
  if (throwOnError) {
64
- throw new Error(message);
64
+ throw new Error(message, { cause: err });
65
65
  }
66
66
  else if (verbose) {
67
67
  console.warn(message);
@@ -1 +1 @@
1
- {"version":3,"file":"readJson.js","sourceRoot":"","sources":["../src/readJson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,sGAAsG;AACtG,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,GAAG,MAAM,KAAK,CAAC;AA2BtB;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAQ,IAAY,EAAE,UAA2B,EAAE;IAC/E,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QACzB,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,IAAI,QAAgB,CAAC;IACrB,IAAI,CAAC;QACH,QAAQ,GAAG,MAAM,UAAU,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACrD,CAAC;IAAC,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D;IACpF,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,YAAY,EAAE,GAAG,MAAM,CAAC;IAC/D,IAAI,CAAC;QACH,IAAI,IAAI,KAAK,YAAY,EAAE,CAAC;YAC1B,OAAO,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAU,CAAC;QACtC,CAAC;QACD,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAU,CAAC;IACvC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,OAAO,GAAG,2BAA2B,IAAI,KAAM,GAAa,CAAC,OAAO,IAAI,GAAG,EAAE,CAAC;QACpF,IAAI,YAAY,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;QAC3B,CAAC;aAAM,IAAI,OAAO,EAAE,CAAC;YACnB,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import fs from 'fs';\n// NOTE: Importing the whole module (not named exports) is required to make Jest mocks work elsewhere.\nimport fsPromises from 'fs/promises';\nimport jju from 'jju';\n\n/**\n * Options for `readJson` and `readJsonSync`.\n */\nexport interface ReadJsonOptions {\n /**\n * How to read the JSON file:\n * - `strict`: Use `JSON.parse` (fastest, but does not give informative error messages)\n * - `permissive`: Use `jju.parse` (slower, but allows comments and trailing commas, and gives\n * informative error messages if `verbose` is true)\n * @default 'strict'\n */\n mode?: 'strict' | 'permissive';\n /**\n * If true, log a warning if the file can't be parsed (or exists but can't be read).\n * This does NOT log a message if the file doesn't exist.\n * (Ignored if `throwOnError` is true.)\n */\n verbose?: boolean;\n /**\n * If true, throw on a parse error. Otherwise (default), return undefined.\n * If the file doesn't exist, that is NOT an error.\n */\n throwOnError?: boolean;\n}\n\n/**\n * Reads JSON from a path and returns the object, or undefined if it does not exist.\n * If the file exists but is unparsable, behavior depends on options (return undefined by default).\n */\nexport async function readJson<TData>(path: string, options: ReadJsonOptions = {}): Promise<TData | undefined> {\n if (!fs.existsSync(path)) {\n return undefined;\n }\n\n let contents: string;\n try {\n contents = await fsPromises.readFile(path, 'utf8');\n } catch (err) {\n const message = `Error reading ${path}: ${(err as Error).message || err}`;\n if (options.throwOnError) {\n throw new Error(message);\n } else if (options.verbose) {\n console.warn(message);\n }\n return undefined;\n }\n\n return parseJson({ contents, path, ...options });\n}\n\n/**\n * Synchronously reads JSON from a path and returns the object, or undefined if it does not exist.\n * If the file exists but is unparsable, behavior depends on options (return undefined by default).\n */\nexport function readJsonSync<TData>(path: string, options: ReadJsonOptions = {}): TData | undefined {\n if (!fs.existsSync(path)) {\n return undefined;\n }\n\n let contents: string;\n try {\n contents = fs.readFileSync(path, 'utf8');\n } catch (err) {\n const message = `Error reading ${path}: ${(err as Error).message || err}`;\n if (options.throwOnError) {\n throw new Error(message);\n } else if (options.verbose) {\n console.warn(message);\n }\n return undefined;\n }\n\n return parseJson({ contents, path, ...options });\n}\n\nfunction parseJson<TData>(params: { contents: string; path: string } & ReadJsonOptions): TData | undefined {\n const { contents, path, mode, verbose, throwOnError } = params;\n try {\n if (mode === 'permissive') {\n return jju.parse(contents) as TData;\n }\n return JSON.parse(contents) as TData;\n } catch (err) {\n const message = `Error parsing JSON from ${path}: ${(err as Error).message || err}`;\n if (throwOnError) {\n throw new Error(message);\n } else if (verbose) {\n console.warn(message);\n }\n }\n return undefined;\n}\n"]}
1
+ {"version":3,"file":"readJson.js","sourceRoot":"","sources":["../src/readJson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,sGAAsG;AACtG,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,GAAG,MAAM,KAAK,CAAC;AA2BtB;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAQ,IAAY,EAAE,UAA2B,EAAE;IAC/E,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI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AC;aAAM,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;YAC3B,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACxB,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,OAAO,SAAS,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;AACnD,CAAC;AAED,SAAS,SAAS,CAAQ,MAA4D;IACpF,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,YAAY,EAAE,GAAG,MAAM,CAAC;IAC/D,IAAI,CAAC;QACH,IAAI,IAAI,KAAK,YAAY,EAAE,CAAC;YAC1B,OAAO,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAU,CAAC;QACtC,CAAC;QACD,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAU,CAAC;IACvC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,OAAO,GAAG,2BAA2B,IAAI,KAAM,GAAa,CAAC,OAAO,IAAI,GAAG,EAAE,CAAC;QACpF,IAAI,YAAY,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;QAC3C,CAAC;aAAM,IAAI,OAAO,EAAE,CAAC;YACnB,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import fs from 'fs';\n// NOTE: Importing the whole module (not named exports) is required to make Jest mocks work elsewhere.\nimport fsPromises from 'fs/promises';\nimport jju from 'jju';\n\n/**\n * Options for `readJson` and `readJsonSync`.\n */\nexport interface ReadJsonOptions {\n /**\n * How to read the JSON file:\n * - `strict`: Use `JSON.parse` (fastest, but does not give informative error messages)\n * - `permissive`: Use `jju.parse` (slower, but allows comments and trailing commas, and gives\n * informative error messages if `verbose` is true)\n * @default 'strict'\n */\n mode?: 'strict' | 'permissive';\n /**\n * If true, log a warning if the file can't be parsed (or exists but can't be read).\n * This does NOT log a message if the file doesn't exist.\n * (Ignored if `throwOnError` is true.)\n */\n verbose?: boolean;\n /**\n * If true, throw on a parse error. Otherwise (default), return undefined.\n * If the file doesn't exist, that is NOT an error.\n */\n throwOnError?: boolean;\n}\n\n/**\n * Reads JSON from a path and returns the object, or undefined if it does not exist.\n * If the file exists but is unparsable, behavior depends on options (return undefined by default).\n */\nexport async function readJson<TData>(path: string, options: ReadJsonOptions = {}): Promise<TData | undefined> {\n if (!fs.existsSync(path)) {\n return undefined;\n }\n\n let contents: string;\n try {\n contents = await fsPromises.readFile(path, 'utf8');\n } catch (err) {\n const message = `Error reading ${path}: ${(err as Error).message || err}`;\n if (options.throwOnError) {\n throw new Error(message, { cause: err });\n } else if (options.verbose) {\n console.warn(message);\n }\n return undefined;\n }\n\n return parseJson({ contents, path, ...options });\n}\n\n/**\n * Synchronously reads JSON from a path and returns the object, or undefined if it does not exist.\n * If the file exists but is unparsable, behavior depends on options (return undefined by default).\n */\nexport function readJsonSync<TData>(path: string, options: ReadJsonOptions = {}): TData | undefined {\n if (!fs.existsSync(path)) {\n return undefined;\n }\n\n let contents: string;\n try {\n contents = fs.readFileSync(path, 'utf8');\n } catch (err) {\n const message = `Error reading ${path}: ${(err as Error).message || err}`;\n if (options.throwOnError) {\n throw new Error(message, { cause: err });\n } else if (options.verbose) {\n console.warn(message);\n }\n return undefined;\n }\n\n return parseJson({ contents, path, ...options });\n}\n\nfunction parseJson<TData>(params: { contents: string; path: string } & ReadJsonOptions): TData | undefined {\n const { contents, path, mode, verbose, throwOnError } = params;\n try {\n if (mode === 'permissive') {\n return jju.parse(contents) as TData;\n }\n return JSON.parse(contents) as TData;\n } catch (err) {\n const message = `Error parsing JSON from ${path}: ${(err as Error).message || err}`;\n if (throwOnError) {\n throw new Error(message, { cause: err });\n } else if (verbose) {\n console.warn(message);\n }\n }\n return undefined;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/json-utilities",
3
- "version": "0.1.16",
3
+ "version": "0.1.17",
4
4
  "description": "Helpers for reading/writing JSON files in Cloudpack",
5
5
  "license": "MIT",
6
6
  "type": "module",