Skip to content

Instantly share code, notes, and snippets.

@cvuorinen
Last active November 2, 2020 05:54
Show Gist options
  • Save cvuorinen/738f5b4ee690c9ba068606612a1a3f91 to your computer and use it in GitHub Desktop.
Save cvuorinen/738f5b4ee690c9ba068606612a1a3f91 to your computer and use it in GitHub Desktop.
Version Badge
const querystring = require('querystring');
const semver = require("semver");
const dateFns = require("date-fns");
const versionDates = {
// https://nodejs.org/en/about/releases/
// https://endoflife.date/nodejs
nodejs: [
{ version: "16.x", eol: "2024-04-30" },
{ version: "15.x", eol: "2021-06-01" },
{ version: "14.x", eol: "2023-04-30" },
{ version: "13.x", eol: "2020-06-01" },
{ version: "12.x", eol: "2022-04-30" },
{ version: "10.x", eol: "2021-04-30" },
{ version: "8.x", eol: "2019-12-31" },
],
// https://www.php.net/supported-versions.php
// https://endoflife.date/php
php: [
{ version: "7.4.x", eol: "2022-11-28" },
{ version: "7.3.x", eol: "2021-12-06" },
{ version: "7.2.x", eol: "2020-11-30" },
{ version: "7.1.x", eol: "2019-12-01" },
{ version: "7.0.x", eol: "2019-01-01" },
{ version: "5.6.x", eol: "2018-12-31" },
],
};
exports.endpoint = function(request, response) {
const [ _, lang, versionRaw ] = request.url.split('/');
const version = querystring.unescape(versionRaw);
if (!lang || !versionDates[lang]) {
response.statusCode = 400;
return response.end(`Invalid lang`);
}
if (!semver.valid(semver.coerce(version))) {
response.statusCode = 400;
return response.end(`Invalid version`);
}
const v = versionDates[lang].find(v => semver.satisfies(semver.coerce(version), v.version));
console.log({ version, coerce: semver.coerce(version).toString(), v })
if (!v) {
response.statusCode = 404;
return response.end(`Version not found`);
}
// https://badgen.net/runkit
response.end(JSON.stringify({
subject: `${lang} ${version}`,
status: v.eol,
color: dateFns.isPast(dateFns.parseISO(v.eol)) ? "red" : "green"
}));
}
@cvuorinen
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment