Created
September 27, 2021 18:09
-
-
Save pwndev/572938e2b3034de9c570ccc3bff9c1cf to your computer and use it in GitHub Desktop.
A JavaScript function to check for newer versions using GitHub releases.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This checks for newer releases on GitHub. If any newer versions can be found, it returns a promise resolving as the latest version tag. Otherwise the promise resolves as null. | |
* @param {String} releaseTag The release tag of the code you implement this into. | |
* @param {String} repo The repository containing the code. | |
* @returns A Promise, resolving as the version tag if the provided release tag doesn't match the one provided on GitHub. | |
*/ | |
const checkIfOutdated = (releaseTag, repo) => new Promise(async (resolve, reject) => { | |
const releases = await fetch(`https://api.github.com/repos/${repo}/releases`).then(res => res.json()); | |
if(!Array.isArray(releases)) return reject(); | |
const latestRelease = releases[0]; | |
return resolve(latestRelease.tag_name === releaseTag ? false : latestRelease.tag_name); | |
}); | |
/** | |
* A short demonstration of how you could implement this to your own project. | |
* | |
* In this example, if the latest release doesn't match v3.0, newerVersion will be the latest version tag. Otherwise it will be null. | |
*/ | |
checkIfOutdated('v3.0', 'GeneTv/jubilant-happiness').then( newerVersion => { | |
// NOTE: If during development, you provide a newer version than released on GitHub, this will also be detected as outdated. Either disable this during development or change the version tag in your code before you release the version. | |
console.log( newerVersion ? `There is a newer version available: ${newerVersion}` : 'You are up to date.') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment