audiobookshelf/client/plugins/version.js

70 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-09-16 00:59:38 +02:00
import packagejson from '../package.json'
import axios from 'axios'
function parseSemver(ver) {
if (!ver) return null
var groups = ver.match(/^v((([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)$/)
if (groups && groups.length > 6) {
var total = Number(groups[3]) * 10000 + Number(groups[4]) * 100 + Number(groups[5])
2021-09-16 00:59:38 +02:00
if (isNaN(total)) {
console.warn('Invalid version total', groups[3], groups[4], groups[5])
return null
}
return {
total,
version: groups[2],
major: Number(groups[3]),
minor: Number(groups[4]),
patch: Number(groups[5]),
preRelease: groups[6] || null
}
} else {
console.warn('Invalid semver string', ver)
}
return null
}
export const currentVersion = packagejson.version
2021-09-16 00:59:38 +02:00
export async function checkForUpdate() {
if (!packagejson.version) {
return null
2021-09-16 00:59:38 +02:00
}
var currVerObj = parseSemver('v' + packagejson.version)
if (!currVerObj) {
console.error('Invalid version', packagejson.version)
return null
2021-09-16 00:59:38 +02:00
}
var largestVer = null
await axios.get(`https://api.github.com/repos/advplyr/audiobookshelf/releases`).then((res) => {
var releases = res.data
if (releases && releases.length) {
releases.forEach((release) => {
var tagName = release.tag_name
var verObj = parseSemver(tagName)
2021-09-16 00:59:38 +02:00
if (verObj) {
if (!largestVer || largestVer.total < verObj.total) {
largestVer = verObj
}
}
if (verObj.version == currVerObj.version) {
currVerObj.changelog = release.body
}
2021-09-16 00:59:38 +02:00
})
}
2021-09-16 00:59:38 +02:00
})
if (!largestVer) {
console.error('No valid version tags to compare with')
return null
2021-09-16 00:59:38 +02:00
}
2021-09-16 00:59:38 +02:00
return {
hasUpdate: largestVer.total > currVerObj.total,
latestVersion: largestVer.version,
githubTagUrl: `https://github.com/advplyr/audiobookshelf/releases/tag/v${largestVer.version}`,
currentVersion: currVerObj.version,
currentVersionChangelog: currVerObj.changelog
2021-09-16 00:59:38 +02:00
}
}