Last active
February 5, 2021 17:16
-
-
Save msimerson/7825cb402e6da5dedf2f72e8f4ee6223 to your computer and use it in GitHub Desktop.
Get Node.js LTS versions
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
#!/usr/bin/env node | |
const fs = require('fs') | |
const path = require('path') | |
const nodeVersionData = require('node-version-data') | |
const semver = require('semver') | |
const majorsLatest = {} | |
const majorsInitial = {} | |
nodeVersionData((err, versions) => { | |
if (err) { | |
console.error('Download error') | |
console.error(err.stack) | |
process.exit(1) | |
} | |
for (const v of versions) { | |
const major = semver.major(v.version) // ex: v12, v10, ... | |
if (v.lts === false) continue // ignore all but LTS | |
// find the earliest LTS release for each major | |
if (!majorsInitial[major]) majorsInitial[major] = v | |
if (semver.lt(v.version, majorsInitial[major].version)) { | |
majorsInitial[major] = v | |
} | |
// find the largest LTS for each major | |
if (!majorsLatest[major]) majorsLatest[major] = v | |
if (semver.gt(v.version, majorsLatest[major].version)) { | |
majorsLatest[major] = v | |
} | |
} | |
printLatest() | |
// printInitial() | |
}) | |
function printLatest() { | |
console.log(`\nLatest LTS`); | |
for (const m in majorsLatest) { | |
const v = majorsLatest[m] | |
if (isOlderThanThirtyMonths(majorsInitial[m].date)) continue | |
console.log(`${v.date} ${v.version} ${v.lts}`) | |
} | |
} | |
function printInitial () { | |
console.log(`\nInitial LTS`); | |
for (const m in majorsInitial) { | |
const v = majorsInitial[m] | |
if (isOlderThanThirtyMonths(majorsInitial[m].date)) continue | |
console.log(`${v.date} ${v.version}`); | |
} | |
} | |
function isOlderThanThirtyMonths (ymd) { | |
// https://nodejs.org/en/about/releases/ says, "..for a total of 30 months" | |
const now = new Date() | |
const expire = deltaDate(new Date(ymd), 0, 30, 0); | |
if (expire.getTime() < now.getTime()) return true | |
return false; | |
} | |
function deltaDate(input, days, months, years) { | |
// https://stackoverflow.com/questions/37002681/subtract-days-months-years-from-a-date-in-javascript | |
return new Date( | |
input.getFullYear() + years, | |
input.getMonth() + months, | |
Math.min( | |
input.getDate() + days, | |
new Date(input.getFullYear() + years, input.getMonth() + months + 1, 0).getDate() | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment