diff options
author | jakobst1n <jakob.stendahl@outlook.com> | 2018-09-07 00:32:51 +0200 |
---|---|---|
committer | jakobst1n <jakob.stendahl@outlook.com> | 2018-09-07 00:32:51 +0200 |
commit | 78d7c8d75a5f55ab56dd018edc85ebce9aa033bb (patch) | |
tree | 2ad9da977667b79236cfdd827d0446a5bf9fea18 /src/versionChecker | |
download | Luxcena-Neo-78d7c8d75a5f55ab56dd018edc85ebce9aa033bb.tar.gz Luxcena-Neo-78d7c8d75a5f55ab56dd018edc85ebce9aa033bb.zip |
:construction: Add pre-v1 project
Because of some stupid mistakes with the repo, I decided to delete the
git history. Create a new, fresh repo, and move all the code there.
Since all this is pre-v1, everything is in a testing-phase anyways. So i
do not think we are going to feel the need for any history. The old repo
is renamed to Luxcena-Neo-Old, and will be there until i convince
myself i won't need the history.
Diffstat (limited to 'src/versionChecker')
-rw-r--r-- | src/versionChecker/index.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/versionChecker/index.js b/src/versionChecker/index.js new file mode 100644 index 0000000..69bf848 --- /dev/null +++ b/src/versionChecker/index.js @@ -0,0 +1,58 @@ +let fs = require("fs-extra"); +let url = require("url"); +let request = require('request'); +let exec = require("child_process").exec; + +class versionChecker { + + constructor(configJson, packageJson) { + this.config = JSON.parse(fs.readFileSync(configJson)); + this.CPackageJson = JSON.parse(fs.readFileSync(packageJson)); + + this.version = this.CPackageJson["version"]; + this.repoVersion = this.version; + this.checkFrequency = this.config["checkInterval"] * 10 * 10; // takes in hours + this.repoLink = this.CPackageJson["repository"]["url"]; + this.repoBranch = this.config["branch"]; + + this.checkVersion(this); // Because we have to send the reference in our interval, we have to to it here as well + this.updateChecker = setInterval(this.checkVersion, this.checkFrequency, this); // We have to send a reference to this class + } + + checkVersion(parent) { + if (typeof(parent) == "undefined") { parent = this; } + let path = url.parse(parent.repoLink); + let link = "https://raw.githubusercontent.com" + path.pathname + '/' + parent.repoBranch + "/package.json"; + + request.get(link, (error, response, body) => { + if (!error && response.statusCode === 200) { + let remotePackageJSON = JSON.parse(body); + this.repoVersion = remotePackageJSON["version"]; + if (parent.VersionIsNewerThan(this.repoVersion, parent.version)) { + global.log("A new version is available on \"" + parent.repoBranch + "\" (v" + this.repoVersion + ")", "INFO"); + } + } else { + global.log("Could not find latest version! Please check you internet connection.", "ERROR"); + } + }); + } + + VersionIsNewerThan(check, current) { + let checkParts = check.split("."); + let currentParts = current.split("."); + let lCheckParts = checkParts.length; + let lCurrentParts = currentParts.length; + + for (let i = 0; i < lCheckParts; i++) { + if (i >= lCurrentParts) { return true; } + if (Number (checkParts[i]) > Number (currentParts[i])) { return true; } + if (Number (checkParts[i]) < Number (currentParts[i])) { return false; } + } + return false; + } + +} + +module.exports = (configJson, packageJson) => { + return new versionChecker(configJson, packageJson); +}; |