@ms-cloudpack/json-utilities 0.0.8 → 0.1.0

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/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { readJson, readJsonSync } from './readJson.js';
1
+ export { readJson, readJsonSync, type ReadJsonOptions } from './readJson.js';
2
2
  export { writeJson, writeJsonSync } from './writeJson.js';
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,eAAe,EAAE,MAAM,eAAe,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC"}
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["export { readJson, readJsonSync } from './readJson.js';\nexport { writeJson, writeJsonSync } from './writeJson.js';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAwB,MAAM,eAAe,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["export { readJson, readJsonSync, type ReadJsonOptions } from './readJson.js';\nexport { writeJson, writeJsonSync } from './writeJson.js';\n"]}
package/lib/readJson.d.ts CHANGED
@@ -1,10 +1,28 @@
1
+ /**
2
+ * Options for `readJson` and `readJsonSync`.
3
+ */
4
+ export interface ReadJsonOptions {
5
+ /**
6
+ * How to read the JSON file:
7
+ * - `strict`: Use `JSON.parse` (fastest, but does not give informative error messages)
8
+ * - `permissive`: Use `jju.parse` (slower, but allows comments and trailing commas, and gives
9
+ * informative error messages if `verbose` is true)
10
+ * @default 'strict'
11
+ */
12
+ mode?: 'strict' | 'permissive';
13
+ /**
14
+ * If true, log a warning if the file can't be parsed (or exists but can't be read).
15
+ * This does NOT log a message if the file doesn't exist.
16
+ */
17
+ verbose?: boolean;
18
+ }
1
19
  /**
2
20
  * Reads JSON from a path and returns the object, or undefined if it does not exist or is unparsable.
3
21
  */
4
- export declare function readJson<TData>(path: string): Promise<TData | undefined>;
22
+ export declare function readJson<TData>(path: string, options?: ReadJsonOptions): Promise<TData | undefined>;
5
23
  /**
6
24
  * Synchronously reads JSON from a path and returns the object, or undefined if it does not exist
7
25
  * or is unparsable.
8
26
  */
9
- export declare function readJsonSync<TData>(path: string): TData | undefined;
27
+ export declare function readJsonSync<TData>(path: string, options?: ReadJsonOptions): TData | undefined;
10
28
  //# sourceMappingURL=readJson.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"readJson.d.ts","sourceRoot":"","sources":["../src/readJson.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,wBAAsB,QAAQ,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAU9E;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS,CAUnE"}
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"}
package/lib/readJson.js CHANGED
@@ -1,31 +1,57 @@
1
1
  import fs from 'fs';
2
2
  // NOTE: Importing the whole module (not named exports) is required to make Jest mocks work elsewhere.
3
3
  import fsPromises from 'fs/promises';
4
+ import jju from 'jju';
4
5
  /**
5
6
  * Reads JSON from a path and returns the object, or undefined if it does not exist or is unparsable.
6
7
  */
7
- export async function readJson(path) {
8
- let result = undefined;
8
+ export async function readJson(path, options = {}) {
9
+ if (!fs.existsSync(path)) {
10
+ return undefined;
11
+ }
9
12
  try {
10
- result = JSON.parse(await fsPromises.readFile(path, 'utf8'));
13
+ const contents = await fsPromises.readFile(path, 'utf8');
14
+ return parseJson({ contents, path, ...options });
11
15
  }
12
- catch {
13
- /* no-op */
16
+ catch (err) {
17
+ if (options.verbose) {
18
+ console.warn(`Error reading ${path}: ${err.message || err}`);
19
+ }
14
20
  }
15
- return result;
21
+ return undefined;
16
22
  }
17
23
  /**
18
24
  * Synchronously reads JSON from a path and returns the object, or undefined if it does not exist
19
25
  * or is unparsable.
20
26
  */
21
- export function readJsonSync(path) {
22
- let result = undefined;
27
+ export function readJsonSync(path, options = {}) {
28
+ if (!fs.existsSync(path)) {
29
+ return undefined;
30
+ }
31
+ try {
32
+ const contents = fs.readFileSync(path, 'utf8');
33
+ return parseJson({ contents, path, ...options });
34
+ }
35
+ catch (err) {
36
+ if (options.verbose) {
37
+ console.warn(`Error reading ${path}: ${err.message || err}`);
38
+ }
39
+ }
40
+ return undefined;
41
+ }
42
+ function parseJson(params) {
43
+ const { contents, path, mode, verbose } = params;
23
44
  try {
24
- result = JSON.parse(fs.readFileSync(path, 'utf8'));
45
+ if (mode === 'permissive') {
46
+ return jju.parse(contents);
47
+ }
48
+ return JSON.parse(contents);
25
49
  }
26
- catch {
27
- /* no-op */
50
+ catch (err) {
51
+ if (verbose) {
52
+ console.warn(`Error parsing JSON from ${path}: ${err.message || err}`);
53
+ }
28
54
  }
29
- return result;
55
+ return undefined;
30
56
  }
31
57
  //# sourceMappingURL=readJson.js.map
@@ -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;AAErC;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAQ,IAAY;IAChD,IAAI,MAAM,GAAsB,SAAS,CAAC;IAE1C,IAAI;QACF,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,UAAU,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAU,CAAC;KACvE;IAAC,MAAM;QACN,WAAW;KACZ;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAQ,IAAY;IAC9C,IAAI,MAAM,GAAsB,SAAS,CAAC;IAE1C,IAAI;QACF,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAU,CAAC;KAC7D;IAAC,MAAM;QACN,WAAW;KACZ;IAED,OAAO,MAAM,CAAC;AAChB,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';\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): Promise<TData | undefined> {\n let result: TData | undefined = undefined;\n\n try {\n result = JSON.parse(await fsPromises.readFile(path, 'utf8')) as TData;\n } catch {\n /* no-op */\n }\n\n return result;\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): TData | undefined {\n let result: TData | undefined = undefined;\n\n try {\n result = JSON.parse(fs.readFileSync(path, 'utf8')) as TData;\n } catch {\n /* no-op */\n }\n\n return result;\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;AAqBtB;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAQ,IAAY,EAAE,UAA2B,EAAE;IAC/E,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QACxB,OAAO,SAAS,CAAC;KAClB;IAED,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,UAAU,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACzD,OAAO,SAAS,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;KAClD;IAAC,OAAO,GAAG,EAAE;QACZ,IAAI,OAAO,CAAC,OAAO,EAAE;YACnB,OAAO,CAAC,IAAI,CAAC,iBAAiB,IAAI,KAAM,GAAa,CAAC,OAAO,IAAI,GAAG,EAAE,CAAC,CAAC;SACzE;KACF;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAQ,IAAY,EAAE,UAA2B,EAAE;IAC7E,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QACxB,OAAO,SAAS,CAAC;KAClB;IAED,IAAI;QACF,MAAM,QAAQ,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC/C,OAAO,SAAS,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;KAClD;IAAC,OAAO,GAAG,EAAE;QACZ,IAAI,OAAO,CAAC,OAAO,EAAE;YACnB,OAAO,CAAC,IAAI,CAAC,iBAAiB,IAAI,KAAM,GAAa,CAAC,OAAO,IAAI,GAAG,EAAE,CAAC,CAAC;SACzE;KACF;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;QACF,IAAI,IAAI,KAAK,YAAY,EAAE;YACzB,OAAO,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAU,CAAC;SACrC;QACD,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAU,CAAC;KACtC;IAAC,OAAO,GAAG,EAAE;QACZ,IAAI,OAAO,EAAE;YACX,OAAO,CAAC,IAAI,CAAC,2BAA2B,IAAI,KAAM,GAAa,CAAC,OAAO,IAAI,GAAG,EAAE,CAAC,CAAC;SACnF;KACF;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"]}
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.36.4"
8
+ "packageVersion": "7.37.1"
9
9
  }
10
10
  ]
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/json-utilities",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
4
4
  "description": "Helpers for reading/writing json files.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -14,18 +14,23 @@
14
14
  }
15
15
  },
16
16
  "dependencies": {
17
- "fs-extra": "^11.0.0"
17
+ "fs-extra": "^11.0.0",
18
+ "jju": "^1.4.0"
18
19
  },
19
20
  "devDependencies": {
20
21
  "@ms-cloudpack/eslint-plugin-internal": "*",
21
- "@ms-cloudpack/scripts": "*"
22
+ "@ms-cloudpack/scripts": "*",
23
+ "@types/jju": "^1.4.4"
22
24
  },
23
25
  "scripts": {
24
26
  "api": "cloudpack-scripts api",
25
27
  "build:watch": "cloudpack-scripts build-watch",
26
28
  "build": "cloudpack-scripts build",
27
29
  "lint:update": "cloudpack-scripts lint-update",
28
- "lint": "cloudpack-scripts lint"
30
+ "lint": "cloudpack-scripts lint",
31
+ "test:update": "cloudpack-scripts test-update",
32
+ "test:watch": "cloudpack-scripts test-watch",
33
+ "test": "cloudpack-scripts test"
29
34
  },
30
35
  "files": [
31
36
  "lib/**/!(*.test.*)"