UNPKG

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