@ms-cloudpack/json-utilities
Advanced tools
Comparing version 0.1.0 to 0.1.1
export { readJson, readJsonSync, type ReadJsonOptions } from './readJson.js'; | ||
export { writeJson, writeJsonSync } from './writeJson.js'; | ||
export { writeJson, writeJsonSync, type WriteJsonOptions } from './writeJson.js'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -8,5 +8,5 @@ // This file is read by tools that parse documentation comments conforming to the TSDoc standard. | ||
"packageName": "@microsoft/api-extractor", | ||
"packageVersion": "7.37.1" | ||
"packageVersion": "7.38.1" | ||
} | ||
] | ||
} |
@@ -0,9 +1,17 @@ | ||
import type { ReadJsonOptions } from './readJson.js'; | ||
export interface WriteJsonOptions extends Pick<ReadJsonOptions, 'mode'> { | ||
/** | ||
* If true, read the file (if it exists) and preserve as much of the original formatting as possible. | ||
* (For files with comments, `mode: 'permissive'` must also be set.) | ||
*/ | ||
update?: boolean; | ||
} | ||
/** | ||
* Writes json to a path. Ensures the path exists. | ||
*/ | ||
export declare function writeJson(filePath: string, data: any): Promise<void>; | ||
export declare function writeJson(filePath: string, data: unknown, options?: WriteJsonOptions): Promise<void>; | ||
/** | ||
* Synchronously writes json to a path. Ensures the path exists. | ||
*/ | ||
export declare function writeJsonSync(filePath: string, data: any): void; | ||
export declare function writeJsonSync(filePath: string, data: unknown, options?: WriteJsonOptions): void; | ||
//# sourceMappingURL=writeJson.d.ts.map |
@@ -5,10 +5,11 @@ import fs from 'fs'; | ||
import fsExtra from 'fs-extra'; | ||
import jju from 'jju'; | ||
/** | ||
* Writes json to a path. Ensures the path exists. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export async function writeJson(filePath, data) { | ||
export async function writeJson(filePath, data, options = {}) { | ||
const folderPath = path.dirname(filePath); | ||
await fsExtra.ensureDir(folderPath); | ||
await fsPromises.writeFile(filePath, JSON.stringify(data, null, 2), 'utf8'); | ||
const contents = stringify(filePath, data, options); | ||
await fsPromises.writeFile(filePath, contents, 'utf8'); | ||
} | ||
@@ -18,8 +19,23 @@ /** | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function writeJsonSync(filePath, data) { | ||
export function writeJsonSync(filePath, data, options = {}) { | ||
const folderPath = path.dirname(filePath); | ||
fsExtra.ensureDirSync(folderPath); | ||
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8'); | ||
const contents = stringify(filePath, data, options); | ||
fs.writeFileSync(filePath, contents, 'utf8'); | ||
} | ||
function stringify(filePath, data, options) { | ||
const { update, mode } = options; | ||
if (update && fs.existsSync(filePath)) { | ||
// eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem | ||
const originalContent = fs.readFileSync(filePath, 'utf8'); | ||
return jju.update(originalContent, data, { | ||
mode: mode === 'permissive' ? 'json5' : 'json', | ||
indent: 2, | ||
quote: '"', | ||
quote_keys: true, | ||
no_trailing_comma: true, | ||
}); | ||
} | ||
return JSON.stringify(data, null, 2); | ||
} | ||
//# sourceMappingURL=writeJson.js.map |
{ | ||
"name": "@ms-cloudpack/json-utilities", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
15954
152