UNPKG

3.38 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.parseJson = exports.readAndParseJson = exports.JSONFile = void 0;
11const fs_1 = require("fs");
12const jsonc_parser_1 = require("jsonc-parser");
13/** @internal */
14class JSONFile {
15 constructor(path) {
16 this.path = path;
17 const buffer = fs_1.readFileSync(this.path);
18 if (buffer) {
19 this.content = buffer.toString();
20 }
21 else {
22 throw new Error(`Could not read '${path}'.`);
23 }
24 }
25 get JsonAst() {
26 if (this._jsonAst) {
27 return this._jsonAst;
28 }
29 const errors = [];
30 this._jsonAst = jsonc_parser_1.parseTree(this.content, errors, { allowTrailingComma: true });
31 if (errors.length) {
32 formatError(this.path, errors);
33 }
34 return this._jsonAst;
35 }
36 get(jsonPath) {
37 const jsonAstNode = this.JsonAst;
38 if (!jsonAstNode) {
39 return undefined;
40 }
41 if (jsonPath.length === 0) {
42 return jsonc_parser_1.getNodeValue(jsonAstNode);
43 }
44 const node = jsonc_parser_1.findNodeAtLocation(jsonAstNode, jsonPath);
45 return node === undefined ? undefined : jsonc_parser_1.getNodeValue(node);
46 }
47 modify(jsonPath, value, insertInOrder) {
48 if (value === undefined && this.get(jsonPath) === undefined) {
49 // Cannot remove a value which doesn't exist.
50 return false;
51 }
52 let getInsertionIndex;
53 if (insertInOrder === undefined) {
54 const property = jsonPath.slice(-1)[0];
55 getInsertionIndex = (properties) => [...properties, property].sort().findIndex((p) => p === property);
56 }
57 else if (insertInOrder !== false) {
58 getInsertionIndex = insertInOrder;
59 }
60 const edits = jsonc_parser_1.modify(this.content, jsonPath, value, {
61 getInsertionIndex,
62 formattingOptions: {
63 insertSpaces: true,
64 tabSize: 2,
65 },
66 });
67 if (edits.length === 0) {
68 return false;
69 }
70 this.content = jsonc_parser_1.applyEdits(this.content, edits);
71 this._jsonAst = undefined;
72 return true;
73 }
74 save() {
75 fs_1.writeFileSync(this.path, this.content);
76 }
77}
78exports.JSONFile = JSONFile;
79// eslint-disable-next-line @typescript-eslint/no-explicit-any
80function readAndParseJson(path) {
81 const errors = [];
82 const content = jsonc_parser_1.parse(fs_1.readFileSync(path, 'utf-8'), errors, { allowTrailingComma: true });
83 if (errors.length) {
84 formatError(path, errors);
85 }
86 return content;
87}
88exports.readAndParseJson = readAndParseJson;
89function formatError(path, errors) {
90 const { error, offset } = errors[0];
91 throw new Error(`Failed to parse "${path}" as JSON AST Object. ${jsonc_parser_1.printParseErrorCode(error)} at location: ${offset}.`);
92}
93// eslint-disable-next-line @typescript-eslint/no-explicit-any
94function parseJson(content) {
95 return jsonc_parser_1.parse(content, undefined, { allowTrailingComma: true });
96}
97exports.parseJson = parseJson;