@@ -9,11 +9,14 @@ import { ResolvedColorSource, ResolvedSource, ResourceNodeAttribute, ResourceNod
9
9
10
10
const debug = Debug ( 'cordova-res:config' ) ;
11
11
12
- export async function run ( configPath : string , resourcesDirectory : string , sources : ReadonlyArray < ResolvedSource > , resources : ReadonlyArray < GeneratedResource > , errstream ?: NodeJS . WritableStream ) : Promise < void > {
12
+ export function getConfigPath ( directory : string ) : string {
13
+ return pathlib . resolve ( directory , 'config.xml' ) ;
14
+ }
15
+
16
+ export async function run ( configPath : string , resourcesDirectory : string , doc : et . ElementTree , sources : ReadonlyArray < ResolvedSource > , resources : ReadonlyArray < GeneratedResource > , errstream ?: NodeJS . WritableStream ) : Promise < void > {
13
17
const colors = sources . filter ( ( source ) : source is ResolvedColorSource => source . type === SourceType . COLOR ) ;
14
- const config = await read ( configPath ) ;
15
18
16
- const androidPlatformElement = resolvePlatformElement ( config . getroot ( ) , Platform . ANDROID ) ;
19
+ const androidPlatformElement = resolvePlatformElement ( doc . getroot ( ) , Platform . ANDROID ) ;
17
20
18
21
if ( colors . length > 0 ) {
19
22
debug ( 'Color sources found--generating colors document.' ) ;
@@ -31,9 +34,7 @@ export async function run(configPath: string, resourcesDirectory: string, source
31
34
resourceFileElement . set ( 'target' , '/app/src/main/res/values/colors.xml' ) ;
32
35
}
33
36
34
- runConfig ( configPath , resources , config , errstream ) ;
35
-
36
- await write ( configPath , config ) ;
37
+ runConfig ( configPath , doc , resources , errstream ) ;
37
38
}
38
39
39
40
export async function resolveColorsDocument ( colorsPath : string ) : Promise < et . ElementTree > {
@@ -69,9 +70,9 @@ export async function runColorsConfig(colorsPath: string, colors: ReadonlyArray<
69
70
await write ( colorsPath , colorsDocument ) ;
70
71
}
71
72
72
- export function runConfig ( configPath : string , resources : ReadonlyArray < GeneratedResource > , doc : et . ElementTree , errstream ?: NodeJS . WritableStream ) : void {
73
+ export function runConfig ( configPath : string , doc : et . ElementTree , resources : ReadonlyArray < GeneratedResource > , errstream ?: NodeJS . WritableStream ) : void {
73
74
const root = doc . getroot ( ) ;
74
- const orientationPreference = getPreference ( doc , 'Orientation' ) ;
75
+ const orientationPreference = getPreference ( root , 'Orientation' ) ;
75
76
debug ( 'Orientation preference: %O' , orientationPreference ) ;
76
77
77
78
const orientation = orientationPreference || 'default' ;
@@ -89,7 +90,7 @@ export function runConfig(configPath: string, resources: ReadonlyArray<Generated
89
90
filteredResources = filteredResources . filter ( img => typeof img . target === 'string' ) ;
90
91
}
91
92
for ( const resource of filteredResources ) {
92
- runResource ( configPath , resource , platformElement ) ;
93
+ runResource ( configPath , platformElement , resource ) ;
93
94
}
94
95
}
95
96
}
@@ -98,7 +99,7 @@ export function conformPath(configPath: string, value: string | number): string
98
99
return pathlib . relative ( pathlib . dirname ( configPath ) , value . toString ( ) ) . replace ( / \\ / g, '/' ) ;
99
100
}
100
101
101
- export function runResource ( configPath : string , resource : GeneratedResource , container : et . Element ) : void {
102
+ export function runResource ( configPath : string , container : et . Element , resource : GeneratedResource ) : void {
102
103
const src = resource [ resource . indexAttribute . key ] ;
103
104
104
105
if ( typeof src !== 'string' ) {
@@ -181,9 +182,15 @@ export async function write(path: string, doc: et.ElementTree): Promise<void> {
181
182
await writeFile ( path , contents , 'utf8' ) ;
182
183
}
183
184
184
- export function getPreference ( doc : et . ElementTree , name : string ) : string | undefined {
185
- const root = doc . getroot ( ) ;
186
- const preferenceElement = root . find ( `preference[@name='${ name } ']` ) ;
185
+ export function getPlatforms ( container : et . Element ) : string [ ] {
186
+ const platformElements = container . findall ( 'platform' ) ;
187
+ const platforms = platformElements . map ( el => el . get ( 'name' ) ) ;
188
+
189
+ return platforms . filter ( ( p ) : p is string => typeof p === 'string' ) ;
190
+ }
191
+
192
+ export function getPreference ( container : et . Element , name : string ) : string | undefined {
193
+ const preferenceElement = container . find ( `preference[@name='${ name } ']` ) ;
187
194
188
195
if ( ! preferenceElement ) {
189
196
return undefined ;
0 commit comments