@@ -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' ;
1616import {
1717 findPropertyInAstObject ,
1818 appendValueInAstArray ,
1919} from '@schematics/angular/utility/json-utils' ;
2020import { Schema as UniversalOptions } from '@schematics/angular/universal/schema' ;
2121import { stripTsExtension , getOutputPath , getProject } from '../utils' ;
22+ import { updateWorkspace } from '@schematics/angular/utility/workspace' ;
2223
2324export 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+
6180function 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