parse-tsconfig.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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.TsconfigParseError = void 0;
  11. exports.parseTsconfigFile = parseTsconfigFile;
  12. const ts = require("typescript");
  13. const virtual_host_1 = require("./virtual-host");
  14. const path_1 = require("path");
  15. const diagnostics_1 = require("./diagnostics");
  16. /** Code of the error raised by TypeScript when a tsconfig doesn't match any files. */
  17. const NO_INPUTS_ERROR_CODE = 18003;
  18. /** Class capturing a tsconfig parse error. */
  19. class TsconfigParseError extends Error {
  20. }
  21. exports.TsconfigParseError = TsconfigParseError;
  22. /**
  23. * Attempts to parse the specified tsconfig file.
  24. *
  25. * @throws {TsconfigParseError} If the tsconfig could not be read or parsed.
  26. */
  27. function parseTsconfigFile(tsconfigPath, fileSystem) {
  28. if (!fileSystem.fileExists(tsconfigPath)) {
  29. throw new TsconfigParseError(`Tsconfig cannot not be read: ${tsconfigPath}`);
  30. }
  31. const { config, error } = ts.readConfigFile(tsconfigPath, p => fileSystem.read(fileSystem.resolve(p)));
  32. // If there is a config reading error, we never attempt to parse the config.
  33. if (error) {
  34. throw new TsconfigParseError((0, diagnostics_1.formatDiagnostics)([error], fileSystem));
  35. }
  36. const parsed = ts.parseJsonConfigFileContent(config, new virtual_host_1.FileSystemHost(fileSystem), (0, path_1.dirname)(tsconfigPath), {});
  37. // Skip the "No inputs found..." error since we don't want to interrupt the migration if a
  38. // tsconfig doesn't match a file. This will result in an empty `Program` which is still valid.
  39. const errors = parsed.errors.filter(diag => diag.code !== NO_INPUTS_ERROR_CODE);
  40. if (errors.length) {
  41. throw new TsconfigParseError((0, diagnostics_1.formatDiagnostics)(errors, fileSystem));
  42. }
  43. return parsed;
  44. }
  45. //# sourceMappingURL=parse-tsconfig.js.map