Skip to content

Commit

Permalink
Merge pull request #3157 from nextcloud/dev/sync-app-version
Browse files Browse the repository at this point in the history
synchronize app version with npm version
  • Loading branch information
dartcafe authored Nov 11, 2023
2 parents 79ac628 + 3a42296 commit 700f28d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
31 changes: 30 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
77 changes: 77 additions & 0 deletions update-app-version.mjs
Original file line number Diff line number Diff line change
@@ -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<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))

0 comments on commit 700f28d

Please sign in to comment.