Skip to content

Commit 32f4fca

Browse files
authored
[FIX] Flathub release workflow (#778)
* [FIX] Flathub release workflow * chore: move to fast-xml-parser to improve format * fix: path --------- Co-authored-by: Flavio F Lima <flavioislima@users.noreply.github.com>
1 parent 435ff92 commit 32f4fca

File tree

3 files changed

+64
-34
lines changed

3 files changed

+64
-34
lines changed

flathub/update-flathub.ts

+50-25
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import * as crypto from 'crypto'
33
import * as os from 'os'
44
import * as child_process from 'child_process'
55
import axios from 'axios'
6-
import * as convert from 'xml-js'
7-
import { Element } from 'xml-js'
6+
import { XMLParser, XMLBuilder } from 'fast-xml-parser'
87

98
async function main() {
109
console.log('tag name: ', process.env.RELEASE_VERSION)
@@ -101,43 +100,69 @@ async function main() {
101100
)
102101

103102
const releaseNotesJson = JSON.parse(releaseNotesStdOut)
104-
const releaseNotesComponents = releaseNotesJson.body.split('\n')
103+
const releaseNotesComponents: string[] = releaseNotesJson.body.split('\n')
105104

106-
const hpXmlJson = convert.xml2js(hpXml, { compact: false }) as Element
107-
const releaseNotesElements: Element[] = []
105+
// XML Modification with fast-xml-parser
106+
hpXml = fs.readFileSync(xmlFilePath).toString()
107+
108+
const parserOptions = {
109+
ignoreAttributes: false,
110+
preserveOrder: true,
111+
format: true, // Enable formatting
112+
indentBy: ' ' // Use two spaces for indentation
113+
}
114+
115+
const parser = new XMLParser(parserOptions)
116+
117+
const hpXmlJson = parser.parse(hpXml)
118+
119+
const builder = new XMLBuilder(parserOptions)
120+
121+
const releaseNotesElements: Record<string, Array<Record<string, string>>>[] =
122+
[] // An array to hold generated <li> elements
108123

109124
for (const [i, releaseComponent_i] of releaseNotesComponents.entries()) {
110-
//update metainfo hpXml
111125
if (i === 0) continue
112126
if (!releaseComponent_i.startsWith('*')) continue
113127
if (releaseComponent_i.includes('http')) continue
114-
const releaseNoteElement: Element = {
115-
type: 'element',
116-
name: 'li',
117-
elements: [
118-
{
119-
type: 'text',
120-
text: releaseComponent_i.slice(1) //remove the asterisk
121-
}
128+
129+
const li = releaseComponent_i
130+
.replace(/\n/g, '')
131+
.replace(/\r/g, '')
132+
.replace(/\t/g, '')
133+
.slice(1)
134+
.trim()
135+
136+
// Creating the <li> element (compatible with fast-xml-parser)
137+
const releaseNoteElement = {
138+
li: [
139+
{ '#text': li } // Directly set the text within the <li> element
122140
]
123141
}
142+
124143
releaseNotesElements.push(releaseNoteElement)
125144
}
126145

127-
const componentsTag = hpXmlJson.elements?.[0]
128-
const releasesTag = componentsTag?.elements?.find(
129-
(val) => val.name === 'releases'
130-
) //.releases.release.description.ul =
131-
const releaseListTag =
132-
releasesTag?.elements?.[0].elements?.[0].elements?.find(
133-
(val) => val.name === 'ul'
134-
)
146+
const componentsTag = hpXmlJson[1].component
147+
// Locate the 'releases' element within the 'componentsTag'
148+
const releasesTag = componentsTag.find((val) => val.releases !== undefined)
149+
150+
// Proceed to find the <ul> element as before
151+
const releaseListTag = releasesTag?.releases[0].release[0].description[1]
152+
135153
if (releaseListTag === undefined) {
136154
throw 'releaseListTag ul undefined'
137155
}
138-
releaseListTag.elements = releaseNotesElements
139-
hpXml = convert.js2xml(hpXmlJson)
140-
fs.writeFileSync(xmlFilePath, hpXml)
156+
157+
releaseListTag.ul = [...releaseNotesElements] // Set the <ul> element to the generated <li> elements
158+
159+
console.log('new releaseListTag = ', JSON.stringify(releaseListTag, null, 2))
160+
161+
const updatedHpXml = builder.build(hpXmlJson)
162+
163+
console.log('updatedHpXml = ', updatedHpXml)
164+
165+
fs.writeFileSync(xmlFilePath, updatedHpXml)
141166

142167
console.log(
143168
'Finished updating flathub release! Please review and merge the flathub repo PR manually.'

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@
330330
"eslint-config-prettier": "^8.5.0",
331331
"eslint-plugin-import": "^2.26.0",
332332
"eslint-plugin-react": "^7.29.4",
333+
"fast-xml-parser": "^4.3.5",
333334
"husky": "^7.0.4",
334335
"i18next-parser": "^6.3.0",
335336
"jest": "^29.7.0",
@@ -345,8 +346,7 @@
345346
"typescript": "^5.3.2",
346347
"vite": "^3.2.7",
347348
"vite-plugin-electron": "^0.10.2",
348-
"vite-plugin-svgr": "^2.2.2",
349-
"xml-js": "^1.6.11"
349+
"vite-plugin-svgr": "^2.2.2"
350350
},
351351
"optionalDependencies": {},
352352
"packageManager": "yarn@1.22.19",

yarn.lock

+12-7
Original file line numberDiff line numberDiff line change
@@ -7357,6 +7357,13 @@ fast-safe-stringify@^2.0.6, fast-safe-stringify@^2.1.1:
73577357
resolved "https://registry.yarnpkg.com/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz#c406a83b6e70d9e35ce3b30a81141df30aeba884"
73587358
integrity sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==
73597359

7360+
fast-xml-parser@^4.3.5:
7361+
version "4.3.5"
7362+
resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.3.5.tgz#e2f2a2ae8377e9c3dc321b151e58f420ca7e5ccc"
7363+
integrity sha512-sWvP1Pl8H03B8oFJpFR3HE31HUfwtX7Rlf9BNsvdpujD4n7WMhfmu8h9wOV2u+c1k0ZilTADhPqypzx2J690ZQ==
7364+
dependencies:
7365+
strnum "^1.0.5"
7366+
73607367
fastq@^1.6.0:
73617368
version "1.13.0"
73627369
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.13.0.tgz#616760f88a7526bdfc596b7cab8c18938c36b98c"
@@ -12734,6 +12741,11 @@ strip-json-comments@~2.0.1:
1273412741
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
1273512742
integrity sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==
1273612743

12744+
strnum@^1.0.5:
12745+
version "1.0.5"
12746+
resolved "https://registry.yarnpkg.com/strnum/-/strnum-1.0.5.tgz#5c4e829fe15ad4ff0d20c3db5ac97b73c9b072db"
12747+
integrity sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA==
12748+
1273712749
stylis@4.0.13:
1273812750
version "4.0.13"
1273912751
resolved "https://registry.yarnpkg.com/stylis/-/stylis-4.0.13.tgz#f5db332e376d13cc84ecfe5dace9a2a51d954c91"
@@ -13891,13 +13903,6 @@ ws@~8.11.0:
1389113903
resolved "https://registry.yarnpkg.com/ws/-/ws-8.11.0.tgz#6a0d36b8edfd9f96d8b25683db2f8d7de6e8e143"
1389213904
integrity sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg==
1389313905

13894-
xml-js@^1.6.11:
13895-
version "1.6.11"
13896-
resolved "https://registry.yarnpkg.com/xml-js/-/xml-js-1.6.11.tgz#927d2f6947f7f1c19a316dd8eea3614e8b18f8e9"
13897-
integrity sha512-7rVi2KMfwfWFl+GpPg6m80IVMWXLRjO+PxTq7V2CDhoGak0wzYzFgUY2m4XJ47OGdXd8eLE8EmwfAmdjw7lC1g==
13898-
dependencies:
13899-
sax "^1.2.4"
13900-
1390113906
xml-name-validator@^4.0.0:
1390213907
version "4.0.0"
1390313908
resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-4.0.0.tgz#79a006e2e63149a8600f15430f0a4725d1524835"

0 commit comments

Comments
 (0)