ng-ast-utils.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * @license
  5. * Copyright Google Inc. All Rights Reserved.
  6. *
  7. * Use of this source code is governed by an MIT-style license that can be
  8. * found in the LICENSE file at https://angular.io/license
  9. */
  10. const core_1 = require("@angular-devkit/core");
  11. const schematics_1 = require("@angular-devkit/schematics");
  12. const path_1 = require("path");
  13. const ts = require("../third_party/github.com/Microsoft/TypeScript/lib/typescript");
  14. const ast_utils_1 = require("../utility/ast-utils");
  15. function findBootstrapModuleCall(host, mainPath) {
  16. const mainBuffer = host.read(mainPath);
  17. if (!mainBuffer) {
  18. throw new schematics_1.SchematicsException(`Main file (${mainPath}) not found`);
  19. }
  20. const mainText = mainBuffer.toString('utf-8');
  21. const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
  22. const allNodes = ast_utils_1.getSourceNodes(source);
  23. let bootstrapCall = null;
  24. for (const node of allNodes) {
  25. let bootstrapCallNode = null;
  26. bootstrapCallNode = ast_utils_1.findNode(node, ts.SyntaxKind.Identifier, 'bootstrapModule');
  27. // Walk up the parent until CallExpression is found.
  28. while (bootstrapCallNode && bootstrapCallNode.parent
  29. && bootstrapCallNode.parent.kind !== ts.SyntaxKind.CallExpression) {
  30. bootstrapCallNode = bootstrapCallNode.parent;
  31. }
  32. if (bootstrapCallNode !== null &&
  33. bootstrapCallNode.parent !== undefined &&
  34. bootstrapCallNode.parent.kind === ts.SyntaxKind.CallExpression) {
  35. bootstrapCall = bootstrapCallNode.parent;
  36. break;
  37. }
  38. }
  39. return bootstrapCall;
  40. }
  41. exports.findBootstrapModuleCall = findBootstrapModuleCall;
  42. function findBootstrapModulePath(host, mainPath) {
  43. const bootstrapCall = findBootstrapModuleCall(host, mainPath);
  44. if (!bootstrapCall) {
  45. throw new schematics_1.SchematicsException('Bootstrap call not found');
  46. }
  47. const bootstrapModule = bootstrapCall.arguments[0];
  48. const mainBuffer = host.read(mainPath);
  49. if (!mainBuffer) {
  50. throw new schematics_1.SchematicsException(`Client app main file (${mainPath}) not found`);
  51. }
  52. const mainText = mainBuffer.toString('utf-8');
  53. const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
  54. const allNodes = ast_utils_1.getSourceNodes(source);
  55. const bootstrapModuleRelativePath = allNodes
  56. .filter(node => node.kind === ts.SyntaxKind.ImportDeclaration)
  57. .filter(imp => {
  58. return ast_utils_1.findNode(imp, ts.SyntaxKind.Identifier, bootstrapModule.getText());
  59. })
  60. .map((imp) => {
  61. const modulePathStringLiteral = imp.moduleSpecifier;
  62. return modulePathStringLiteral.text;
  63. })[0];
  64. return bootstrapModuleRelativePath;
  65. }
  66. exports.findBootstrapModulePath = findBootstrapModulePath;
  67. function getAppModulePath(host, mainPath) {
  68. const moduleRelativePath = findBootstrapModulePath(host, mainPath);
  69. const mainDir = path_1.dirname(mainPath);
  70. const modulePath = core_1.normalize(`/${mainDir}/${moduleRelativePath}.ts`);
  71. return modulePath;
  72. }
  73. exports.getAppModulePath = getAppModulePath;