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.io/license
|
---|
8 | */
|
---|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | exports.getAppModulePath = exports.findBootstrapModulePath = exports.findBootstrapModuleCall = void 0;
|
---|
30 | const core_1 = require("@angular-devkit/core");
|
---|
31 | const schematics_1 = require("@angular-devkit/schematics");
|
---|
32 | const path_1 = require("path");
|
---|
33 | const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
|
---|
34 | const ast_utils_1 = require("../utility/ast-utils");
|
---|
35 | function findBootstrapModuleCall(host, mainPath) {
|
---|
36 | const mainBuffer = host.read(mainPath);
|
---|
37 | if (!mainBuffer) {
|
---|
38 | throw new schematics_1.SchematicsException(`Main file (${mainPath}) not found`);
|
---|
39 | }
|
---|
40 | const mainText = mainBuffer.toString('utf-8');
|
---|
41 | const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
|
---|
42 | const allNodes = ast_utils_1.getSourceNodes(source);
|
---|
43 | let bootstrapCall = null;
|
---|
44 | for (const node of allNodes) {
|
---|
45 | let bootstrapCallNode = null;
|
---|
46 | bootstrapCallNode = ast_utils_1.findNode(node, ts.SyntaxKind.Identifier, 'bootstrapModule');
|
---|
47 | // Walk up the parent until CallExpression is found.
|
---|
48 | while (bootstrapCallNode &&
|
---|
49 | bootstrapCallNode.parent &&
|
---|
50 | bootstrapCallNode.parent.kind !== ts.SyntaxKind.CallExpression) {
|
---|
51 | bootstrapCallNode = bootstrapCallNode.parent;
|
---|
52 | }
|
---|
53 | if (bootstrapCallNode !== null &&
|
---|
54 | bootstrapCallNode.parent !== undefined &&
|
---|
55 | bootstrapCallNode.parent.kind === ts.SyntaxKind.CallExpression) {
|
---|
56 | bootstrapCall = bootstrapCallNode.parent;
|
---|
57 | break;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | return bootstrapCall;
|
---|
61 | }
|
---|
62 | exports.findBootstrapModuleCall = findBootstrapModuleCall;
|
---|
63 | function findBootstrapModulePath(host, mainPath) {
|
---|
64 | const bootstrapCall = findBootstrapModuleCall(host, mainPath);
|
---|
65 | if (!bootstrapCall) {
|
---|
66 | throw new schematics_1.SchematicsException('Bootstrap call not found');
|
---|
67 | }
|
---|
68 | const bootstrapModule = bootstrapCall.arguments[0];
|
---|
69 | const mainBuffer = host.read(mainPath);
|
---|
70 | if (!mainBuffer) {
|
---|
71 | throw new schematics_1.SchematicsException(`Client app main file (${mainPath}) not found`);
|
---|
72 | }
|
---|
73 | const mainText = mainBuffer.toString('utf-8');
|
---|
74 | const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
|
---|
75 | const allNodes = ast_utils_1.getSourceNodes(source);
|
---|
76 | const bootstrapModuleRelativePath = allNodes
|
---|
77 | .filter(ts.isImportDeclaration)
|
---|
78 | .filter((imp) => {
|
---|
79 | return ast_utils_1.findNode(imp, ts.SyntaxKind.Identifier, bootstrapModule.getText());
|
---|
80 | })
|
---|
81 | .map((imp) => {
|
---|
82 | const modulePathStringLiteral = imp.moduleSpecifier;
|
---|
83 | return modulePathStringLiteral.text;
|
---|
84 | })[0];
|
---|
85 | return bootstrapModuleRelativePath;
|
---|
86 | }
|
---|
87 | exports.findBootstrapModulePath = findBootstrapModulePath;
|
---|
88 | function getAppModulePath(host, mainPath) {
|
---|
89 | const moduleRelativePath = findBootstrapModulePath(host, mainPath);
|
---|
90 | const mainDir = path_1.dirname(mainPath);
|
---|
91 | const modulePath = core_1.normalize(`/${mainDir}/${moduleRelativePath}.ts`);
|
---|
92 | return modulePath;
|
---|
93 | }
|
---|
94 | exports.getAppModulePath = getAppModulePath;
|
---|