UNPKG

3.25 kBJavaScriptView Raw
1"use strict";
2Object.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 */
10const core_1 = require("@angular-devkit/core");
11const schematics_1 = require("@angular-devkit/schematics");
12const path_1 = require("path");
13const ts = require("../third_party/github.com/Microsoft/TypeScript/lib/typescript");
14const ast_utils_1 = require("../utility/ast-utils");
15function 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}
41exports.findBootstrapModuleCall = findBootstrapModuleCall;
42function 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}
66exports.findBootstrapModulePath = findBootstrapModulePath;
67function 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}
73exports.getAppModulePath = getAppModulePath;