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) {
|
2021-09-24 23:14:33 +02:00
|
|
|
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
|
|
|
|
}
|
2022-06-02 00:15:13 +02:00
|
|
|
|
|
|
|
export const currentVersion = packagejson.version
|
|
|
|
|
2021-09-16 00:59:38 +02:00
|
|
|
export async function checkForUpdate() {
|
|
|
|
if (!packagejson.version) {
|
2022-06-02 00:15:13 +02:00
|
|
|
return null
|
2021-09-16 00:59:38 +02:00
|
|
|
}
|
|
|
|
var currVerObj = parseSemver('v' + packagejson.version)
|
|
|
|
if (!currVerObj) {
|
|
|
|
console.error('Invalid version', packagejson.version)
|
2022-06-02 00:15:13 +02:00
|
|
|
return null
|
2021-09-16 00:59:38 +02:00
|
|
|
}
|
|
|
|
var largestVer = null
|
2022-04-29 19:20:51 +02:00
|
|
|
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 (!largestVer) {
|
|
|
|
console.error('No valid version tags to compare with')
|
2022-06-02 00:15:13 +02:00
|
|
|
return null
|
2021-09-16 00:59:38 +02:00
|
|
|
}
|
2022-04-29 19:20:51 +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
|
|
|
|
}
|
|
|
|
}
|