@ms-cloudpack/json-utilities 0.1.5 → 0.1.6

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.d.ts CHANGED
@@ -13,16 +13,23 @@ export interface ReadJsonOptions {
13
13
  /**
14
14
  * If true, log a warning if the file can't be parsed (or exists but can't be read).
15
15
  * This does NOT log a message if the file doesn't exist.
16
+ * (Ignored if `throwOnError` is true.)
16
17
  */
17
18
  verbose?: boolean;
19
+ /**
20
+ * If true, throw on a parse error. Otherwise (default), return undefined.
21
+ * If the file doesn't exist, that is NOT an error.
22
+ */
23
+ throwOnError?: boolean;
18
24
  }
19
25
  /**
20
- * Reads JSON from a path and returns the object, or undefined if it does not exist or is unparsable.
26
+ * Reads JSON from a path and returns the object, or undefined if it does not exist.
27
+ * If the file exists but is unparsable, behavior depends on options (return undefined by default).
21
28
  */
22
29
  export declare function readJson<TData>(path: string, options?: ReadJsonOptions): Promise<TData | undefined>;
23
30
  /**
24
- * Synchronously reads JSON from a path and returns the object, or undefined if it does not exist
25
- * or is unparsable.
31
+ * Synchronously reads JSON from a path and returns the object, or undefined if it does not exist.
32
+ * If the file exists but is unparsable, behavior depends on options (return undefined by default).
26
33
  */
27
34
  export declare function readJsonSync<TData>(path: string, options?: ReadJsonOptions): TData | undefined;
28
35
  //# sourceMappingURL=readJson.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"readJson.d.ts","sourceRoot":"","sources":["../src/readJson.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,QAAQ,GAAG,YAAY,CAAC;IAC/B;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;GAEG;AACH,wBAAsB,QAAQ,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,eAAoB,GAAG,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAe7G;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,eAAoB,GAAG,KAAK,GAAG,SAAS,CAelG"}
1
+ {"version":3,"file":"readJson.d.ts","sourceRoot":"","sources":["../src/readJson.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,QAAQ,GAAG,YAAY,CAAC;IAC/B;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED;;;GAGG;AACH,wBAAsB,QAAQ,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,eAAoB,GAAG,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAmB7G;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,eAAoB,GAAG,KAAK,GAAG,SAAS,CAmBlG"}
package/lib/readJson.js CHANGED
@@ -3,44 +3,55 @@ import fs from 'fs';
3
3
  import fsPromises from 'fs/promises';
4
4
  import jju from 'jju';
5
5
  /**
6
- * Reads JSON from a path and returns the object, or undefined if it does not exist or is unparsable.
6
+ * Reads JSON from a path and returns the object, or undefined if it does not exist.
7
+ * If the file exists but is unparsable, behavior depends on options (return undefined by default).
7
8
  */
8
9
  export async function readJson(path, options = {}) {
9
10
  if (!fs.existsSync(path)) {
10
11
  return undefined;
11
12
  }
13
+ let contents;
12
14
  try {
13
- const contents = await fsPromises.readFile(path, 'utf8');
14
- return parseJson({ contents, path, ...options });
15
+ contents = await fsPromises.readFile(path, 'utf8');
15
16
  }
16
17
  catch (err) {
17
- if (options.verbose) {
18
- console.warn(`Error reading ${path}: ${err.message || err}`);
18
+ const message = `Error reading ${path}: ${err.message || err}`;
19
+ if (options.throwOnError) {
20
+ throw new Error(message);
19
21
  }
22
+ else if (options.verbose) {
23
+ console.warn(message);
24
+ }
25
+ return undefined;
20
26
  }
21
- return undefined;
27
+ return parseJson({ contents, path, ...options });
22
28
  }
23
29
  /**
24
- * Synchronously reads JSON from a path and returns the object, or undefined if it does not exist
25
- * or is unparsable.
30
+ * Synchronously reads JSON from a path and returns the object, or undefined if it does not exist.
31
+ * If the file exists but is unparsable, behavior depends on options (return undefined by default).
26
32
  */
27
33
  export function readJsonSync(path, options = {}) {
28
34
  if (!fs.existsSync(path)) {
29
35
  return undefined;
30
36
  }
37
+ let contents;
31
38
  try {
32
- const contents = fs.readFileSync(path, 'utf8');
33
- return parseJson({ contents, path, ...options });
39
+ contents = fs.readFileSync(path, 'utf8');
34
40
  }
35
41
  catch (err) {
36
- if (options.verbose) {
37
- console.warn(`Error reading ${path}: ${err.message || err}`);
42
+ const message = `Error reading ${path}: ${err.message || err}`;
43
+ if (options.throwOnError) {
44
+ throw new Error(message);
45
+ }
46
+ else if (options.verbose) {
47
+ console.warn(message);
38
48
  }
49
+ return undefined;
39
50
  }
40
- return undefined;
51
+ return parseJson({ contents, path, ...options });
41
52
  }
42
53
  function parseJson(params) {
43
- const { contents, path, mode, verbose } = params;
54
+ const { contents, path, mode, verbose, throwOnError } = params;
44
55
  try {
45
56
  if (mode === 'permissive') {
46
57
  return jju.parse(contents);
@@ -48,8 +59,12 @@ function parseJson(params) {
48
59
  return JSON.parse(contents);
49
60
  }
50
61
  catch (err) {
51
- if (verbose) {
52
- console.warn(`Error parsing JSON from ${path}: ${err.message || err}`);
62
+ const message = `Error parsing JSON from ${path}: ${err.message || err}`;
63
+ if (throwOnError) {
64
+ throw new Error(message);
65
+ }
66
+ else if (verbose) {
67
+ console.warn(message);
53
68
  }
54
69
  }
55
70
  return undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"readJson.js","sourceRoot":"","sources":["../src/readJson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,C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AAC,CAAC;QAC1E,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,SAAS,CAAQ,MAA4D;IACpF,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;IACjD,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,IAAI,OAAO,EAAE,CAAC;YACZ,OAAO,CAAC,IAAI,CAAC,2BAA2B,IAAI,KAAM,GAAa,CAAC,OAAO,IAAI,GAAG,EAAE,CAAC,CAAC;QACpF,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 */\n verbose?: boolean;\n}\n\n/**\n * Reads JSON from a path and returns the object, or undefined if it does not exist or is unparsable.\n */\nexport async function readJson<TData>(path: string, options: ReadJsonOptions = {}): Promise<TData | undefined> {\n if (!fs.existsSync(path)) {\n return undefined;\n }\n\n try {\n const contents = await fsPromises.readFile(path, 'utf8');\n return parseJson({ contents, path, ...options });\n } catch (err) {\n if (options.verbose) {\n console.warn(`Error reading ${path}: ${(err as Error).message || err}`);\n }\n }\n\n return undefined;\n}\n\n/**\n * Synchronously reads JSON from a path and returns the object, or undefined if it does not exist\n * or is unparsable.\n */\nexport function readJsonSync<TData>(path: string, options: ReadJsonOptions = {}): TData | undefined {\n if (!fs.existsSync(path)) {\n return undefined;\n }\n\n try {\n const contents = fs.readFileSync(path, 'utf8');\n return parseJson({ contents, path, ...options });\n } catch (err) {\n if (options.verbose) {\n console.warn(`Error reading ${path}: ${(err as Error).message || err}`);\n }\n }\n\n return undefined;\n}\n\nfunction parseJson<TData>(params: { contents: string; path: string } & ReadJsonOptions): TData | undefined {\n const { contents, path, mode, verbose } = 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 if (verbose) {\n console.warn(`Error parsing JSON from ${path}: ${(err as Error).message || err}`);\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,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,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,OAAO,GAAG,iBAAiB,IAAI,KAAM,GAAa,CAAC,OAAO,IAAI,GAAG,EAAE,CAAC;QAC1E,IAAI,OAAO,CAAC,YAAY,EAA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AY,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/json-utilities",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Helpers for reading/writing json files.",
5
5
  "license": "MIT",
6
6
  "type": "module",