@@ -18,33 +18,64 @@ module.exports = function (context) {
1818 const stringsXmlString = fs . readFileSync ( stringsXmlPath , 'utf-8' ) ;
1919 const stringsXmlDoc = parser . parseFromString ( stringsXmlString , 'text/xml' )
2020
21- if ( authenticate == "true" ) {
22- // insert bool value in strings.xml
23- const booleanElements = stringsXmlDoc . getElementsByTagName ( 'bool' ) ;
24-
25- // set text for each <bool> element
26- for ( let i = 0 ; i < booleanElements . length ; i ++ ) {
27- const name = booleanElements [ i ] . getAttribute ( 'name' ) ;
28- if ( name == "migration_auth" ) {
29- booleanElements [ i ] . textContent = authenticate ;
21+ // Keys to update and their values
22+ const boolKey = "migration_auth" ;
23+ const stringKeys = {
24+ biometric_prompt_title : auth_prompt_title ,
25+ biometric_prompt_subtitle : auth_prompt_subtitle ,
26+ biometric_prompt_negative_button : auth_prompt_negative_button
27+ } ;
28+
29+ // process <bool> entry
30+ const boolElements = Array . from ( stringsXmlDoc . getElementsByTagName ( 'bool' ) ) ;
31+ const boolMatches = boolElements . filter ( el => el . getAttribute ( 'name' ) === boolKey ) ;
32+
33+ if ( authenticate == "true" ) {
34+ if ( boolMatches . length > 0 ) {
35+ // remove any duplicates beyond the first
36+ for ( let i = 1 ; i < boolMatches . length ; i ++ ) {
37+ boolMatches [ i ] . parentNode . removeChild ( boolMatches [ i ] ) ;
38+ }
39+
40+ // update first match if needed
41+ const existingBool = boolMatches [ 0 ] ;
42+ if ( existingBool . textContent !== authenticate ) {
43+ existingBool . textContent = authenticate ;
3044 }
45+ } else {
46+ // add new <bool> if it doesn't exist
47+ const newBool = stringsXmlDoc . createElement ( 'bool' ) ;
48+ newBool . setAttribute ( 'name' , boolKey ) ;
49+ newBool . textContent = authenticate ;
50+ stringsXmlDoc . documentElement . appendChild ( newBool ) ;
3151 }
3252 }
3353
34- // insert string values in strings.xml
35- const stringElements = stringsXmlDoc . getElementsByTagName ( 'string' ) ;
54+ // process < string> entries
55+ const allStrings = Array . from ( stringsXmlDoc . getElementsByTagName ( 'string' ) ) ;
3656
37- // set text for each <string> element
38- for ( let i = 0 ; i < stringElements . length ; i ++ ) {
39- const name = stringElements [ i ] . getAttribute ( 'name' ) ;
40- if ( name == "biometric_prompt_title" && auth_prompt_title != "" ) {
41- stringElements [ i ] . textContent = auth_prompt_title ;
42- }
43- else if ( name == "biometric_prompt_subtitle" && auth_prompt_subtitle != "" ) {
44- stringElements [ i ] . textContent = auth_prompt_subtitle ;
45- }
46- else if ( name == "biometric_prompt_negative_button" && auth_prompt_negative_button != "" ) {
47- stringElements [ i ] . textContent = auth_prompt_negative_button ;
57+ for ( const [ key , value ] of Object . entries ( stringKeys ) ) {
58+ if ( ! value || value . trim ( ) === "" ) continue ;
59+
60+ const matchingStrings = allStrings . filter ( el => el . getAttribute ( 'name' ) === key ) ;
61+
62+ if ( matchingStrings . length > 0 ) {
63+ // remove duplicates beyond the first
64+ for ( let i = 1 ; i < matchingStrings . length ; i ++ ) {
65+ matchingStrings [ i ] . parentNode . removeChild ( matchingStrings [ i ] ) ;
66+ }
67+
68+ // update first if needed
69+ const existingString = matchingStrings [ 0 ] ;
70+ if ( existingString . textContent !== value ) {
71+ existingString . textContent = value ;
72+ }
73+ } else {
74+ // add new <string> if it doesn't exist
75+ const newString = stringsXmlDoc . createElement ( 'string' ) ;
76+ newString . setAttribute ( 'name' , key ) ;
77+ newString . textContent = value ;
78+ stringsXmlDoc . documentElement . appendChild ( newString ) ;
4879 }
4980 }
5081
@@ -54,5 +85,4 @@ module.exports = function (context) {
5485
5586 // write the updated XML string back to the same file
5687 fs . writeFileSync ( stringsXmlPath , updatedXmlString , 'utf-8' ) ;
57-
5888} ;
0 commit comments