Skip to content

Instantly share code, notes, and snippets.

@gauravmuk
Created July 7, 2019 18:07
Show Gist options
  • Save gauravmuk/9f1591310ca7665f69d5322aec39fadb to your computer and use it in GitHub Desktop.
Save gauravmuk/9f1591310ca7665f69d5322aec39fadb to your computer and use it in GitHub Desktop.
Get dependencies using await
const fetch = require('node-fetch');
async function getDependencies(packageName) {
const url = `https://registry.npmjs.org/${packageName}/latest`;
try {
const response = await fetch(url);
const body = await response.json();
return Array.from(Object.keys(body.dependencies));
} catch (err) {
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