diff --git a/flathub/update-flathub.ts b/flathub/update-flathub.ts index 3e41f1d6b8..6932017f63 100644 --- a/flathub/update-flathub.ts +++ b/flathub/update-flathub.ts @@ -3,8 +3,7 @@ import * as crypto from 'crypto' import * as os from 'os' import * as child_process from 'child_process' import axios from 'axios' -import * as convert from 'xml-js' -import { Element } from 'xml-js' +import { XMLParser, XMLBuilder } from 'fast-xml-parser' async function main() { console.log('tag name: ', process.env.RELEASE_VERSION) @@ -15,12 +14,12 @@ async function main() { console.log('updating url in com.heroicgameslauncher.hgl.yml') const ymlFilePath = './com.heroicgameslauncher.hgl/com.heroicgameslauncher.hgl.yml' - let hpYml = fs.readFileSync(ymlFilePath).toString() + let heroicYml = fs.readFileSync(ymlFilePath).toString() const releaseString = `https://github.com/${repoName}/releases/download/${ process.env.RELEASE_VERSION }/Heroic-${process.env.RELEASE_VERSION?.substring(1)}.AppImage` - hpYml = hpYml.replace( + heroicYml = heroicYml.replace( /https:\/\/github.com\/Heroic-Games-Launcher\/HeroicGamesLauncher\/releases\/download\/v.*..*..*\/Heroic-.*..*..*.AppImage/, releaseString ) @@ -47,9 +46,9 @@ async function main() { const sha512 = hashSum.digest('hex') fs.rmSync(outputFile) - hpYml = hpYml.replace(/sha512: [0-9, a-f]{128}/, `sha512: ${sha512}`) + heroicYml = heroicYml.replace(/sha512: [0-9, a-f]{128}/, `sha512: ${sha512}`) - fs.writeFileSync(ymlFilePath, hpYml) + fs.writeFileSync(ymlFilePath, heroicYml) // update release version and date on xml tag in com.heroicgameslauncher.hgl.metainfo.xml console.log( @@ -57,16 +56,16 @@ async function main() { ) const xmlFilePath = './com.heroicgameslauncher.hgl/com.heroicgameslauncher.hgl.metainfo.xml' - let hpXml = fs.readFileSync(xmlFilePath).toString() + let heroicXml = fs.readFileSync(xmlFilePath).toString() const date = new Date() const isoDate = date.toISOString().slice(0, 10) - hpXml = hpXml.replace( + heroicXml = heroicXml.replace( /release version="v.*..*..*" date="[0-9]{4}-[0-9]{2}-[0-9]{2}"/, `release version="${process.env.RELEASE_VERSION}" date="${isoDate}"` ) - fs.writeFileSync(xmlFilePath, hpXml) + fs.writeFileSync(xmlFilePath, heroicXml) console.log( 'Finished updating flathub release! Be sure to update release notes manually before merging.' ) @@ -101,43 +100,69 @@ async function main() { ) const releaseNotesJson = JSON.parse(releaseNotesStdOut) - const releaseNotesComponents = releaseNotesJson.body.split('\n') + const releaseNotesComponents: string[] = releaseNotesJson.body.split('\n') - const hpXmlJson = convert.xml2js(hpXml, { compact: false }) as Element - const releaseNotesElements: Element[] = [] + // XML Modification with fast-xml-parser + heroicXml = fs.readFileSync(xmlFilePath).toString() + + const parserOptions = { + ignoreAttributes: false, + preserveOrder: true, + format: true, // Enable formatting + indentBy: ' ' // Use two spaces for indentation + } + + const parser = new XMLParser(parserOptions) + + const heroicXmlJson = parser.parse(heroicXml) + + const builder = new XMLBuilder(parserOptions) + + const releaseNotesElements: Record>>[] = + [] // An array to hold generated
  • elements for (const [i, releaseComponent_i] of releaseNotesComponents.entries()) { - //update metainfo hpXml if (i === 0) continue if (!releaseComponent_i.startsWith('*')) continue if (releaseComponent_i.includes('http')) continue - const releaseNoteElement: Element = { - type: 'element', - name: 'li', - elements: [ - { - type: 'text', - text: releaseComponent_i.slice(1) //remove the asterisk - } + + const li = releaseComponent_i + .replace(/\n/g, '') + .replace(/\r/g, '') + .replace(/\t/g, '') + .slice(1) + .trim() + + // Creating the
  • element (compatible with fast-xml-parser) + const releaseNoteElement = { + li: [ + { '#text': li } // Directly set the text within the
  • element ] } + releaseNotesElements.push(releaseNoteElement) } - const componentsTag = hpXmlJson.elements?.[0] - const releasesTag = componentsTag?.elements?.find( - (val) => val.name === 'releases' - ) //.releases.release.description.ul = - const releaseListTag = - releasesTag?.elements?.[0].elements?.[0].elements?.find( - (val) => val.name === 'ul' - ) + const componentsTag = heroicXmlJson[1].component + // Locate the 'releases' element within the 'componentsTag' + const releasesTag = componentsTag.find((val) => val.releases !== undefined) + + // Proceed to find the