Last active
November 2, 2020 05:54
-
-
Save cvuorinen/738f5b4ee690c9ba068606612a1a3f91 to your computer and use it in GitHub Desktop.
Version Badge
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
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" | |
})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DEPRECATED
see https://github.com/cvuorinen/version-badge instead