8
8
import { JsonAstObject } from '@angular-devkit/core' ;
9
9
import { Rule , Tree , UpdateRecorder } from '@angular-devkit/schematics' ;
10
10
import { getWorkspacePath } from '../../utility/config' ;
11
+ import { NodeDependencyType , addPackageJsonDependency , getPackageJsonDependency } from '../../utility/dependencies' ;
11
12
import {
12
13
appendValueInAstArray ,
13
14
findPropertyInAstObject ,
14
15
insertPropertyInAstObjectInOrder ,
15
16
removePropertyInAstObject ,
16
17
} from '../../utility/json-utils' ;
18
+ import { latestVersions } from '../../utility/latest-versions' ;
17
19
import { Builders } from '../../utility/workspace-models' ;
18
20
import { getAllOptions , getProjectTarget , getTargets , getWorkspace , isIvyEnabled } from './utils' ;
19
21
@@ -28,29 +30,41 @@ export function updateWorkspaceConfig(): Rule {
28
30
const workspace = getWorkspace ( tree ) ;
29
31
const recorder = tree . beginUpdate ( workspacePath ) ;
30
32
31
- for ( const { target } of getTargets ( workspace , 'build' , Builders . Browser ) ) {
33
+ for ( const { target, project } of getTargets ( workspace , 'build' , Builders . Browser ) ) {
32
34
updateStyleOrScriptOption ( 'styles' , recorder , target ) ;
33
35
updateStyleOrScriptOption ( 'scripts' , recorder , target ) ;
34
36
addAnyComponentStyleBudget ( recorder , target ) ;
35
37
updateAotOption ( tree , recorder , target ) ;
38
+ addBuilderI18NOptions ( recorder , target , project ) ;
36
39
}
37
40
38
- for ( const { target } of getTargets ( workspace , 'test' , Builders . Karma ) ) {
41
+ for ( const { target, project } of getTargets ( workspace , 'test' , Builders . Karma ) ) {
39
42
updateStyleOrScriptOption ( 'styles' , recorder , target ) ;
40
43
updateStyleOrScriptOption ( 'scripts' , recorder , target ) ;
44
+ addBuilderI18NOptions ( recorder , target , project ) ;
41
45
}
42
46
43
47
for ( const { target } of getTargets ( workspace , 'server' , Builders . Server ) ) {
44
48
updateOptimizationOption ( recorder , target ) ;
45
49
}
46
50
51
+ for ( const { target, project } of getTargets ( workspace , 'extract-i18n' , Builders . ExtractI18n ) ) {
52
+ addProjectI18NOptions ( recorder , tree , target , project ) ;
53
+ removeExtracti18nDeprecatedOptions ( recorder , target ) ;
54
+ }
55
+
47
56
tree . commitUpdate ( recorder ) ;
48
57
49
58
return tree ;
50
59
} ;
51
60
}
52
61
53
- function addProjectI18NOptions ( recorder : UpdateRecorder , builderConfig : JsonAstObject , projectConfig : JsonAstObject ) {
62
+ function addProjectI18NOptions (
63
+ recorder : UpdateRecorder ,
64
+ tree : Tree ,
65
+ builderConfig : JsonAstObject ,
66
+ projectConfig : JsonAstObject ,
67
+ ) {
54
68
const browserConfig = getProjectTarget ( projectConfig , 'build' , Builders . Browser ) ;
55
69
if ( ! browserConfig || browserConfig . kind !== 'object' ) {
56
70
return ;
@@ -86,33 +100,101 @@ function addProjectI18NOptions(recorder: UpdateRecorder, builderConfig: JsonAstO
86
100
// Get sourceLocale from extract-i18n builder
87
101
const i18nOptions = getAllOptions ( builderConfig ) ;
88
102
const sourceLocale = i18nOptions
89
- . map ( o => {
90
- const sourceLocale = findPropertyInAstObject ( o , 'i18nLocale' ) ;
103
+ . map ( o => {
104
+ const sourceLocale = findPropertyInAstObject ( o , 'i18nLocale' ) ;
91
105
92
- return sourceLocale && sourceLocale . value ;
93
- } )
94
- . find ( x => ! ! x ) ;
106
+ return sourceLocale && sourceLocale . value ;
107
+ } )
108
+ . find ( x => ! ! x ) ;
95
109
96
110
// Add i18n project configuration
97
111
insertPropertyInAstObjectInOrder ( recorder , projectConfig , 'i18n' , {
98
112
locales,
99
113
// tslint:disable-next-line: no-any
100
114
sourceLocale : sourceLocale as any ,
101
115
} , 6 ) ;
116
+
117
+ // Add @angular /localize if not already a dependency
118
+ if ( ! getPackageJsonDependency ( tree , '@angular/localize' ) ) {
119
+ addPackageJsonDependency ( tree , {
120
+ name : '@angular/localize' ,
121
+ version : latestVersions . Angular ,
122
+ type : NodeDependencyType . Default ,
123
+ } ) ;
124
+ }
102
125
}
103
126
}
104
127
105
- function addBuilderI18NOptions ( recorder : UpdateRecorder , builderConfig : JsonAstObject ) {
128
+ function addBuilderI18NOptions ( recorder : UpdateRecorder , builderConfig : JsonAstObject , projectConfig : JsonAstObject ) {
106
129
const options = getAllOptions ( builderConfig ) ;
107
130
131
+ let hasi18n = false ;
108
132
for ( const option of options ) {
109
133
const localeId = findPropertyInAstObject ( option , 'i18nLocale' ) ;
110
- if ( ! localeId || localeId . kind !== 'string' ) {
111
- continue ;
134
+ if ( localeId && localeId . kind === 'string' ) {
135
+ // add new localize option
136
+ insertPropertyInAstObjectInOrder ( recorder , option , 'localize' , [ localeId . value ] , 12 ) ;
137
+ removePropertyInAstObject ( recorder , option , 'i18nLocale' ) ;
138
+ }
139
+
140
+ const i18nFile = findPropertyInAstObject ( option , 'i18nFile' ) ;
141
+ if ( i18nFile ) {
142
+ removePropertyInAstObject ( recorder , option , 'i18nFile' ) ;
143
+ }
144
+
145
+ const i18nFormat = findPropertyInAstObject ( option , 'i18nFormat' ) ;
146
+ if ( i18nFormat ) {
147
+ removePropertyInAstObject ( recorder , option , 'i18nFormat' ) ;
112
148
}
113
149
114
- // add new localize option
115
- insertPropertyInAstObjectInOrder ( recorder , option , 'localize' , [ localeId . value ] , 12 ) ;
150
+ hasi18n = ! ! ( hasi18n || i18nFormat || i18nFile || localeId ) ;
151
+ }
152
+
153
+ if ( hasi18n ) {
154
+ const options = findPropertyInAstObject ( builderConfig , 'options' ) ;
155
+ if ( ! options || options . kind !== 'object' ) {
156
+ return ;
157
+ }
158
+
159
+ // Don't add localize option of it's already present in the main options
160
+ if ( findPropertyInAstObject ( options , 'i18nLocale' ) || findPropertyInAstObject ( options , 'localize' ) ) {
161
+ return ;
162
+ }
163
+
164
+ // Get sourceLocale from extract-i18n builder
165
+ const extractI18nConfig = getProjectTarget ( projectConfig , 'extract-i18n' , Builders . ExtractI18n ) ;
166
+ let sourceLocale : string | undefined ;
167
+
168
+ if ( extractI18nConfig && extractI18nConfig . kind === 'object' ) {
169
+ const i18nOptions = getAllOptions ( extractI18nConfig ) ;
170
+ sourceLocale = i18nOptions
171
+ . map ( o => {
172
+ const sourceLocale = findPropertyInAstObject ( o , 'i18nLocale' ) ;
173
+
174
+ return sourceLocale && sourceLocale . value ;
175
+ } )
176
+ . find ( x => ! ! x ) as string ;
177
+ }
178
+
179
+ insertPropertyInAstObjectInOrder ( recorder , options , 'localize' , [ sourceLocale || 'en-US' ] , 12 ) ;
180
+ }
181
+ }
182
+
183
+ function removeExtracti18nDeprecatedOptions ( recorder : UpdateRecorder , builderConfig : JsonAstObject ) {
184
+ const options = getAllOptions ( builderConfig ) ;
185
+
186
+ for ( const option of options ) {
187
+ // deprecated options
188
+ removePropertyInAstObject ( recorder , option , 'i18nLocale' ) ;
189
+ const i18nFormat = option . properties . find ( ( { key } ) => key . value === 'i18nFormat' ) ;
190
+
191
+ if ( i18nFormat ) {
192
+ // i18nFormat has been changed to format
193
+ const key = i18nFormat . key ;
194
+ const offset = key . start . offset + 1 ;
195
+ recorder . remove ( offset , key . value . length ) ;
196
+ recorder . insertLeft ( offset , 'format' ) ;
197
+ }
116
198
}
117
199
}
118
200
@@ -122,7 +204,6 @@ function updateAotOption(tree: Tree, recorder: UpdateRecorder, builderConfig: Js
122
204
return ;
123
205
}
124
206
125
-
126
207
const tsConfig = findPropertyInAstObject ( options , 'tsConfig' ) ;
127
208
// Do not add aot option if the users already opted out from Ivy.
128
209
if ( tsConfig && tsConfig . kind === 'string' && ! isIvyEnabled ( tree , tsConfig . value ) ) {
0 commit comments