Skip to content

Commit

Permalink
Add init code to check whether an update is available
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30 committed Jan 22, 2023
1 parent 1e90854 commit eb01cbd
Showing 1 changed file with 91 additions and 2 deletions.
93 changes: 91 additions & 2 deletions js/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Handle version related methods
*/

const VERSION = "v0.5"
const VERSION = "0.5"


function populateVersionInSettings() {
Expand All @@ -26,5 +26,94 @@ function generateVersionString(version) {
* @returns {string} - Returns a generated string to be used to show the version
* and the product name.
*/
return `startpage - ${version}`
return `startpage - v${version}`
}


async function checkIfUpdateAvailable() {
/**
* Check if a new version is available from the current version.
*
* We will essentially hit GitHub API to fetch the latest tag and
* check if that tag is higher than the current hardcoded version.
*/
latestTagAvailable = await getGithubReleaseTag();

// We need to compare the tags to see if the tag available is older than
// the current one
isUpdateAvailable = isTagHigher(latestTagAvailable, VERSION);
}


async function getGithubReleaseTag() {
/**
* Hit GitHub API to fetch the latest release tag for the repo
*/
const URL = "https://api.github.com/repos/deepjyoti30/startpage/releases/latest"

var response = await fetch(URL);
var responseJSON = await response.json();

var tagName = responseJSON.tag_name;

// NOTE: Sometimes (due to personal mistakes), the tag might contain a `v` prefix
// so we will need to remove it before returning the value.
if (tagName.charAt(0) == "v") {
tagName = tagName.slice(1, tagName.length);
}

return tagName;
}


function isTagHigher(newerTag, olderTag) {
/**
* Check if the newer tag is higher than the older tag.
*/
var newerTagSplitted = newerTag.split(".");
var olderTagSplitted = olderTag.split(".");

// We want to make both the arrays of same length and all the non
// existent digits will be 0.
if (newerTagSplitted.length != olderTagSplitted.length) {
if (newerTagSplitted.length > olderTagSplitted.length) {
// Older tag is shorter
//
// Reverse and add 0 till the lengths become same
// and then reverse it back.
var olderTagReversed = olderTagSplitted.reverse();
const shorterBy = newerTagSplitted.length - olderTagSplitted.length;

for(var i = 0; i < shorterBy; i++) {
olderTagReversed.push('0');
}

olderTagSplitted = olderTagReversed.reverse();
} else {
var newerTagReversed = newerTagSplitted.reverse();
const shorterBy = olderTagSplitted.length - newerTagSplitted.length;

for(var i = 0; i < shorterBy; i++) {
newerTagReversed.push('0');
}

newerTagSplitted = newerTagReversed.reverse();
}
}

// At this point both arrays should be of same length and we
// can easily compare them to see which one is larger.
var isNewerHigher = false;

for(var i = 0; i < newerTagSplitted.length; i++) {
newerDigit = parseInt(newerTagSplitted[i]);
olderDigit = parseInt(olderTagSplitted[i]);

if (newerDigit > olderDigit) {
isNewerHigher = true;
break;
}
}

return isNewerHigher;
}

0 comments on commit eb01cbd

Please sign in to comment.