MCPcopy Index your code
hub / github.com/angular/angular-cli / JSONFile

Class JSONFile

packages/schematics/angular/utility/json-file.ts:27–115  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25
26/** @private */
27export class JSONFile {
28 content: string;
29 private eol: string;
30
31 constructor(
32 private readonly host: Tree,
33 private readonly path: string,
34 ) {
35 this.content = this.host.readText(this.path);
36 this.eol = getEOL(this.content);
37 }
38
39 private _jsonAst: Node | undefined;
40 private get JsonAst(): Node | undefined {
41 if (this._jsonAst) {
42 return this._jsonAst;
43 }
44
45 const errors: ParseError[] = [];
46 this._jsonAst = parseTree(this.content, errors, { allowTrailingComma: true });
47 if (errors.length) {
48 const { error, offset } = errors[0];
49 throw new Error(
50 `Failed to parse "${this.path}" as JSON AST Object. ${printParseErrorCode(
51 error,
52 )} at location: ${offset}.`,
53 );
54 }
55
56 return this._jsonAst;
57 }
58
59 get(jsonPath: JSONPath): unknown {
60 const jsonAstNode = this.JsonAst;
61 if (!jsonAstNode) {
62 return undefined;
63 }
64
65 if (jsonPath.length === 0) {
66 return getNodeValue(jsonAstNode);
67 }
68
69 const node = findNodeAtLocation(jsonAstNode, jsonPath);
70
71 return node === undefined ? undefined : getNodeValue(node);
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) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected