ng-ast-utils.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. var desc = Object.getOwnPropertyDescriptor(m, k);
  12. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  13. desc = { enumerable: true, get: function() { return m[k]; } };
  14. }
  15. Object.defineProperty(o, k2, desc);
  16. }) : (function(o, m, k, k2) {
  17. if (k2 === undefined) k2 = k;
  18. o[k2] = m[k];
  19. }));
  20. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  21. Object.defineProperty(o, "default", { enumerable: true, value: v });
  22. }) : function(o, v) {
  23. o["default"] = v;
  24. });
  25. var __importStar = (this && this.__importStar) || (function () {
  26. var ownKeys = function(o) {
  27. ownKeys = Object.getOwnPropertyNames || function (o) {
  28. var ar = [];
  29. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  30. return ar;
  31. };
  32. return ownKeys(o);
  33. };
  34. return function (mod) {
  35. if (mod && mod.__esModule) return mod;
  36. var result = {};
  37. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  38. __setModuleDefault(result, mod);
  39. return result;
  40. };
  41. })();
  42. Object.defineProperty(exports, "__esModule", { value: true });
  43. exports.findBootstrapModuleCall = findBootstrapModuleCall;
  44. exports.getAppModulePath = getAppModulePath;
  45. exports.isStandaloneApp = isStandaloneApp;
  46. const schematics_1 = require("@angular-devkit/schematics");
  47. const posix_1 = require("node:path/posix");
  48. const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
  49. const ast_utils_1 = require("../utility/ast-utils");
  50. const util_1 = require("./standalone/util");
  51. function findBootstrapModuleCall(host, mainPath) {
  52. const mainText = host.readText(mainPath);
  53. const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
  54. const allNodes = (0, ast_utils_1.getSourceNodes)(source);
  55. let bootstrapCall = null;
  56. for (const node of allNodes) {
  57. let bootstrapCallNode = null;
  58. bootstrapCallNode = (0, ast_utils_1.findNode)(node, ts.SyntaxKind.Identifier, 'bootstrapModule');
  59. // Walk up the parent until CallExpression is found.
  60. while (bootstrapCallNode &&
  61. bootstrapCallNode.parent &&
  62. bootstrapCallNode.parent.kind !== ts.SyntaxKind.CallExpression) {
  63. bootstrapCallNode = bootstrapCallNode.parent;
  64. }
  65. if (bootstrapCallNode !== null &&
  66. bootstrapCallNode.parent !== undefined &&
  67. bootstrapCallNode.parent.kind === ts.SyntaxKind.CallExpression) {
  68. bootstrapCall = bootstrapCallNode.parent;
  69. break;
  70. }
  71. }
  72. return bootstrapCall;
  73. }
  74. function findBootstrapModulePath(host, mainPath) {
  75. const bootstrapCall = findBootstrapModuleCall(host, mainPath);
  76. if (!bootstrapCall) {
  77. throw new schematics_1.SchematicsException('Bootstrap call not found');
  78. }
  79. const bootstrapModule = bootstrapCall.arguments[0];
  80. const mainText = host.readText(mainPath);
  81. const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
  82. const allNodes = (0, ast_utils_1.getSourceNodes)(source);
  83. const bootstrapModuleRelativePath = allNodes
  84. .filter(ts.isImportDeclaration)
  85. .filter((imp) => {
  86. return (0, ast_utils_1.findNode)(imp, ts.SyntaxKind.Identifier, bootstrapModule.getText());
  87. })
  88. .map((imp) => {
  89. const modulePathStringLiteral = imp.moduleSpecifier;
  90. return modulePathStringLiteral.text;
  91. })[0];
  92. return bootstrapModuleRelativePath;
  93. }
  94. function getAppModulePath(host, mainPath) {
  95. const moduleRelativePath = findBootstrapModulePath(host, mainPath);
  96. const mainDir = (0, posix_1.dirname)(mainPath);
  97. const modulePath = (0, posix_1.join)(mainDir, `${moduleRelativePath}.ts`);
  98. return modulePath;
  99. }
  100. function isStandaloneApp(host, mainPath) {
  101. try {
  102. (0, util_1.findBootstrapApplicationCall)(host, mainPath);
  103. return true;
  104. }
  105. catch (error) {
  106. if (error instanceof schematics_1.SchematicsException) {
  107. return false;
  108. }
  109. throw error;
  110. }
  111. }