@ms-cloudpack/json-utilities 0.1.3 → 0.1.4

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.
@@ -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;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"]}
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,CAAC;QACzB,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,IAAI,CAAC;QACH,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;IACnD,CAAC;I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AI,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"]}
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.38.3"
8
+ "packageVersion": "7.39.4"
9
9
  }
10
10
  ]
11
11
  }
package/lib/writeJson.js CHANGED
@@ -34,6 +34,6 @@ function stringify(filePath, data, options) {
34
34
  no_trailing_comma: true,
35
35
  });
36
36
  }
37
- return JSON.stringify(data, null, 2);
37
+ return JSON.stringify(data, null, 2) + '\n';
38
38
  }
39
39
  //# sourceMappingURL=writeJson.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"writeJson.js","sourceRoot":"","sources":["../src/writeJson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,OAAO,MAAM,UAAU,CAAC;AAC/B,OAAO,GAAG,MAAM,KAAK,CAAC;AAWtB;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,QAAgB,EAAE,IAAa,EAAE,UAA4B,EAAE;IAC7F,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC1C,MAAM,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;IAEpC,MAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACpD,MAAM,UAAU,CAAC,SAAS,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AACzD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,QAAgB,EAAE,IAAa,EAAE,UAA4B,EAAE;IAC3F,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC1C,OAAO,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;IAElC,MAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACpD,EAAE,CAAC,aAAa,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AAC/C,CAAC;AAED,SAAS,SAAS,CAAC,QAAgB,EAAE,IAAa,EAAE,OAAyB;IAC3E,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;IAEjC,IAAI,MAAM,IAAI,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;QACrC,qEAAqE;QACrE,MAAM,eAAe,GAAG,EAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAC1D,OAAO,GAAG,CAAC,MAAM,CAAC,eAAe,EAAE,IAAI,EAAE;YACvC,IAAI,EAAE,IAAI,KAAK,YAAY,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM;YAC9C,MAAM,EAAE,CAAC;YACT,KAAK,EAAE,GAAG;YACV,UAAU,EAAE,IAAI;YAChB,iBAAiB,EAAE,IAAI;SACxB,CAAC,CAAC;KACJ;IAED,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AACvC,CAAC","sourcesContent":["import fs from 'fs';\nimport fsPromises from 'fs/promises';\nimport path from 'path';\nimport fsExtra from 'fs-extra';\nimport jju from 'jju';\nimport type { ReadJsonOptions } from './readJson.js';\n\nexport interface WriteJsonOptions extends Pick<ReadJsonOptions, 'mode'> {\n /**\n * If true, read the file (if it exists) and preserve as much of the original formatting as possible.\n * (For files with comments, `mode: 'permissive'` must also be set.)\n */\n update?: boolean;\n}\n\n/**\n * Writes json to a path. Ensures the path exists.\n */\nexport async function writeJson(filePath: string, data: unknown, options: WriteJsonOptions = {}): Promise<void> {\n const folderPath = path.dirname(filePath);\n await fsExtra.ensureDir(folderPath);\n\n const contents = stringify(filePath, data, options);\n await fsPromises.writeFile(filePath, contents, 'utf8');\n}\n\n/**\n * Synchronously writes json to a path. Ensures the path exists.\n */\nexport function writeJsonSync(filePath: string, data: unknown, options: WriteJsonOptions = {}): void {\n const folderPath = path.dirname(filePath);\n fsExtra.ensureDirSync(folderPath);\n\n const contents = stringify(filePath, data, options);\n fs.writeFileSync(filePath, contents, 'utf8');\n}\n\nfunction stringify(filePath: string, data: unknown, options: WriteJsonOptions): string {\n const { update, mode } = options;\n\n if (update && fs.existsSync(filePath)) {\n // eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem\n const originalContent = fs.readFileSync(filePath, 'utf8');\n return jju.update(originalContent, data, {\n mode: mode === 'permissive' ? 'json5' : 'json',\n indent: 2,\n quote: '\"',\n quote_keys: true,\n no_trailing_comma: true,\n });\n }\n\n return JSON.stringify(data, null, 2);\n}\n"]}
1
+ {"version":3,"file":"writeJson.js","sourceRoot":"","sources":["../src/writeJson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,OAAO,MAAM,UAAU,CAAC;AAC/B,OAAO,GAAG,MAAM,KAAK,CAAC;AAWtB;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,QAAgB,EAAE,IAAa,EAAE,UAA4B,EAAE;IAC7F,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC1C,MAAM,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;IAEpC,MAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACpD,MAAM,UAAU,CAAC,SAAS,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AACzD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,QAAgB,EAAE,IAAa,EAAE,UAA4B,EAAE;IAC3F,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC1C,OAAO,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;IAElC,MAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACpD,EAAE,CAAC,aAAa,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AAC/C,CAAC;AAED,SAAS,SAAS,CAAC,QAAgB,EAAE,IAAa,EAAE,OAAyB;IAC3E,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;IAEjC,IAAI,MAAM,IAAI,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QACtC,qEAAqE;QACrE,MAAM,eAAe,GAAG,EAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAC1D,OAAO,GAAG,CAAC,MAAM,CAAC,eAAe,EAAE,IAAI,EAAE;YACvC,IAAI,EAAE,IAAI,KAAK,YAAY,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM;YAC9C,MAAM,EAAE,CAAC;YACT,KAAK,EAAE,GAAG;YACV,UAAU,EAAE,IAAI;YAChB,iBAAiB,EAAE,IAAI;SACxB,CAAC,CAAC;IACL,CAAC;IAED,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC;AAC9C,CAAC","sourcesContent":["import fs from 'fs';\nimport fsPromises from 'fs/promises';\nimport path from 'path';\nimport fsExtra from 'fs-extra';\nimport jju from 'jju';\nimport type { ReadJsonOptions } from './readJson.js';\n\nexport interface WriteJsonOptions extends Pick<ReadJsonOptions, 'mode'> {\n /**\n * If true, read the file (if it exists) and preserve as much of the original formatting as possible.\n * (For files with comments, `mode: 'permissive'` must also be set.)\n */\n update?: boolean;\n}\n\n/**\n * Writes json to a path. Ensures the path exists.\n */\nexport async function writeJson(filePath: string, data: unknown, options: WriteJsonOptions = {}): Promise<void> {\n const folderPath = path.dirname(filePath);\n await fsExtra.ensureDir(folderPath);\n\n const contents = stringify(filePath, data, options);\n await fsPromises.writeFile(filePath, contents, 'utf8');\n}\n\n/**\n * Synchronously writes json to a path. Ensures the path exists.\n */\nexport function writeJsonSync(filePath: string, data: unknown, options: WriteJsonOptions = {}): void {\n const folderPath = path.dirname(filePath);\n fsExtra.ensureDirSync(folderPath);\n\n const contents = stringify(filePath, data, options);\n fs.writeFileSync(filePath, contents, 'utf8');\n}\n\nfunction stringify(filePath: string, data: unknown, options: WriteJsonOptions): string {\n const { update, mode } = options;\n\n if (update && fs.existsSync(filePath)) {\n // eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem\n const originalContent = fs.readFileSync(filePath, 'utf8');\n return jju.update(originalContent, data, {\n mode: mode === 'permissive' ? 'json5' : 'json',\n indent: 2,\n quote: '\"',\n quote_keys: true,\n no_trailing_comma: true,\n });\n }\n\n return JSON.stringify(data, null, 2) + '\\n';\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/json-utilities",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Helpers for reading/writing json files.",
5
5
  "license": "MIT",
6
6
  "type": "module",