Skip to content

Instantly share code, notes, and snippets.

@gauravmuk
Created July 7, 2019 18:04
Show Gist options
  • Save gauravmuk/da34564c7c157a6b6d042dd1e4480457 to your computer and use it in GitHub Desktop.
Save gauravmuk/da34564c7c157a6b6d042dd1e4480457 to your computer and use it in GitHub Desktop.
Get Dependencies Recursive
const fetch = require('node-fetch');
function getDependencies(packageName) {
const url = `https://registry.npmjs.org/${packageName}/latest`;
return fetch(url).then((response) => {
return response.json();
}).catch(() => {
return [];
}).then((body) => {
return Array.from(Object.keys(body.dependencies));
}).catch(() => {
return [];
});
}
function getAllDependencies(packageName) {
return getDependencies(packageName).then(deps => {
return Promise.all(deps.map(dep => {
return getAllDependencies(dep).then(subDepValues => {
return [...subDepValues, ...deps];
});
})).then(values => {
return [...new Set([].concat(...values))];
});
});
}
getAllDependencies('forever').then(values => {
console.log(values);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment