1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const logger_1 = require("../../util/logger");
|
4 | const projectUtils_1 = require("./projectUtils");
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function doWithJson(p, jsonPath, manipulation) {
|
16 | return projectUtils_1.doWithFiles(p, jsonPath, file => {
|
17 | return file.getContent()
|
18 | .then(content => {
|
19 | const newContent = manipulate(content, manipulation, jsonPath);
|
20 | return file.setContent(newContent)
|
21 | .then(() => p);
|
22 | });
|
23 | });
|
24 | }
|
25 | exports.doWithJson = doWithJson;
|
26 | const spacePossibilities = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, " ", " ", "\t"];
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function manipulate(jsonIn, manipulation, context = "") {
|
35 | if (!jsonIn) {
|
36 | return jsonIn;
|
37 | }
|
38 | try {
|
39 | const newline = jsonIn.endsWith("\n");
|
40 | const jsonToCompare = newline ? jsonIn.replace(/\n$/, "") : jsonIn;
|
41 | const obj = JSON.parse(jsonIn);
|
42 | let space = 2;
|
43 | for (const sp of spacePossibilities) {
|
44 | const maybe = JSON.stringify(obj, null, sp);
|
45 | if (jsonToCompare === maybe) {
|
46 | logger_1.logger.debug(`Definitely inferred space as [${sp}]`);
|
47 | space = sp;
|
48 | break;
|
49 | }
|
50 | }
|
51 | logger_1.logger.debug(`Inferred space is [${space}]`);
|
52 | manipulation(obj);
|
53 | return JSON.stringify(obj, null, space) + (newline ? "\n" : "");
|
54 | }
|
55 | catch (e) {
|
56 | logger_1.logger.warn("Syntax error parsing supposed JSON (%s). Context:[%s]. Alleged JSON:\n%s", e, context, jsonIn);
|
57 | return jsonIn;
|
58 | }
|
59 | }
|
60 | exports.manipulate = manipulate;
|
61 |
|
\ | No newline at end of file |