7
7
*/
8
8
9
9
import { BuilderContext } from '@angular-devkit/architect' ;
10
- import { json } from '@angular-devkit/core' ;
11
- import fs from 'fs' ;
12
- import module from 'module' ;
13
- import os from 'os' ;
14
- import path from 'path' ;
10
+ import fs from 'node:fs' ;
11
+ import { createRequire } from 'node:module' ;
12
+ import os from 'node:os' ;
13
+ import path from 'node:path' ;
15
14
import { Schema as BrowserBuilderSchema , I18NTranslation } from '../builders/browser/schema' ;
16
15
import { Schema as ServerBuilderSchema } from '../builders/server/schema' ;
17
16
import { readTsconfig } from '../utils/read-tsconfig' ;
@@ -43,7 +42,7 @@ export interface I18nOptions {
43
42
}
44
43
45
44
function normalizeTranslationFileOption (
46
- option : json . JsonValue ,
45
+ option : unknown ,
47
46
locale : string ,
48
47
expectObjectInError : boolean ,
49
48
) : string [ ] {
@@ -65,14 +64,25 @@ function normalizeTranslationFileOption(
65
64
throw new Error ( errorMessage ) ;
66
65
}
67
66
67
+ function ensureObject ( value : unknown , name : string ) : asserts value is Record < string , unknown > {
68
+ if ( ! value || typeof value !== 'object' || Array . isArray ( value ) ) {
69
+ throw new Error ( `Project ${ name } field is malformed. Expected an object.` ) ;
70
+ }
71
+ }
72
+
73
+ function ensureString ( value : unknown , name : string ) : asserts value is string {
74
+ if ( typeof value !== 'string' ) {
75
+ throw new Error ( `Project ${ name } field is malformed. Expected a string.` ) ;
76
+ }
77
+ }
78
+
68
79
export function createI18nOptions (
69
- metadata : json . JsonObject ,
80
+ projectMetadata : { i18n ?: unknown } ,
70
81
inline ?: boolean | string [ ] ,
71
82
) : I18nOptions {
72
- if ( metadata . i18n !== undefined && ! json . isJsonObject ( metadata . i18n ) ) {
73
- throw new Error ( 'Project i18n field is malformed. Expected an object.' ) ;
74
- }
75
- metadata = metadata . i18n || { } ;
83
+ const { i18n : metadata = { } } = projectMetadata ;
84
+
85
+ ensureObject ( metadata , 'i18n' ) ;
76
86
77
87
const i18n : I18nOptions = {
78
88
inlineLocales : new Set < string > ( ) ,
@@ -86,24 +96,23 @@ export function createI18nOptions(
86
96
87
97
let rawSourceLocale ;
88
98
let rawSourceLocaleBaseHref ;
89
- if ( json . isJsonObject ( metadata . sourceLocale ) ) {
90
- rawSourceLocale = metadata . sourceLocale . code ;
91
- if (
92
- metadata . sourceLocale . baseHref !== undefined &&
93
- typeof metadata . sourceLocale . baseHref !== 'string'
94
- ) {
95
- throw new Error ( 'Project i18n sourceLocale baseHref field is malformed. Expected a string.' ) ;
96
- }
97
- rawSourceLocaleBaseHref = metadata . sourceLocale . baseHref ;
98
- } else {
99
+ if ( typeof metadata . sourceLocale === 'string' ) {
99
100
rawSourceLocale = metadata . sourceLocale ;
100
- }
101
+ } else if ( metadata . sourceLocale !== undefined ) {
102
+ ensureObject ( metadata . sourceLocale , 'i18n sourceLocale' ) ;
101
103
102
- if ( rawSourceLocale !== undefined ) {
103
- if ( typeof rawSourceLocale !== 'string' ) {
104
- throw new Error ( 'Project i18n sourceLocale field is malformed. Expected a string.' ) ;
104
+ if ( metadata . sourceLocale . code !== undefined ) {
105
+ ensureString ( metadata . sourceLocale . code , 'i18n sourceLocale code' ) ;
106
+ rawSourceLocale = metadata . sourceLocale . code ;
107
+ }
108
+
109
+ if ( metadata . sourceLocale . baseHref !== undefined ) {
110
+ ensureString ( metadata . sourceLocale . baseHref , 'i18n sourceLocale baseHref' ) ;
111
+ rawSourceLocaleBaseHref = metadata . sourceLocale . baseHref ;
105
112
}
113
+ }
106
114
115
+ if ( rawSourceLocale !== undefined ) {
107
116
i18n . sourceLocale = rawSourceLocale ;
108
117
i18n . hasDefinedSourceLocale = true ;
109
118
}
@@ -113,16 +122,17 @@ export function createI18nOptions(
113
122
baseHref : rawSourceLocaleBaseHref ,
114
123
} ;
115
124
116
- if ( metadata . locales !== undefined && ! json . isJsonObject ( metadata . locales ) ) {
117
- throw new Error ( 'Project i18n locales field is malformed. Expected an object. ') ;
118
- } else if ( metadata . locales ) {
125
+ if ( metadata . locales !== undefined ) {
126
+ ensureObject ( metadata . locales , ' i18n locales') ;
127
+
119
128
for ( const [ locale , options ] of Object . entries ( metadata . locales ) ) {
120
129
let translationFiles ;
121
130
let baseHref ;
122
- if ( json . isJsonObject ( options ) ) {
131
+ if ( options && typeof options === 'object' && 'translation' in options ) {
123
132
translationFiles = normalizeTranslationFileOption ( options . translation , locale , false ) ;
124
133
125
- if ( typeof options . baseHref === 'string' ) {
134
+ if ( 'baseHref' in options ) {
135
+ ensureString ( options . baseHref , `i18n locales ${ locale } baseHref` ) ;
126
136
baseHref = options . baseHref ;
127
137
}
128
138
} else {
@@ -181,7 +191,7 @@ export async function configureI18nBuild<T extends BrowserBuilderSchema | Server
181
191
182
192
const projectRoot = path . join ( context . workspaceRoot , ( metadata . root as string ) || '' ) ;
183
193
// The trailing slash is required to signal that the path is a directory and not a file.
184
- const projectRequire = module . createRequire ( projectRoot + '/' ) ;
194
+ const projectRequire = createRequire ( projectRoot + '/' ) ;
185
195
const localeResolver = ( locale : string ) =>
186
196
projectRequire . resolve ( path . join ( LOCALE_DATA_BASE_MODULE , locale ) ) ;
187
197
0 commit comments