@ms-cloudpack/json-utilities 0.1.0 → 0.1.2
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/README.md +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/tsdoc-metadata.json +1 -1
- package/lib/writeJson.d.ts +10 -2
- package/lib/writeJson.d.ts.map +1 -1
- package/lib/writeJson.js +22 -6
- package/lib/writeJson.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ async function start() {
|
|
|
11
11
|
const result = (await readJSON(`path/to/file.json`)) || defaultValue;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
const app =
|
|
14
|
+
const app = createExpressApp([5000, 5001], (app) => {
|
|
15
15
|
// Set up routes.
|
|
16
16
|
app.get('*', async (req, res) => {
|
|
17
17
|
res.send(`<html>...</html>`);
|
package/lib/index.d.ts
CHANGED
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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"}
|
|
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,KAAK,gBAAgB,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,EAAwB,MAAM,eAAe,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,aAAa,
|
|
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,EAAyB,MAAM,gBAAgB,CAAC","sourcesContent":["export { readJson, readJsonSync, type ReadJsonOptions } from './readJson.js';\nexport { writeJson, writeJsonSync, type WriteJsonOptions } from './writeJson.js';\n"]}
|
package/lib/tsdoc-metadata.json
CHANGED
package/lib/writeJson.d.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
+
import type { ReadJsonOptions } from './readJson.js';
|
|
2
|
+
export interface WriteJsonOptions extends Pick<ReadJsonOptions, 'mode'> {
|
|
3
|
+
/**
|
|
4
|
+
* If true, read the file (if it exists) and preserve as much of the original formatting as possible.
|
|
5
|
+
* (For files with comments, `mode: 'permissive'` must also be set.)
|
|
6
|
+
*/
|
|
7
|
+
update?: boolean;
|
|
8
|
+
}
|
|
1
9
|
/**
|
|
2
10
|
* Writes json to a path. Ensures the path exists.
|
|
3
11
|
*/
|
|
4
|
-
export declare function writeJson(filePath: string, data:
|
|
12
|
+
export declare function writeJson(filePath: string, data: unknown, options?: WriteJsonOptions): Promise<void>;
|
|
5
13
|
/**
|
|
6
14
|
* Synchronously writes json to a path. Ensures the path exists.
|
|
7
15
|
*/
|
|
8
|
-
export declare function writeJsonSync(filePath: string, data:
|
|
16
|
+
export declare function writeJsonSync(filePath: string, data: unknown, options?: WriteJsonOptions): void;
|
|
9
17
|
//# sourceMappingURL=writeJson.d.ts.map
|
package/lib/writeJson.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writeJson.d.ts","sourceRoot":"","sources":["../src/writeJson.ts"],"names":[],"mappings":"AAKA;;GAEG;
|
|
1
|
+
{"version":3,"file":"writeJson.d.ts","sourceRoot":"","sources":["../src/writeJson.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAErD,MAAM,WAAW,gBAAiB,SAAQ,IAAI,CAAC,eAAe,EAAE,MAAM,CAAC;IACrE;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;GAEG;AACH,wBAAsB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,gBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAM9G;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,gBAAqB,GAAG,IAAI,CAMnG"}
|
package/lib/writeJson.js
CHANGED
|
@@ -2,22 +2,38 @@ import fs from 'fs';
|
|
|
2
2
|
import fsPromises from 'fs/promises';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import fsExtra from 'fs-extra';
|
|
5
|
+
import jju from 'jju';
|
|
5
6
|
/**
|
|
6
7
|
* Writes json to a path. Ensures the path exists.
|
|
7
8
|
*/
|
|
8
|
-
|
|
9
|
-
export async function writeJson(filePath, data) {
|
|
9
|
+
export async function writeJson(filePath, data, options = {}) {
|
|
10
10
|
const folderPath = path.dirname(filePath);
|
|
11
11
|
await fsExtra.ensureDir(folderPath);
|
|
12
|
-
|
|
12
|
+
const contents = stringify(filePath, data, options);
|
|
13
|
+
await fsPromises.writeFile(filePath, contents, 'utf8');
|
|
13
14
|
}
|
|
14
15
|
/**
|
|
15
16
|
* Synchronously writes json to a path. Ensures the path exists.
|
|
16
17
|
*/
|
|
17
|
-
|
|
18
|
-
export function writeJsonSync(filePath, data) {
|
|
18
|
+
export function writeJsonSync(filePath, data, options = {}) {
|
|
19
19
|
const folderPath = path.dirname(filePath);
|
|
20
20
|
fsExtra.ensureDirSync(folderPath);
|
|
21
|
-
|
|
21
|
+
const contents = stringify(filePath, data, options);
|
|
22
|
+
fs.writeFileSync(filePath, contents, 'utf8');
|
|
23
|
+
}
|
|
24
|
+
function stringify(filePath, data, options) {
|
|
25
|
+
const { update, mode } = options;
|
|
26
|
+
if (update && fs.existsSync(filePath)) {
|
|
27
|
+
// eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem
|
|
28
|
+
const originalContent = fs.readFileSync(filePath, 'utf8');
|
|
29
|
+
return jju.update(originalContent, data, {
|
|
30
|
+
mode: mode === 'permissive' ? 'json5' : 'json',
|
|
31
|
+
indent: 2,
|
|
32
|
+
quote: '"',
|
|
33
|
+
quote_keys: true,
|
|
34
|
+
no_trailing_comma: true,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return JSON.stringify(data, null, 2);
|
|
22
38
|
}
|
|
23
39
|
//# sourceMappingURL=writeJson.js.map
|
package/lib/writeJson.js.map
CHANGED
|
@@ -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;
|
|
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,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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"]}
|