project-tsconfig-paths.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. const core_1 = require("@angular-devkit/core");
  11. /** Name of the default Angular CLI workspace configuration files. */
  12. const defaultWorkspaceConfigPaths = ['/angular.json', '/.angular.json'];
  13. /**
  14. * Gets all tsconfig paths from a CLI project by reading the workspace configuration
  15. * and looking for common tsconfig locations.
  16. */
  17. function getProjectTsConfigPaths(tree) {
  18. // Start with some tsconfig paths that are generally used within CLI projects. Note
  19. // that we are not interested in IDE-specific tsconfig files (e.g. /tsconfig.json)
  20. const buildPaths = new Set([]);
  21. const testPaths = new Set([]);
  22. // Add any tsconfig directly referenced in a build or test task of the angular.json workspace.
  23. const workspace = getWorkspaceConfigGracefully(tree);
  24. if (workspace) {
  25. const projects = Object.keys(workspace.projects).map(name => workspace.projects[name]);
  26. for (const project of projects) {
  27. const buildPath = getTargetTsconfigPath(project, 'build');
  28. const testPath = getTargetTsconfigPath(project, 'test');
  29. if (buildPath) {
  30. buildPaths.add(buildPath);
  31. }
  32. if (testPath) {
  33. testPaths.add(testPath);
  34. }
  35. }
  36. }
  37. // Filter out tsconfig files that don't exist in the CLI project.
  38. return {
  39. buildPaths: Array.from(buildPaths).filter(p => tree.exists(p)),
  40. testPaths: Array.from(testPaths).filter(p => tree.exists(p)),
  41. };
  42. }
  43. exports.getProjectTsConfigPaths = getProjectTsConfigPaths;
  44. /** Gets the tsconfig path from the given target within the specified project. */
  45. function getTargetTsconfigPath(project, targetName) {
  46. if (project.targets && project.targets[targetName] && project.targets[targetName].options &&
  47. project.targets[targetName].options.tsConfig) {
  48. return core_1.normalize(project.targets[targetName].options.tsConfig);
  49. }
  50. if (project.architect && project.architect[targetName] && project.architect[targetName].options &&
  51. project.architect[targetName].options.tsConfig) {
  52. return core_1.normalize(project.architect[targetName].options.tsConfig);
  53. }
  54. return null;
  55. }
  56. /**
  57. * Resolve the workspace configuration of the specified tree gracefully. We cannot use the utility
  58. * functions from the default Angular schematics because those might not be present in older
  59. * versions of the CLI. Also it's important to resolve the workspace gracefully because
  60. * the CLI project could be still using `.angular-cli.json` instead of thew new config.
  61. */
  62. function getWorkspaceConfigGracefully(tree) {
  63. const path = defaultWorkspaceConfigPaths.find(filePath => tree.exists(filePath));
  64. const configBuffer = tree.read(path);
  65. if (!path || !configBuffer) {
  66. return null;
  67. }
  68. try {
  69. // Parse the workspace file as JSON5 which is also supported for CLI
  70. // workspace configurations.
  71. return core_1.parseJson(configBuffer.toString(), core_1.JsonParseMode.Json5);
  72. }
  73. catch (e) {
  74. return null;
  75. }
  76. }
  77. //# sourceMappingURL=project-tsconfig-paths.js.map