Skip to content

Commit

Permalink
added memoizee to update checker to prevent hitting githubs api limit
Browse files Browse the repository at this point in the history
  • Loading branch information
CyferShepard committed Dec 27, 2024
1 parent be3c3fc commit da6b619
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 17 deletions.
55 changes: 38 additions & 17 deletions backend/version-control.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,64 @@
const GitHub = require('github-api');
const packageJson = require('../package.json');
const {compareVersions} =require('compare-versions');
const GitHub = require("github-api");
const packageJson = require("../package.json");
const { compareVersions } = require("compare-versions");
const memoizee = require("memoizee");

async function checkForUpdates() {
const currentVersion = packageJson.version;
const repoOwner = 'cyfershepard';
const repoName = 'Jellystat';
const repoOwner = "cyfershepard";
const repoName = "Jellystat";
const gh = new GitHub();

let result={current_version: packageJson.version, latest_version:'', message:'', update_available:false};
let result = { current_version: packageJson.version, latest_version: "", message: "", update_available: false };

let latestVersion;

try {
const path = 'package.json';
const path = "package.json";

const response = await gh.getRepo(repoOwner, repoName).getContents('main', path);
const response = await gh.getRepo(repoOwner, repoName).getContents("main", path);
const content = response.data.content;
const decodedContent = Buffer.from(content, 'base64').toString();
const decodedContent = Buffer.from(content, "base64").toString();
latestVersion = JSON.parse(decodedContent).version;

if (compareVersions(latestVersion,currentVersion) > 0) {
if (compareVersions(latestVersion, currentVersion) > 0) {
// console.log(`A new version V.${latestVersion} of ${repoName} is available.`);
result = { current_version: packageJson.version, latest_version: latestVersion, message: `${repoName} has an update ${latestVersion}`, update_available:true };
} else if (compareVersions(latestVersion,currentVersion) < 0) {
result = {
current_version: packageJson.version,
latest_version: latestVersion,
message: `${repoName} has an update ${latestVersion}`,
update_available: true,
};
} else if (compareVersions(latestVersion, currentVersion) < 0) {
// console.log(`${repoName} is using a beta version.`);
result = { current_version: packageJson.version, latest_version: latestVersion, message: `${repoName} is using a beta version`, update_available:false };
result = {
current_version: packageJson.version,
latest_version: latestVersion,
message: `${repoName} is using a beta version`,
update_available: false,
};
} else {
// console.log(`${repoName} is up to date.`);
result = { current_version: packageJson.version, latest_version: latestVersion, message: `${repoName} is up to date`, update_available:false };
result = {
current_version: packageJson.version,
latest_version: latestVersion,
message: `${repoName} is up to date`,
update_available: false,
};
}
} catch (error) {
console.error(`Failed to fetch releases for ${repoName}: ${error.message}`);
result = { current_version: packageJson.version, latest_version: 'N/A', message: `Failed to fetch releases for ${repoName}: ${error.message}`, update_available:false };
result = {
current_version: packageJson.version,
latest_version: "N/A",
message: `Failed to fetch releases for ${repoName}: ${error.message}`,
update_available: false,
};
}

return result;
}

const memoizedCheckForUpdates = memoizee(checkForUpdates, { maxAge: 300000, promise: true });


module.exports = { checkForUpdates };
module.exports = { checkForUpdates: memoizedCheckForUpdates };
145 changes: 145 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"i18next-http-backend": "^2.4.3",
"knex": "^2.4.2",
"material-react-table": "^3.1.0",
"memoizee": "^0.4.17",
"moment": "^2.29.4",
"multer": "^1.4.5-lts.1",
"passport": "^0.6.0",
Expand Down

0 comments on commit da6b619

Please sign in to comment.