-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added memoizee to update checker to prevent hitting githubs api limit
- Loading branch information
1 parent
be3c3fc
commit da6b619
Showing
3 changed files
with
184 additions
and
17 deletions.
There are no files selected for viewing
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
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 }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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