forked from nextcloud/polls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-app-version.mjs
81 lines (68 loc) · 2.32 KB
/
update-app-version.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* SPDX-FileCopyrightText: 2023 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import fs from 'fs/promises'
import { parseString, Builder } from 'xml2js'
const xmlFilePath = './appinfo/info.xml'
/**
* Asynchronously reads a file.
* @function
* @param {string} path - The path to the file.
* @param {string} encoding - The character encoding of the file.
* @return {Promise<string>} A promise that contains the file content as a string.
*/
const readFileAsync = (path, encoding) => fs.readFile(path, encoding)
/**
* Asynchronously writes a file.
* @function
* @param {string} path - The path to the file.
* @param {string} data - The data to be written to the file.
* @param {string} encoding - The character encoding of the file.
* @return {Promise<void>} A promise that is fulfilled after writing the file.
*/
const writeFileAsync = (path, data, encoding) => fs.writeFile(path, data, encoding)
/**
* Syncronizes app version with package version
* @function
* @throws {Error} If an error occurs while updating the XML file.
*/
const updateXml = async () => {
try {
// Read the current version from package.json
const packageJsonContent = await readFileAsync('./package.json', 'utf-8')
const { version: newVersion } = JSON.parse(packageJsonContent)
// Read the XML file
const data = await readFileAsync(xmlFilePath, 'utf-8')
// Parse the XML data
const result = await parseXmlAsync(data)
// Update the version in the XML (under info.version)
result.info.version = newVersion
// Build the updated XML
const xmlBuilder = new Builder()
const updatedXml = xmlBuilder.buildObject(result)
// Write the updated XML back to the file
await writeFileAsync(xmlFilePath, updatedXml, 'utf-8')
console.info(`${xmlFilePath} successfully updated.`)
} catch (error) {
throw new Error(`Error updating ${xmlFilePath}: ${error.message}`)
}
}
/**
* Asynchronously parses XML data.
* @function
* @param {string} data - The XML data as a string.
* @return {Promise<object>} A promise that contains the parsed XML as a JavaScript object.
*/
const parseXmlAsync = (data) =>
new Promise((resolve, reject) => {
parseString(data, (err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
// Perform the update
updateXml().catch((error) => console.error(error))