1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
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 |
|
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;
|