Skip to content

Commit 643ff77

Browse files
alan-agius4mgechev
authored andcommitted
fix(@schematics/angular): support adding app-shell after nguniversal schematics (#15606)
1 parent b94bd77 commit 643ff77

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

packages/schematics/angular/app-shell/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ function getSourceFile(host: Tree, path: string): ts.SourceFile {
4646

4747
function getServerModulePath(
4848
host: Tree,
49-
projectRoot: string,
49+
sourceRoot: string,
5050
mainPath: string,
5151
): string | null {
52-
const mainSource = getSourceFile(host, mainPath);
52+
const mainSource = getSourceFile(host, join(normalize(sourceRoot), mainPath));
5353
const allNodes = getSourceNodes(mainSource);
54-
const expNode = allNodes.filter(node => node.kind === ts.SyntaxKind.ExportDeclaration)[0];
54+
const expNode = allNodes.find(node => ts.isExportDeclaration(node));
5555
if (!expNode) {
5656
return null;
5757
}
5858
const relativePath = (expNode as ts.ExportDeclaration).moduleSpecifier as ts.StringLiteral;
59-
const modulePath = normalize(`/${projectRoot}/src/${relativePath.text}.ts`);
59+
const modulePath = normalize(`/${sourceRoot}/${relativePath.text}.ts`);
6060

6161
return modulePath;
6262
}
@@ -243,7 +243,7 @@ function addServerRoutes(options: AppShellOptions): Rule {
243243
if (!clientServerOptions) {
244244
throw new SchematicsException('Server target does not contain options.');
245245
}
246-
const modulePath = getServerModulePath(host, clientProject.root, clientServerOptions.main);
246+
const modulePath = getServerModulePath(host, clientProject.sourceRoot || 'src', options.main as string);
247247
if (modulePath === null) {
248248
throw new SchematicsException('Universal/server module not found.');
249249
}

packages/schematics/angular/app-shell/index_spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,22 @@ describe('App Shell Schematic', () => {
157157
expect(content).toMatch(/import { Routes, RouterModule } from \'@angular\/router\';/);
158158
});
159159

160+
it('should work after adding nguniversal', async () => {
161+
let tree = await schematicRunner.runSchematicAsync('universal', defaultOptions, appTree)
162+
.toPromise();
163+
164+
// change main tsconfig to mimic ng add for nguniveral
165+
const workspace = JSON.parse(appTree.readContent('/angular.json'));
166+
workspace.projects.bar.architect.server.options.main = 'server.ts';
167+
appTree.overwrite('angular.json', JSON.stringify(workspace, undefined, 2));
168+
169+
tree = await schematicRunner.runSchematicAsync('appShell', defaultOptions, tree)
170+
.toPromise();
171+
const filePath = '/projects/bar/src/app/app.server.module.ts';
172+
const content = tree.readContent(filePath);
173+
expect(content).toMatch(/import { Routes, RouterModule } from \'@angular\/router\';/);
174+
});
175+
160176
it('should define a server route', async () => {
161177
const tree = await schematicRunner.runSchematicAsync('appShell', defaultOptions, appTree)
162178
.toPromise();

0 commit comments

Comments
 (0)