Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 9674ebf

Browse files
alan-agius4vikerman
authored andcommitted
fix(common): update server main option when using ng add/ng update
The server target main option needs to be updated when running the add schematic to point to the server.ts entrypoint
1 parent 4d661f2 commit 9674ebf

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

modules/common/schematics/add/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('Add Schematic Rule', () => {
3434
expect(architect.build.configurations.production).toBeDefined();
3535
expect(architect.build.options.outputPath).toBe('dist/test-app/browser');
3636
expect(architect.server.options.outputPath).toBe('dist/test-app/server');
37+
expect(architect.server.options.main).toBe('projects/test-app/server.ts');
3738

3839
const productionConfig = architect.server.configurations.production;
3940
expect(productionConfig.fileReplacements).toBeDefined();

modules/common/schematics/add/index.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import {
1212
SchematicsException,
1313
noop,
1414
} from '@angular-devkit/schematics';
15-
import {parseJsonAst, JsonParseMode} from '@angular-devkit/core';
15+
import {parseJsonAst, JsonParseMode, normalize, join} from '@angular-devkit/core';
1616
import {
1717
findPropertyInAstObject,
1818
appendValueInAstArray,
1919
} from '@schematics/angular/utility/json-utils';
2020
import {Schema as UniversalOptions} from '@schematics/angular/universal/schema';
2121
import {stripTsExtension, getOutputPath, getProject} from '../utils';
22+
import {updateWorkspace} from '@schematics/angular/utility/workspace';
2223

2324
export interface AddUniversalOptions extends UniversalOptions {
2425
serverFileName?: string;
@@ -34,6 +35,7 @@ export function addUniversalCommonRule(options: AddUniversalOptions): Rule {
3435
: externalSchematic('@schematics/angular', 'universal', options),
3536
addScriptsRule(options),
3637
updateServerTsConfigRule(options),
38+
updateWorkspaceConfigRule(options),
3739
]);
3840
};
3941
}
@@ -58,6 +60,23 @@ function addScriptsRule(options: AddUniversalOptions): Rule {
5860
};
5961
}
6062

63+
function updateWorkspaceConfigRule(options: AddUniversalOptions): Rule {
64+
return () => {
65+
return updateWorkspace(workspace => {
66+
const project = workspace.projects.get(options.clientProject);
67+
if (!project) {
68+
return;
69+
}
70+
71+
const serverTarget = project.targets.get('server');
72+
serverTarget.options.main = join(
73+
normalize(project.root),
74+
stripTsExtension(options.serverFileName) + '.ts',
75+
);
76+
});
77+
};
78+
}
79+
6180
function updateServerTsConfigRule(options: AddUniversalOptions): Rule {
6281
return async host => {
6382
const clientProject = await getProject(host, options.clientProject);
@@ -102,17 +121,3 @@ function updateServerTsConfigRule(options: AddUniversalOptions): Rule {
102121
}
103122
};
104123
}
105-
106-
export default function (options: UniversalOptions): Rule {
107-
return async host => {
108-
const clientProject = await getProject(host, options.clientProject);
109-
110-
return chain([
111-
clientProject.targets.has('server')
112-
? noop()
113-
: externalSchematic('@schematics/angular', 'universal', options),
114-
addScriptsRule(options),
115-
updateServerTsConfigRule(options),
116-
]);
117-
};
118-
}

0 commit comments

Comments
 (0)