This repository has been archived by the owner on Feb 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Directly lifted from Neko.js from @TehSeph
- Loading branch information
SteamingMutt
authored and
SteamingMutt
committed
Dec 4, 2015
1 parent
bbdecaa
commit a7cbb8a
Showing
4 changed files
with
112 additions
and
5 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
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 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 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
var Request = require("request"); | ||
|
||
// ======================================================================== | ||
// Version Fetching | ||
// ======================================================================== | ||
|
||
var version = require("../package.json").version.split("."); | ||
|
||
exports.getCurrentVersion = function() { return version.join("."); }; | ||
exports.getCurrentMajor = function() { return version[0]; }; | ||
exports.getCurrentMinor = function() { return version[1]; }; | ||
exports.getCurrentPatch = function() { return version[2]; }; | ||
|
||
exports.getLatestVersion = function(callback) { | ||
|
||
// fetch latest version number from GitHub | ||
Request("https://raw.githubusercontent.com/SteamingMutt/DougleyBot/master/package.json", function (error, response, body) { | ||
|
||
if (error) { return callback(error, null); } // error handle | ||
|
||
if (response.statusCode == 200) { | ||
|
||
var latest = JSON.parse(body).version.split("."); | ||
return callback(null, JSON.parse(body).version); // return version | ||
|
||
} else { // some other response code... | ||
console.log("versioncheck failed:", { response: response.statusCode }); | ||
return callback(null, "failed"); | ||
} | ||
}); | ||
}; | ||
|
||
exports.getLatestMajor = function(callback) { | ||
this.getLatest(function(err, latest) { | ||
if (err) { return callback(err, null); } // error handle | ||
return callback(null, parseInt(latest.split(".")[0])); | ||
}); | ||
}; | ||
|
||
exports.getLatestMinor = function(callback) { | ||
this.getLatest(function(err, latest) { | ||
if (err) { return callback(err, null); } // error handle | ||
return callback(null, parseInt(latest.split(".")[1])); | ||
}); | ||
}; | ||
|
||
exports.getLatestPatch = function(callback) { | ||
this.getLatest(function(err, latest) { | ||
if (err) { return callback(err, null); } // error handle | ||
return callback(null, parseInt(latest.split(".")[2])); | ||
}); | ||
}; | ||
|
||
// ======================================================================== | ||
// Version Checking | ||
// ======================================================================== | ||
|
||
exports.getStatus = function(callback) { | ||
|
||
// fetch latest version number from GitHub | ||
this.getLatestVersion(function(err, latest) { | ||
|
||
if (err) { return callback(err, null); } // error handle | ||
if (latest === "versioncheck failed") { return callback(null, latest); } // failure handle | ||
|
||
// split result into an array | ||
latest = latest.split("."); | ||
|
||
// create variables for differences | ||
var majorDiff = parseInt(latest[0]) - parseInt(version[0]); | ||
var minorDiff = parseInt(latest[1]) - parseInt(version[1]); | ||
var patchDiff = parseInt(latest[2]) - parseInt(version[2]); | ||
|
||
// check for major updates | ||
if (majorDiff < 0) { | ||
return callback(null, "Bot is " + Math.abs(majorDiff) + " major versions ahead! (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); | ||
} else if (majorDiff > 0) { | ||
return callback(null, "Bot is " + Math.abs(majorDiff) + " major versions behind. (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); | ||
} | ||
|
||
// check for minor updates | ||
if (minorDiff < 0) { | ||
return callback(null, "Bot is " + Math.abs(minorDiff) + " minor versions ahead! (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); | ||
} else if (minorDiff > 0) { | ||
return callback(null, "Bot is " + Math.abs(minorDiff) + " minor versions behind. (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); | ||
} | ||
|
||
// check for patch updates | ||
if (patchDiff < 0) { | ||
return callback(null, "Bot is " + Math.abs(patchDiff) + " patch versions ahead! (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); | ||
} else if (patchDiff > 0) { | ||
return callback(null, "Bot is " + Math.abs(patchDiff) + " patch versions behind. (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); | ||
} | ||
|
||
// up to date :) | ||
return callback(null, "Bot is fully up to date. (version: " + version.join(".") + ")"); | ||
}); | ||
}; |
a7cbb8a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enjoy my messy code! :D
a7cbb8a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks :P