(
jsonPath: JSONPath,
value: JsonValue | undefined,
insertInOrder?: InsertionIndex | false,
)
| 72 | } |
| 73 | |
| 74 | modify( |
| 75 | jsonPath: JSONPath, |
| 76 | value: JsonValue | undefined, |
| 77 | insertInOrder?: InsertionIndex | false, |
| 78 | ): void { |
| 79 | let getInsertionIndex: InsertionIndex | undefined; |
| 80 | if (insertInOrder === undefined) { |
| 81 | const property = jsonPath.slice(-1)[0]; |
| 82 | getInsertionIndex = (properties) => |
| 83 | [...properties, property].sort().findIndex((p) => p === property); |
| 84 | } else if (insertInOrder !== false) { |
| 85 | getInsertionIndex = insertInOrder; |
| 86 | } |
| 87 | |
| 88 | const edits = modify(this.content, jsonPath, value, { |
| 89 | getInsertionIndex, |
| 90 | |
| 91 | formattingOptions: { |
| 92 | eol: this.eol, |
| 93 | insertSpaces: true, |
| 94 | tabSize: 2, |
| 95 | }, |
| 96 | }); |
| 97 | |
| 98 | if (edits.length > 0) { |
| 99 | const editedContent = applyEdits(this.content, edits); |
| 100 | |
| 101 | // Update the file content if it changed |
| 102 | if (editedContent !== this.content) { |
| 103 | this.content = editedContent; |
| 104 | this.host.overwrite(this.path, editedContent); |
| 105 | this._jsonAst = undefined; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | remove(jsonPath: JSONPath): void { |
| 111 | if (this.get(jsonPath) !== undefined) { |
no test coverage detected