5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { JsonAstObject } from '@angular-devkit/core' ;
8
+ import { JsonAstObject , logging } from '@angular-devkit/core' ;
9
9
import { Rule , Tree , UpdateRecorder } from '@angular-devkit/schematics' ;
10
+ import { posix } from 'path' ;
10
11
import { getWorkspacePath } from '../../utility/config' ;
11
12
import { NodeDependencyType , addPackageJsonDependency , getPackageJsonDependency } from '../../utility/dependencies' ;
12
13
import {
@@ -25,23 +26,23 @@ export const ANY_COMPONENT_STYLE_BUDGET = {
25
26
} ;
26
27
27
28
export function updateWorkspaceConfig ( ) : Rule {
28
- return ( tree : Tree ) => {
29
+ return ( tree , context ) => {
29
30
const workspacePath = getWorkspacePath ( tree ) ;
30
31
const workspace = getWorkspace ( tree ) ;
31
32
const recorder = tree . beginUpdate ( workspacePath ) ;
32
33
33
- for ( const { target, project } of getTargets ( workspace , 'build' , Builders . Browser ) ) {
34
+ for ( const { target } of getTargets ( workspace , 'build' , Builders . Browser ) ) {
34
35
updateStyleOrScriptOption ( 'styles' , recorder , target ) ;
35
36
updateStyleOrScriptOption ( 'scripts' , recorder , target ) ;
36
37
addAnyComponentStyleBudget ( recorder , target ) ;
37
38
updateAotOption ( tree , recorder , target ) ;
38
- addBuilderI18NOptions ( recorder , target , project ) ;
39
+ addBuilderI18NOptions ( recorder , target , context . logger ) ;
39
40
}
40
41
41
- for ( const { target, project } of getTargets ( workspace , 'test' , Builders . Karma ) ) {
42
+ for ( const { target } of getTargets ( workspace , 'test' , Builders . Karma ) ) {
42
43
updateStyleOrScriptOption ( 'styles' , recorder , target ) ;
43
44
updateStyleOrScriptOption ( 'scripts' , recorder , target ) ;
44
- addBuilderI18NOptions ( recorder , target , project ) ;
45
+ addBuilderI18NOptions ( recorder , target , context . logger ) ;
45
46
}
46
47
47
48
for ( const { target } of getTargets ( workspace , 'server' , Builders . Server ) ) {
@@ -148,7 +149,11 @@ function addProjectI18NOptions(
148
149
}
149
150
}
150
151
151
- function addBuilderI18NOptions ( recorder : UpdateRecorder , builderConfig : JsonAstObject , projectConfig : JsonAstObject ) {
152
+ function addBuilderI18NOptions (
153
+ recorder : UpdateRecorder ,
154
+ builderConfig : JsonAstObject ,
155
+ logger : logging . LoggerApi ,
156
+ ) {
152
157
const options = getAllOptions ( builderConfig ) ;
153
158
const mainOptions = findPropertyInAstObject ( builderConfig , 'options' ) ;
154
159
const mainBaseHref =
@@ -160,31 +165,75 @@ function addBuilderI18NOptions(recorder: UpdateRecorder, builderConfig: JsonAstO
160
165
161
166
for ( const option of options ) {
162
167
const localeId = findPropertyInAstObject ( option , 'i18nLocale' ) ;
168
+ const i18nFile = findPropertyInAstObject ( option , 'i18nFile' ) ;
169
+
170
+ // The format is always auto-detected now
171
+ const i18nFormat = findPropertyInAstObject ( option , 'i18nFormat' ) ;
172
+ if ( i18nFormat ) {
173
+ removePropertyInAstObject ( recorder , option , 'i18nFormat' ) ;
174
+ }
175
+
176
+ const outputPath = findPropertyInAstObject ( option , 'outputPath' ) ;
177
+ if (
178
+ localeId &&
179
+ localeId . kind === 'string' &&
180
+ i18nFile &&
181
+ outputPath &&
182
+ outputPath . kind === 'string'
183
+ ) {
184
+ // This first block was intended to remove the redundant output path field
185
+ // but due to defects in the recorder, removing the option will cause malformed json
186
+ // if (
187
+ // mainOutputPathValue &&
188
+ // outputPath.value.match(
189
+ // new RegExp(`[/\\\\]?${mainOutputPathValue}[/\\\\]${localeId.value}[/\\\\]?$`),
190
+ // )
191
+ // ) {
192
+ // removePropertyInAstObject(recorder, option, 'outputPath');
193
+ // } else
194
+ if ( outputPath . value . match ( new RegExp ( `[/\\\\]${ localeId . value } [/\\\\]?$` ) ) ) {
195
+ const newOutputPath = outputPath . value . replace (
196
+ new RegExp ( `[/\\\\]${ localeId . value } [/\\\\]?$` ) ,
197
+ '' ,
198
+ ) ;
199
+ const { start, end } = outputPath ;
200
+ recorder . remove ( start . offset , end . offset - start . offset ) ;
201
+ recorder . insertLeft ( start . offset , `"${ newOutputPath } "` ) ;
202
+ } else {
203
+ logger . warn (
204
+ `Output path value "${ outputPath . value } " for locale "${ localeId . value } " is not supported with the new localization system. ` +
205
+ `With the current value, the localized output would be written to "${ posix . join (
206
+ outputPath . value ,
207
+ localeId . value ,
208
+ ) } ". ` +
209
+ `Keeping existing options for the target configuration of locale "${ localeId . value } ".` ,
210
+ ) ;
211
+
212
+ continue ;
213
+ }
214
+ }
215
+
163
216
if ( localeId && localeId . kind === 'string' ) {
164
217
// add new localize option
165
218
insertPropertyInAstObjectInOrder ( recorder , option , 'localize' , [ localeId . value ] , 12 ) ;
166
219
removePropertyInAstObject ( recorder , option , 'i18nLocale' ) ;
167
220
}
168
221
169
- const i18nFile = findPropertyInAstObject ( option , 'i18nFile' ) ;
170
222
if ( i18nFile ) {
171
223
removePropertyInAstObject ( recorder , option , 'i18nFile' ) ;
172
224
}
173
225
174
- const i18nFormat = findPropertyInAstObject ( option , 'i18nFormat' ) ;
175
- if ( i18nFormat ) {
176
- removePropertyInAstObject ( recorder , option , 'i18nFormat' ) ;
177
- }
178
-
179
226
// localize base HREF values are controlled by the i18n configuration
180
227
const baseHref = findPropertyInAstObject ( option , 'baseHref' ) ;
181
228
if ( localeId && i18nFile && baseHref ) {
182
- removePropertyInAstObject ( recorder , option , 'baseHref' ) ;
183
-
184
229
// if the main option set has a non-default base href,
185
230
// ensure that the augmented base href has the correct base value
186
231
if ( hasMainBaseHref ) {
187
- insertPropertyInAstObjectInOrder ( recorder , option , 'baseHref' , '/' , 12 ) ;
232
+ const { start, end } = baseHref ;
233
+ recorder . remove ( start . offset , end . offset - start . offset ) ;
234
+ recorder . insertLeft ( start . offset , `"/"` ) ;
235
+ } else {
236
+ removePropertyInAstObject ( recorder , option , 'baseHref' ) ;
188
237
}
189
238
}
190
239
}
0 commit comments