Last active
April 14, 2023 19:29
-
-
Save jziggas/314b398efc4adf125cfe69a1f746bd3a to your computer and use it in GitHub Desktop.
Find all of the nested npm dependencies in your repo that have been end of life'd
This file contains hidden or 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
// in your repo first run `npm ls -all -json true > PATH/TO/THIS/FILE/deps.json` | |
// run `node index.js` | |
const _ = require('lodash'); | |
const fs = require('fs'); | |
const axios = require('axios'); | |
const file = fs.readFileSync('deps.json'); | |
const json = JSON.parse(file); | |
function getPropertyRecursive(obj, prop) { | |
return _.reduce(obj, function(result, value, key) { | |
if (key === prop) { | |
result.push(value); | |
} else if (_.isObjectLike(value)) { | |
return result.concat(getPropertyRecursive(value, prop)); | |
} | |
return result; | |
}, []); | |
} | |
const deps = getPropertyRecursive(json, 'dependencies')[0]; | |
const uniqResolved = _.uniq(getPropertyRecursive(json, 'resolved')); | |
const packages = uniqResolved.map(resolved => { | |
const resolvedSplit = resolved.split('/'); | |
const file = _.last(resolvedSplit); | |
// console.log('resolved', resolved); | |
// console.log('resolvedSplit', resolvedSplit); | |
// console.log('file', file); | |
const fileSplit = file.match(/-[0-9].+.tgz$/); | |
let version; | |
if (fileSplit) { | |
version = fileSplit[0]; | |
} else { | |
const pkg = Object.keys(deps).find(dep => dep.includes(file)); | |
version = deps[pkg].version; | |
} | |
const packageName = file.split(version)[0]; | |
version = version.split('.tgz')[0].split('-')[1]; | |
return [packageName, version]; | |
}); | |
const today = new Date(); | |
axios.get('https://endoflife.date/api/all.json') | |
.then(res => { | |
const matches = packages.filter(package => res.data.some(p => p === package[0])); | |
const promises = matches.map((match) => { | |
return axios.get(`https://endoflife.date/api/${match[0]}.json`) | |
.then((res) => ({ | |
pkg: match, | |
cycles: res.data | |
})); | |
}); | |
return Promise.all(promises); | |
}) | |
.then((data) => { | |
let eol = []; | |
data.forEach((datum) => { | |
datum.cycles.some((cycle) => { | |
if (cycle.eol && today > new Date(cycle.eol)) { | |
eol.push({ | |
name: datum.pkg[0], | |
currentVersion: datum.pkg[1], | |
cycle: cycle | |
}); | |
} | |
return true; | |
}); | |
}); | |
console.log('EOL', JSON.stringify(eol)); | |
}) | |
.catch(err => console.log(err)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment