file-system-utility.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  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.dev/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.readJsonFile = readJsonFile;
  11. const schematics_1 = require("@angular-devkit/schematics");
  12. const fs_1 = require("fs");
  13. const jsonc_parser_1 = require("jsonc-parser");
  14. function readJsonFile(path) {
  15. let data;
  16. try {
  17. data = (0, fs_1.readFileSync)(path, 'utf-8');
  18. }
  19. catch (e) {
  20. if (e && typeof e === 'object' && 'code' in e && e.code === 'ENOENT') {
  21. throw new schematics_1.FileDoesNotExistException(path);
  22. }
  23. throw e;
  24. }
  25. const errors = [];
  26. const content = (0, jsonc_parser_1.parse)(data, errors, { allowTrailingComma: true });
  27. if (errors.length) {
  28. const { error, offset } = errors[0];
  29. throw new Error(`Failed to parse "${path}" as JSON AST Object. ${(0, jsonc_parser_1.printParseErrorCode)(error)} at location: ${offset}.`);
  30. }
  31. return content;
  32. }