diff --git a/package-lock.json b/package-lock.json index 8825b7779..6d90848fc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46,7 +46,8 @@ "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^4.2.1", "prettier": "^2.8.8", - "vue-template-compiler": "^2.7.15" + "vue-template-compiler": "^2.7.15", + "xml2js": "^0.6.2" }, "engines": { "node": "^20.0.0", @@ -14994,6 +14995,12 @@ } } }, + "node_modules/sax": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.3.0.tgz", + "integrity": "sha512-0s+oAmw9zLl1V1cS9BtZN7JAd0cW5e0QH4W3LWEK6a4LaLEA2OTpGYWDY+6XasBLtz6wkm3u1xRw95mRuJ59WA==", + "dev": true + }, "node_modules/schema-utils": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-4.2.0.tgz", @@ -17888,6 +17895,28 @@ "node": ">=12" } }, + "node_modules/xml2js": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.6.2.tgz", + "integrity": "sha512-T4rieHaC1EXcES0Kxxj4JWgaUQHDk+qwHcYOCFHfiwKz7tOVPLq7Hjq9dM1WCMhylqMEfP7hMcOIChvotiZegA==", + "dev": true, + "dependencies": { + "sax": ">=0.6.0", + "xmlbuilder": "~11.0.0" + }, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/xmlbuilder": { + "version": "11.0.1", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz", + "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==", + "dev": true, + "engines": { + "node": ">=4.0" + } + }, "node_modules/xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", diff --git a/package.json b/package.json index b4aa7c86c..f3f874f16 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "lint": "eslint --ext .js,.vue src", "lint:fix": "eslint --ext .js,.vue src --fix", "stylelint": "stylelint src/**/*{.scss,.vue,.css}", - "stylelint:fix": "stylelint src --fix" + "stylelint:fix": "stylelint src --fix", + "version": "node update-app-version.mjs && git add ./appinfo/info.xml" }, "dependencies": { "@nextcloud/auth": "^2.2.1", @@ -79,6 +80,7 @@ "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^4.2.1", "prettier": "^2.8.8", - "vue-template-compiler": "^2.7.15" + "vue-template-compiler": "^2.7.15", + "xml2js": "^0.6.2" } } diff --git a/update-app-version.mjs b/update-app-version.mjs new file mode 100644 index 000000000..db3df29b1 --- /dev/null +++ b/update-app-version.mjs @@ -0,0 +1,77 @@ +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} 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} 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} 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))