shajz commented on issue #1418:
URL: https://github.com/apache/cordova-ios/issues/1418#issuecomment-2108621678

   @Yura13 I was using xml2js https://www.npmjs.com/package/xml2js which 
doesn't preserve XML elements order. I switched to 
https://www.npmjs.com/package/fast-xml-parser.
   
   Here's the code (quick and dirty js) :
   
   ```js
   // Parse config.xml, edit app & package name and save the changes
   
   import { XMLBuilder, XMLParser } from "fast-xml-parser";
   import { readFile, writeFile } from 'fs';
   
   const configFile = "config.xml";
   
   const commonOptions = {
     preserveOrder: true,
     ignoreAttributes: false,
     allowBooleanAttributes: true,
     commentPropName: "#comment",
     unpairedTags: [
       'content',
       'access',
       'allow-navigation',
       'preference',
       'allow-intent',
       'param',
       'hook',
       'application',
       'custom-preference',
       'resource-file',
       'icon',
       'splash',
       'true',
     ],
   }
   
   const parser = new XMLParser({
     ...commonOptions,
     ignoreDeclaration: false,
   });
   
   const builder = new XMLBuilder({
     ...commonOptions,
     suppressUnpairedNode: false,
     format: true,
   });
   
   export function updateConfigFile(overrides) {
     readFile(configFile, "utf-8", (err, data) => {
       if (err) {
         throw err;
       }
   
       const result = parser.parse(data);
   
       /** Overrides */
       if (overrides?.['android-packageName']) {
         result[1][':@']['@_android-packageName'] = 
overrides['android-packageName'];
       }
   
       if (overrides?.['ios-CFBundleIdentifier']) {
         result[1][':@']['@_ios-CFBundleIdentifier'] = 
overrides['ios-CFBundleIdentifier'];
       }
   
       if (overrides?.name) {
         result[1].widget = result[1].widget.map((obj) => ((obj?.name != null) 
? {...obj, name: [ { '#text': overrides.name } ] } : obj));
       }
   
       const xml = builder.build(result);
   
       // write updated XML string to a file
       writeFile(configFile, xml, { encoding: "utf-8" }, (err) => {
         if (err) {
           throw err;
         }
   
         console.log(`Updated XML is written to a new file.`);
       });
     });
   }
   ```
   
   You can adapt the overrides part to your usecase, the syntax is weirder than 
`xml2js` but the order is preserved with the provided options :)
   
   Saved this as a .mjs file so I could use `import` syntax safely but you can 
use `require` with .js files.
   
   call it like this in your cordova hooks
   
   ```js
   updateConfigFile({
     'android-packageName': 'com.your.package',
     'ios-CFBundleIdentifier': 'com.your.package',
     name: 'Your app name',
   });
   ```
   
   Sorry I let this up without providing a solution https://xkcd.com/979/ 😅 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org
For additional commands, e-mail: issues-h...@cordova.apache.org

Reply via email to