@@ -12,14 +12,13 @@ import {
1212 SchematicsException ,
1313 noop ,
1414} from '@angular-devkit/schematics' ;
15- import { normalize , join , parseJsonAst , JsonParseMode } from '@angular-devkit/core' ;
16- import { updateWorkspace } from '@schematics/angular/utility/workspace' ;
15+ import { parseJsonAst , JsonParseMode } from '@angular-devkit/core' ;
1716import {
1817 findPropertyInAstObject ,
1918 appendValueInAstArray ,
2019} from '@schematics/angular/utility/json-utils' ;
2120import { Schema as UniversalOptions } from '@schematics/angular/universal/schema' ;
22- import { stripTsExtension , getDistPaths , getProject } from '../utils' ;
21+ import { stripTsExtension , getOutputPath , getProject } from '../utils' ;
2322
2423export interface AddUniversalOptions extends UniversalOptions {
2524 serverFileName ?: string ;
@@ -35,7 +34,6 @@ export function addUniversalCommonRule(options: AddUniversalOptions): Rule {
3534 : externalSchematic ( '@schematics/angular' , 'universal' , options ) ,
3635 addScriptsRule ( options ) ,
3736 updateServerTsConfigRule ( options ) ,
38- updateConfigFileRule ( options ) ,
3937 ] ) ;
4038 } ;
4139}
@@ -48,7 +46,7 @@ function addScriptsRule(options: AddUniversalOptions): Rule {
4846 throw new SchematicsException ( 'Could not find package.json' ) ;
4947 }
5048
51- const { server : serverDist } = await getDistPaths ( host , options . clientProject ) ;
49+ const serverDist = await getOutputPath ( host , options . clientProject , 'server' ) ;
5250 const pkg = JSON . parse ( buffer . toString ( ) ) ;
5351 pkg . scripts = {
5452 ...pkg . scripts ,
@@ -60,42 +58,6 @@ function addScriptsRule(options: AddUniversalOptions): Rule {
6058 } ;
6159}
6260
63- function updateConfigFileRule ( options : AddUniversalOptions ) : Rule {
64- return host => {
65- return updateWorkspace ( ( async workspace => {
66- const clientProject = workspace . projects . get ( options . clientProject ) ;
67- if ( clientProject ) {
68- const buildTarget = clientProject . targets . get ( 'build' ) ;
69- const serverTarget = clientProject . targets . get ( 'server' ) ;
70-
71- // We have to check if the project config has a server target, because
72- // if the Universal step in this schematic isn't run, it can't be guaranteed
73- // to exist
74- if ( ! serverTarget || ! buildTarget ) {
75- return ;
76- }
77-
78- const distPaths = await getDistPaths ( host , options . clientProject ) ;
79-
80- serverTarget . options = {
81- ...serverTarget . options ,
82- outputPath : distPaths . server ,
83- } ;
84-
85- serverTarget . options . main = join (
86- normalize ( clientProject . root ) ,
87- stripTsExtension ( options . serverFileName ) + '.ts' ,
88- ) ;
89-
90- buildTarget . options = {
91- ...buildTarget . options ,
92- outputPath : distPaths . browser ,
93- } ;
94- }
95- } ) ) as unknown as Rule ;
96- } ;
97- }
98-
9961function updateServerTsConfigRule ( options : AddUniversalOptions ) : Rule {
10062 return async host => {
10163 const clientProject = await getProject ( host , options . clientProject ) ;
@@ -140,3 +102,17 @@ function updateServerTsConfigRule(options: AddUniversalOptions): Rule {
140102 }
141103 } ;
142104}
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