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 ts = require("../third_party/github.com/Microsoft/TypeScript/lib/typescript");
|
13 | const ast_utils_1 = require("../utility/ast-utils");
|
14 | const change_1 = require("../utility/change");
|
15 | const ng_ast_utils_1 = require("../utility/ng-ast-utils");
|
16 | const project_targets_1 = require("../utility/project-targets");
|
17 | const workspace_1 = require("../utility/workspace");
|
18 | const workspace_models_1 = require("../utility/workspace-models");
|
19 | function getSourceFile(host, path) {
|
20 | const buffer = host.read(path);
|
21 | if (!buffer) {
|
22 | throw new schematics_1.SchematicsException(`Could not find ${path}.`);
|
23 | }
|
24 | const content = buffer.toString();
|
25 | const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
|
26 | return source;
|
27 | }
|
28 | function getServerModulePath(host, sourceRoot, mainPath) {
|
29 | const mainSource = getSourceFile(host, core_1.join(core_1.normalize(sourceRoot), mainPath));
|
30 | const allNodes = ast_utils_1.getSourceNodes(mainSource);
|
31 | const expNode = allNodes.find(node => ts.isExportDeclaration(node));
|
32 | if (!expNode) {
|
33 | return null;
|
34 | }
|
35 | const relativePath = expNode.moduleSpecifier;
|
36 | const modulePath = core_1.normalize(`/${sourceRoot}/${relativePath.text}.ts`);
|
37 | return modulePath;
|
38 | }
|
39 | function getComponentTemplateInfo(host, componentPath) {
|
40 | const compSource = getSourceFile(host, componentPath);
|
41 | const compMetadata = ast_utils_1.getDecoratorMetadata(compSource, 'Component', '@angular/core')[0];
|
42 | return {
|
43 | templateProp: getMetadataProperty(compMetadata, 'template'),
|
44 | templateUrlProp: getMetadataProperty(compMetadata, 'templateUrl'),
|
45 | };
|
46 | }
|
47 | function getComponentTemplate(host, compPath, tmplInfo) {
|
48 | let template = '';
|
49 | if (tmplInfo.templateProp) {
|
50 | template = tmplInfo.templateProp.getFullText();
|
51 | }
|
52 | else if (tmplInfo.templateUrlProp) {
|
53 | const templateUrl = tmplInfo.templateUrlProp.initializer.text;
|
54 | const dir = core_1.dirname(core_1.normalize(compPath));
|
55 | const templatePath = core_1.join(dir, templateUrl);
|
56 | const buffer = host.read(templatePath);
|
57 | if (buffer) {
|
58 | template = buffer.toString();
|
59 | }
|
60 | }
|
61 | return template;
|
62 | }
|
63 | function getBootstrapComponentPath(host, mainPath) {
|
64 | const modulePath = ng_ast_utils_1.getAppModulePath(host, mainPath);
|
65 | const moduleSource = getSourceFile(host, modulePath);
|
66 | const metadataNode = ast_utils_1.getDecoratorMetadata(moduleSource, 'NgModule', '@angular/core')[0];
|
67 | const bootstrapProperty = getMetadataProperty(metadataNode, 'bootstrap');
|
68 | const arrLiteral = bootstrapProperty
|
69 | .initializer;
|
70 | const componentSymbol = arrLiteral.elements[0].getText();
|
71 | const relativePath = ast_utils_1.getSourceNodes(moduleSource)
|
72 | .filter(node => node.kind === ts.SyntaxKind.ImportDeclaration)
|
73 | .filter(imp => {
|
74 | return ast_utils_1.findNode(imp, ts.SyntaxKind.Identifier, componentSymbol);
|
75 | })
|
76 | .map((imp) => {
|
77 | const pathStringLiteral = imp.moduleSpecifier;
|
78 | return pathStringLiteral.text;
|
79 | })[0];
|
80 | return core_1.join(core_1.dirname(core_1.normalize(modulePath)), relativePath + '.ts');
|
81 | }
|
82 |
|
83 | function validateProject(mainPath) {
|
84 | return (host, context) => {
|
85 | const routerOutletCheckRegex = /<router\-outlet.*?>([\s\S]*?)<\/router\-outlet>/;
|
86 | const componentPath = getBootstrapComponentPath(host, mainPath);
|
87 | const tmpl = getComponentTemplateInfo(host, componentPath);
|
88 | const template = getComponentTemplate(host, componentPath, tmpl);
|
89 | if (!routerOutletCheckRegex.test(template)) {
|
90 | const errorMsg = `Prerequisite for app shell is to define a router-outlet in your root component.`;
|
91 | context.logger.error(errorMsg);
|
92 | throw new schematics_1.SchematicsException(errorMsg);
|
93 | }
|
94 | };
|
95 | }
|
96 | function addUniversalTarget(options) {
|
97 | return () => {
|
98 |
|
99 | const universalOptions = {
|
100 | ...options,
|
101 | };
|
102 |
|
103 | delete universalOptions.route;
|
104 | return schematics_1.schematic('universal', universalOptions);
|
105 | };
|
106 | }
|
107 | function addAppShellConfigToWorkspace(options) {
|
108 | return () => {
|
109 | if (!options.route) {
|
110 | throw new schematics_1.SchematicsException(`Route is not defined`);
|
111 | }
|
112 | return workspace_1.updateWorkspace(workspace => {
|
113 | const project = workspace.projects.get(options.clientProject);
|
114 | if (!project) {
|
115 | return;
|
116 | }
|
117 | project.targets.add({
|
118 | name: 'app-shell',
|
119 | builder: workspace_models_1.Builders.AppShell,
|
120 | options: {
|
121 | browserTarget: `${options.clientProject}:build`,
|
122 | serverTarget: `${options.clientProject}:server`,
|
123 | route: options.route,
|
124 | },
|
125 | configurations: {
|
126 | production: {
|
127 | browserTarget: `${options.clientProject}:build:production`,
|
128 | serverTarget: `${options.clientProject}:server:production`,
|
129 | },
|
130 | },
|
131 | });
|
132 | });
|
133 | };
|
134 | }
|
135 | function addRouterModule(mainPath) {
|
136 | return (host) => {
|
137 | const modulePath = ng_ast_utils_1.getAppModulePath(host, mainPath);
|
138 | const moduleSource = getSourceFile(host, modulePath);
|
139 | const changes = ast_utils_1.addImportToModule(moduleSource, modulePath, 'RouterModule', '@angular/router');
|
140 | const recorder = host.beginUpdate(modulePath);
|
141 | changes.forEach((change) => {
|
142 | if (change instanceof change_1.InsertChange) {
|
143 | recorder.insertLeft(change.pos, change.toAdd);
|
144 | }
|
145 | });
|
146 | host.commitUpdate(recorder);
|
147 | return host;
|
148 | };
|
149 | }
|
150 | function getMetadataProperty(metadata, propertyName) {
|
151 | const properties = metadata.properties;
|
152 | const property = properties
|
153 | .filter(prop => prop.kind === ts.SyntaxKind.PropertyAssignment)
|
154 | .filter((prop) => {
|
155 | const name = prop.name;
|
156 | switch (name.kind) {
|
157 | case ts.SyntaxKind.Identifier:
|
158 | return name.getText() === propertyName;
|
159 | case ts.SyntaxKind.StringLiteral:
|
160 | return name.text === propertyName;
|
161 | }
|
162 | return false;
|
163 | })[0];
|
164 | return property;
|
165 | }
|
166 | function addServerRoutes(options) {
|
167 | return async (host) => {
|
168 |
|
169 | const workspace = await workspace_1.getWorkspace(host);
|
170 | const clientProject = workspace.projects.get(options.clientProject);
|
171 | if (!clientProject) {
|
172 | throw new Error('Universal schematic removed client project.');
|
173 | }
|
174 | const clientServerTarget = clientProject.targets.get('server');
|
175 | if (!clientServerTarget) {
|
176 | throw new Error('Universal schematic did not add server target to client project.');
|
177 | }
|
178 | const clientServerOptions = clientServerTarget.options;
|
179 | if (!clientServerOptions) {
|
180 | throw new schematics_1.SchematicsException('Server target does not contain options.');
|
181 | }
|
182 | const modulePath = getServerModulePath(host, clientProject.sourceRoot || 'src', options.main);
|
183 | if (modulePath === null) {
|
184 | throw new schematics_1.SchematicsException('Universal/server module not found.');
|
185 | }
|
186 | let moduleSource = getSourceFile(host, modulePath);
|
187 | if (!ast_utils_1.isImported(moduleSource, 'Routes', '@angular/router')) {
|
188 | const recorder = host.beginUpdate(modulePath);
|
189 | const routesChange = ast_utils_1.insertImport(moduleSource, modulePath, 'Routes', '@angular/router');
|
190 | if (routesChange.toAdd) {
|
191 | recorder.insertLeft(routesChange.pos, routesChange.toAdd);
|
192 | }
|
193 | const imports = ast_utils_1.getSourceNodes(moduleSource)
|
194 | .filter(node => node.kind === ts.SyntaxKind.ImportDeclaration)
|
195 | .sort((a, b) => a.getStart() - b.getStart());
|
196 | const insertPosition = imports[imports.length - 1].getEnd();
|
197 | const routeText = `\n\nconst routes: Routes = [ { path: '${options.route}', component: AppShellComponent }];`;
|
198 | recorder.insertRight(insertPosition, routeText);
|
199 | host.commitUpdate(recorder);
|
200 | }
|
201 | moduleSource = getSourceFile(host, modulePath);
|
202 | if (!ast_utils_1.isImported(moduleSource, 'RouterModule', '@angular/router')) {
|
203 | const recorder = host.beginUpdate(modulePath);
|
204 | const routerModuleChange = ast_utils_1.insertImport(moduleSource, modulePath, 'RouterModule', '@angular/router');
|
205 | if (routerModuleChange.toAdd) {
|
206 | recorder.insertLeft(routerModuleChange.pos, routerModuleChange.toAdd);
|
207 | }
|
208 | const metadataChange = ast_utils_1.addSymbolToNgModuleMetadata(moduleSource, modulePath, 'imports', 'RouterModule.forRoot(routes)');
|
209 | if (metadataChange) {
|
210 | metadataChange.forEach((change) => {
|
211 | recorder.insertRight(change.pos, change.toAdd);
|
212 | });
|
213 | }
|
214 | host.commitUpdate(recorder);
|
215 | }
|
216 | };
|
217 | }
|
218 | function addShellComponent(options) {
|
219 | const componentOptions = {
|
220 | name: 'app-shell',
|
221 | module: options.rootModuleFileName,
|
222 | project: options.clientProject,
|
223 | };
|
224 | return schematics_1.schematic('component', componentOptions);
|
225 | }
|
226 | function default_1(options) {
|
227 | return async (tree) => {
|
228 | const workspace = await workspace_1.getWorkspace(tree);
|
229 | const clientProject = workspace.projects.get(options.clientProject);
|
230 | if (!clientProject || clientProject.extensions.projectType !== 'application') {
|
231 | throw new schematics_1.SchematicsException(`A client project type of "application" is required.`);
|
232 | }
|
233 | const clientBuildTarget = clientProject.targets.get('build');
|
234 | if (!clientBuildTarget) {
|
235 | throw project_targets_1.targetBuildNotFoundError();
|
236 | }
|
237 | const clientBuildOptions = (clientBuildTarget.options || {});
|
238 | return schematics_1.chain([
|
239 | validateProject(clientBuildOptions.main),
|
240 | clientProject.targets.has('server') ? schematics_1.noop() : addUniversalTarget(options),
|
241 | addAppShellConfigToWorkspace(options),
|
242 | addRouterModule(clientBuildOptions.main),
|
243 | addServerRoutes(options),
|
244 | addShellComponent(options),
|
245 | ]);
|
246 | };
|
247 | }
|
248 | exports.default = default_1;
|