add: getLatestVersion()

This commit is contained in:
peaceiris
2019-09-16 03:16:06 +09:00
parent e07f1d30e8
commit 457d23d414
3 changed files with 35 additions and 18 deletions

32
get-latest-version.js Normal file
View File

@@ -0,0 +1,32 @@
let getLatestVersion = function() {
return new Promise((resolve, reject) => {
// if (typeof milliseconds !== "number") {
// throw new Error("milleseconds not a number");
// }
const xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (this.response) {
const latestURL = this.response["assets"][0].browser_download_url;
console.log(latestURL);
const latestVersion = latestURL.match(/(\d+).(\d+).(\d+)/g)[0];
console.log(latestVersion);
return latestVersion;
}
}
};
xmlHttpRequest.open(
"GET",
"https://api.github.com/repos/gohugoio/hugo/releases/latest",
true
);
xmlHttpRequest.responseType = "json";
xmlHttpRequest.send(null);
resolve(version);
});
};
module.exports = getLatestVersion;