Created
July 7, 2019 18:04
-
-
Save gauravmuk/da34564c7c157a6b6d042dd1e4480457 to your computer and use it in GitHub Desktop.
Get Dependencies Recursive
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 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