@@ -11,11 +11,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1111} ;
1212Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
1313exports . loadTranslations = exports . configureI18nBuild = exports . createI18nOptions = void 0 ;
14- const core_1 = require ( "@angular-devkit/core" ) ;
15- const fs_1 = __importDefault ( require ( "fs" ) ) ;
16- const module_1 = __importDefault ( require ( "module" ) ) ;
17- const os_1 = __importDefault ( require ( "os" ) ) ;
18- const path_1 = __importDefault ( require ( "path" ) ) ;
14+ const node_fs_1 = __importDefault ( require ( "node:fs" ) ) ;
15+ const node_module_1 = require ( "node:module" ) ;
16+ const node_os_1 = __importDefault ( require ( "node:os" ) ) ;
17+ const node_path_1 = __importDefault ( require ( "node:path" ) ) ;
1918const schema_1 = require ( "../builders/browser/schema" ) ;
2019const read_tsconfig_1 = require ( "../utils/read-tsconfig" ) ;
2120const load_translations_1 = require ( "./load-translations" ) ;
@@ -39,11 +38,19 @@ function normalizeTranslationFileOption(option, locale, expectObjectInError) {
3938 }
4039 throw new Error ( errorMessage ) ;
4140}
42- function createI18nOptions ( metadata , inline ) {
43- if ( metadata . i18n !== undefined && ! core_1 . json . isJsonObject ( metadata . i18n ) ) {
44- throw new Error ( ' Project i18n field is malformed. Expected an object.' ) ;
41+ function ensureObject ( value , name ) {
42+ if ( ! value || typeof value !== 'object' || Array . isArray ( value ) ) {
43+ throw new Error ( ` Project ${ name } field is malformed. Expected an object.` ) ;
4544 }
46- metadata = metadata . i18n || { } ;
45+ }
46+ function ensureString ( value , name ) {
47+ if ( typeof value !== 'string' ) {
48+ throw new Error ( `Project ${ name } field is malformed. Expected a string.` ) ;
49+ }
50+ }
51+ function createI18nOptions ( projectMetadata , inline ) {
52+ const { i18n : metadata = { } } = projectMetadata ;
53+ ensureObject ( metadata , 'i18n' ) ;
4754 const i18n = {
4855 inlineLocales : new Set ( ) ,
4956 // en-US is the default locale added to Angular applications (https://angular.io/guide/i18n#i18n-pipes)
@@ -55,38 +62,37 @@ function createI18nOptions(metadata, inline) {
5562 } ;
5663 let rawSourceLocale ;
5764 let rawSourceLocaleBaseHref ;
58- if ( core_1 . json . isJsonObject ( metadata . sourceLocale ) ) {
59- rawSourceLocale = metadata . sourceLocale . code ;
60- if ( metadata . sourceLocale . baseHref !== undefined &&
61- typeof metadata . sourceLocale . baseHref !== 'string' ) {
62- throw new Error ( 'Project i18n sourceLocale baseHref field is malformed. Expected a string.' ) ;
63- }
64- rawSourceLocaleBaseHref = metadata . sourceLocale . baseHref ;
65- }
66- else {
65+ if ( typeof metadata . sourceLocale === 'string' ) {
6766 rawSourceLocale = metadata . sourceLocale ;
6867 }
69- if ( rawSourceLocale !== undefined ) {
70- if ( typeof rawSourceLocale !== 'string' ) {
71- throw new Error ( 'Project i18n sourceLocale field is malformed. Expected a string.' ) ;
68+ else if ( metadata . sourceLocale !== undefined ) {
69+ ensureObject ( metadata . sourceLocale , 'i18n sourceLocale' ) ;
70+ if ( metadata . sourceLocale . code !== undefined ) {
71+ ensureString ( metadata . sourceLocale . code , 'i18n sourceLocale code' ) ;
72+ rawSourceLocale = metadata . sourceLocale . code ;
73+ }
74+ if ( metadata . sourceLocale . baseHref !== undefined ) {
75+ ensureString ( metadata . sourceLocale . baseHref , 'i18n sourceLocale baseHref' ) ;
76+ rawSourceLocaleBaseHref = metadata . sourceLocale . baseHref ;
7277 }
78+ }
79+ if ( rawSourceLocale !== undefined ) {
7380 i18n . sourceLocale = rawSourceLocale ;
7481 i18n . hasDefinedSourceLocale = true ;
7582 }
7683 i18n . locales [ i18n . sourceLocale ] = {
7784 files : [ ] ,
7885 baseHref : rawSourceLocaleBaseHref ,
7986 } ;
80- if ( metadata . locales !== undefined && ! core_1 . json . isJsonObject ( metadata . locales ) ) {
81- throw new Error ( 'Project i18n locales field is malformed. Expected an object.' ) ;
82- }
83- else if ( metadata . locales ) {
87+ if ( metadata . locales !== undefined ) {
88+ ensureObject ( metadata . locales , 'i18n locales' ) ;
8489 for ( const [ locale , options ] of Object . entries ( metadata . locales ) ) {
8590 let translationFiles ;
8691 let baseHref ;
87- if ( core_1 . json . isJsonObject ( options ) ) {
92+ if ( options && typeof options === 'object' && 'translation' in options ) {
8893 translationFiles = normalizeTranslationFileOption ( options . translation , locale , false ) ;
89- if ( typeof options . baseHref === 'string' ) {
94+ if ( 'baseHref' in options ) {
95+ ensureString ( options . baseHref , `i18n locales ${ locale } baseHref` ) ;
9096 baseHref = options . baseHref ;
9197 }
9298 }
@@ -129,10 +135,10 @@ async function configureI18nBuild(context, options) {
129135 if ( ! i18n . shouldInline && ! i18n . hasDefinedSourceLocale ) {
130136 return { buildOptions, i18n } ;
131137 }
132- const projectRoot = path_1 . default . join ( context . workspaceRoot , metadata . root || '' ) ;
138+ const projectRoot = node_path_1 . default . join ( context . workspaceRoot , metadata . root || '' ) ;
133139 // The trailing slash is required to signal that the path is a directory and not a file.
134- const projectRequire = module_1 . default . createRequire ( projectRoot + '/' ) ;
135- const localeResolver = ( locale ) => projectRequire . resolve ( path_1 . default . join ( LOCALE_DATA_BASE_MODULE , locale ) ) ;
140+ const projectRequire = ( 0 , node_module_1 . createRequire ) ( projectRoot + '/' ) ;
141+ const localeResolver = ( locale ) => projectRequire . resolve ( node_path_1 . default . join ( LOCALE_DATA_BASE_MODULE , locale ) ) ;
136142 // Load locale data and translations (if present)
137143 let loader ;
138144 const usedFormats = new Set ( ) ;
@@ -177,11 +183,11 @@ async function configureI18nBuild(context, options) {
177183 if ( i18n . shouldInline ) {
178184 // TODO: we should likely save these in the .angular directory in the next major version.
179185 // We'd need to do a migration to add the temp directory to gitignore.
180- const tempPath = fs_1 . default . mkdtempSync ( path_1 . default . join ( fs_1 . default . realpathSync ( os_1 . default . tmpdir ( ) ) , 'angular-cli-i18n-' ) ) ;
186+ const tempPath = node_fs_1 . default . mkdtempSync ( node_path_1 . default . join ( node_fs_1 . default . realpathSync ( node_os_1 . default . tmpdir ( ) ) , 'angular-cli-i18n-' ) ) ;
181187 buildOptions . outputPath = tempPath ;
182188 process . on ( 'exit' , ( ) => {
183189 try {
184- fs_1 . default . rmSync ( tempPath , { force : true , recursive : true , maxRetries : 3 } ) ;
190+ node_fs_1 . default . rmSync ( tempPath , { force : true , recursive : true , maxRetries : 3 } ) ;
185191 }
186192 catch { }
187193 } ) ;
@@ -203,7 +209,7 @@ function findLocaleDataPath(locale, resolver) {
203209function loadTranslations ( locale , desc , workspaceRoot , loader , logger , usedFormats , duplicateTranslation ) {
204210 let translations = undefined ;
205211 for ( const file of desc . files ) {
206- const loadResult = loader ( path_1 . default . join ( workspaceRoot , file . path ) ) ;
212+ const loadResult = loader ( node_path_1 . default . join ( workspaceRoot , file . path ) ) ;
207213 for ( const diagnostics of loadResult . diagnostics . messages ) {
208214 if ( diagnostics . type === 'error' ) {
209215 logger . error ( `Error parsing translation file '${ file . path } ': ${ diagnostics . message } ` ) ;
0 commit comments