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 { JsonAstObject } from '@angular-devkit/core' ;
8+ import { JsonAstObject , logging } from '@angular-devkit/core' ;
99import { Rule , Tree , UpdateRecorder } from '@angular-devkit/schematics' ;
10+ import { posix } from 'path' ;
1011import { getWorkspacePath } from '../../utility/config' ;
1112import { NodeDependencyType , addPackageJsonDependency , getPackageJsonDependency } from '../../utility/dependencies' ;
1213import {
@@ -25,23 +26,23 @@ export const ANY_COMPONENT_STYLE_BUDGET = {
2526} ;
2627
2728export function updateWorkspaceConfig ( ) : Rule {
28- return ( tree : Tree ) => {
29+ return ( tree , context ) => {
2930 const workspacePath = getWorkspacePath ( tree ) ;
3031 const workspace = getWorkspace ( tree ) ;
3132 const recorder = tree . beginUpdate ( workspacePath ) ;
3233
33- for ( const { target, project } of getTargets ( workspace , 'build' , Builders . Browser ) ) {
34+ for ( const { target } of getTargets ( workspace , 'build' , Builders . Browser ) ) {
3435 updateStyleOrScriptOption ( 'styles' , recorder , target ) ;
3536 updateStyleOrScriptOption ( 'scripts' , recorder , target ) ;
3637 addAnyComponentStyleBudget ( recorder , target ) ;
3738 updateAotOption ( tree , recorder , target ) ;
38- addBuilderI18NOptions ( recorder , target , project ) ;
39+ addBuilderI18NOptions ( recorder , target , context . logger ) ;
3940 }
4041
41- for ( const { target, project } of getTargets ( workspace , 'test' , Builders . Karma ) ) {
42+ for ( const { target } of getTargets ( workspace , 'test' , Builders . Karma ) ) {
4243 updateStyleOrScriptOption ( 'styles' , recorder , target ) ;
4344 updateStyleOrScriptOption ( 'scripts' , recorder , target ) ;
44- addBuilderI18NOptions ( recorder , target , project ) ;
45+ addBuilderI18NOptions ( recorder , target , context . logger ) ;
4546 }
4647
4748 for ( const { target } of getTargets ( workspace , 'server' , Builders . Server ) ) {
@@ -148,7 +149,11 @@ function addProjectI18NOptions(
148149 }
149150}
150151
151- function addBuilderI18NOptions ( recorder : UpdateRecorder , builderConfig : JsonAstObject , projectConfig : JsonAstObject ) {
152+ function addBuilderI18NOptions (
153+ recorder : UpdateRecorder ,
154+ builderConfig : JsonAstObject ,
155+ logger : logging . LoggerApi ,
156+ ) {
152157 const options = getAllOptions ( builderConfig ) ;
153158 const mainOptions = findPropertyInAstObject ( builderConfig , 'options' ) ;
154159 const mainBaseHref =
@@ -160,31 +165,75 @@ function addBuilderI18NOptions(recorder: UpdateRecorder, builderConfig: JsonAstO
160165
161166 for ( const option of options ) {
162167 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+
163216 if ( localeId && localeId . kind === 'string' ) {
164217 // add new localize option
165218 insertPropertyInAstObjectInOrder ( recorder , option , 'localize' , [ localeId . value ] , 12 ) ;
166219 removePropertyInAstObject ( recorder , option , 'i18nLocale' ) ;
167220 }
168221
169- const i18nFile = findPropertyInAstObject ( option , 'i18nFile' ) ;
170222 if ( i18nFile ) {
171223 removePropertyInAstObject ( recorder , option , 'i18nFile' ) ;
172224 }
173225
174- const i18nFormat = findPropertyInAstObject ( option , 'i18nFormat' ) ;
175- if ( i18nFormat ) {
176- removePropertyInAstObject ( recorder , option , 'i18nFormat' ) ;
177- }
178-
179226 // localize base HREF values are controlled by the i18n configuration
180227 const baseHref = findPropertyInAstObject ( option , 'baseHref' ) ;
181228 if ( localeId && i18nFile && baseHref ) {
182- removePropertyInAstObject ( recorder , option , 'baseHref' ) ;
183-
184229 // if the main option set has a non-default base href,
185230 // ensure that the augmented base href has the correct base value
186231 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' ) ;
188237 }
189238 }
190239 }
0 commit comments