55 * Use of this source code is governed by an MIT-style license that can be
66 * found in the LICENSE file at https://angular.io/license
77 */
8- import {
9- basename ,
10- experimental ,
11- join ,
12- normalize ,
13- parseJson ,
14- strings ,
15- } from '@angular-devkit/core' ;
8+ import { experimental , JsonObject , strings } from '@angular-devkit/core' ;
169import {
1710 Rule ,
1811 SchematicContext ,
@@ -21,15 +14,14 @@ import {
2114 apply ,
2215 chain ,
2316 mergeWith ,
24- move ,
2517 template ,
2618 url ,
2719 noop ,
2820 filter ,
2921 externalSchematic ,
3022} from '@angular-devkit/schematics' ;
3123import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks' ;
32- import { getWorkspace } from '@schematics/angular/utility/config' ;
24+ import { getWorkspace , getWorkspacePath } from '@schematics/angular/utility/config' ;
3325import { Schema as UniversalOptions } from './schema' ;
3426
3527
@@ -45,19 +37,6 @@ function getClientProject(
4537 return clientProject ;
4638}
4739
48- function getClientArchitect (
49- host : Tree ,
50- options : UniversalOptions ,
51- ) : experimental . workspace . WorkspaceTool {
52- const clientArchitect = getClientProject ( host , options ) . architect ;
53-
54- if ( ! clientArchitect ) {
55- throw new Error ( 'Client project architect not found.' ) ;
56- }
57-
58- return clientArchitect ;
59- }
60-
6140function addDependenciesAndScripts ( options : UniversalOptions ) : Rule {
6241 return ( host : Tree ) => {
6342
@@ -86,16 +65,44 @@ function addDependenciesAndScripts(options: UniversalOptions): Rule {
8665 } ;
8766}
8867
68+ function updateConfigFile ( options : UniversalOptions ) : Rule {
69+ return ( host : Tree ) => {
70+ const workspace = getWorkspace ( host ) ;
71+ if ( ! workspace . projects [ options . clientProject ] ) {
72+ throw new SchematicsException ( `Client app ${ options . clientProject } not found.` ) ;
73+ }
74+
75+ const clientProject = workspace . projects [ options . clientProject ] ;
76+ if ( ! clientProject . architect ) {
77+ throw new Error ( 'Client project architect not found.' ) ;
78+ }
79+
80+ const serverConfig : JsonObject = {
81+ production : {
82+ fileReplacements : [
83+ {
84+ replace : 'src/environments/environment.ts' ,
85+ with : 'src/environments/environment.prod.ts'
86+ }
87+ ]
88+ }
89+ } ;
90+ clientProject . architect . server . configurations = serverConfig ;
91+
92+ const workspacePath = getWorkspacePath ( host ) ;
93+
94+ host . overwrite ( workspacePath , JSON . stringify ( workspace , null , 2 ) ) ;
95+
96+ return host ;
97+ } ;
98+ }
99+
89100export default function ( options : UniversalOptions ) : Rule {
90101 return ( host : Tree , context : SchematicContext ) => {
91102 const clientProject = getClientProject ( host , options ) ;
92103 if ( clientProject . projectType !== 'application' ) {
93104 throw new SchematicsException ( `Universal requires a project type of "application".` ) ;
94105 }
95- const clientArchitect = getClientArchitect ( host , options ) ;
96- const tsConfigExtends = basename ( clientArchitect . build . options . tsConfig ) ;
97- const rootInSrc = clientProject . root === '' ;
98- const tsConfigDirectory = join ( normalize ( clientProject . root ) , rootInSrc ? 'src' : '' ) ;
99106
100107 if ( ! options . skipInstall ) {
101108 context . addTask ( new NodePackageInstallTask ( ) ) ;
@@ -106,15 +113,13 @@ export default function (options: UniversalOptions): Rule {
106113 template ( {
107114 ...strings ,
108115 ...options as object ,
109- stripTsExtension : ( s : string ) => { return s . replace ( / \. t s $ / , '' ) ; } ,
110- tsConfigExtends,
111- rootInSrc,
112- } ) ,
113- move ( tsConfigDirectory ) ,
116+ stripTsExtension : ( s : string ) => { return s . replace ( / \. t s $ / , '' ) ; }
117+ } )
114118 ] ) ;
115119
116120 return chain ( [
117121 externalSchematic ( '@schematics/angular' , 'universal' , options ) ,
122+ updateConfigFile ( options ) ,
118123 mergeWith ( rootSource ) ,
119124 addDependenciesAndScripts ( options ) ,
120125 ] ) ;
0 commit comments