utils.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google Inc. 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. const schematics_1 = require("@angular-devkit/schematics");
  12. const config_1 = require("../../utility/config");
  13. const json_utils_1 = require("../../utility/json-utils");
  14. /** Get a project target which builder and target names matches the provided. */
  15. function getProjectTarget(project, targetName, builderName) {
  16. const projectRoot = json_utils_1.findPropertyInAstObject(project, 'root');
  17. if (!projectRoot || projectRoot.kind !== 'string') {
  18. return undefined;
  19. }
  20. const architect = json_utils_1.findPropertyInAstObject(project, 'architect');
  21. if (!architect || architect.kind !== 'object') {
  22. return undefined;
  23. }
  24. const target = json_utils_1.findPropertyInAstObject(architect, targetName);
  25. if (!target || target.kind !== 'object') {
  26. return undefined;
  27. }
  28. const builder = json_utils_1.findPropertyInAstObject(target, 'builder');
  29. // Projects who's build builder is @angular-devkit/build-ng-packagr
  30. if (builder && builder.kind === 'string' && builder.value === builderName) {
  31. return target;
  32. }
  33. return undefined;
  34. }
  35. exports.getProjectTarget = getProjectTarget;
  36. function getTargets(workspace, targetName, builderName) {
  37. const projects = json_utils_1.findPropertyInAstObject(workspace, 'projects');
  38. if (!projects || projects.kind !== 'object' || !projects.properties) {
  39. return [];
  40. }
  41. const targets = [];
  42. for (const project of projects.properties) {
  43. const projectConfig = project.value;
  44. if (projectConfig.kind !== 'object') {
  45. continue;
  46. }
  47. const target = getProjectTarget(projectConfig, targetName, builderName);
  48. if (target) {
  49. targets.push({ target, project: projectConfig });
  50. }
  51. }
  52. return targets;
  53. }
  54. exports.getTargets = getTargets;
  55. /** Helper to retreive all the options in various configurations. */
  56. function getAllOptions(builderConfig, configurationsOnly = false) {
  57. const options = [];
  58. const configurations = json_utils_1.findPropertyInAstObject(builderConfig, 'configurations');
  59. if (configurations && configurations.kind === 'object') {
  60. options.push(...configurations.properties.map(x => x.value));
  61. }
  62. if (!configurationsOnly) {
  63. options.push(json_utils_1.findPropertyInAstObject(builderConfig, 'options'));
  64. }
  65. return options.filter(o => o && o.kind === 'object');
  66. }
  67. exports.getAllOptions = getAllOptions;
  68. function getWorkspace(host) {
  69. const path = config_1.getWorkspacePath(host);
  70. const content = readJsonFileAsAstObject(host, path);
  71. if (!content) {
  72. throw new schematics_1.SchematicsException(`Could not find (${path})`);
  73. }
  74. return content;
  75. }
  76. exports.getWorkspace = getWorkspace;
  77. function readJsonFileAsAstObject(host, path) {
  78. const configBuffer = host.read(path);
  79. if (!configBuffer) {
  80. return undefined;
  81. }
  82. const content = configBuffer.toString();
  83. const astContent = core_1.parseJsonAst(content, core_1.JsonParseMode.Loose);
  84. if (!astContent || astContent.kind !== 'object') {
  85. throw new schematics_1.SchematicsException(`Invalid JSON AST Object (${path})`);
  86. }
  87. return astContent;
  88. }
  89. exports.readJsonFileAsAstObject = readJsonFileAsAstObject;
  90. function isIvyEnabled(tree, tsConfigPath) {
  91. // In version 9, Ivy is turned on by default
  92. // Ivy is opted out only when 'enableIvy' is set to false.
  93. const buffer = tree.read(tsConfigPath);
  94. if (!buffer) {
  95. return true;
  96. }
  97. const tsCfgAst = core_1.parseJsonAst(buffer.toString(), core_1.JsonParseMode.Loose);
  98. if (tsCfgAst.kind !== 'object') {
  99. return true;
  100. }
  101. const ngCompilerOptions = json_utils_1.findPropertyInAstObject(tsCfgAst, 'angularCompilerOptions');
  102. if (ngCompilerOptions && ngCompilerOptions.kind === 'object') {
  103. const enableIvy = json_utils_1.findPropertyInAstObject(ngCompilerOptions, 'enableIvy');
  104. if (enableIvy) {
  105. return !!enableIvy.value;
  106. }
  107. }
  108. const configExtends = json_utils_1.findPropertyInAstObject(tsCfgAst, 'extends');
  109. if (configExtends && configExtends.kind === 'string') {
  110. const extendedTsConfigPath = core_1.resolve(core_1.dirname(core_1.normalize(tsConfigPath)), core_1.normalize(configExtends.value));
  111. return isIvyEnabled(tree, extendedTsConfigPath);
  112. }
  113. return true;
  114. }
  115. exports.isIvyEnabled = isIvyEnabled;
  116. // TS represents paths internally with '/' and expects paths to be in this format.
  117. // angular.json expects paths with '/', but doesn't enforce them.
  118. function forwardSlashPath(path) {
  119. return path.replace(/\\/g, '/');
  120. }
  121. exports.forwardSlashPath = forwardSlashPath;