Created
August 16, 2018 01:26
-
-
Save coderek/5e6be858fcd884f4ef73a2a077fc9d5a to your computer and use it in GitHub Desktop.
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 axios = require('axios') | |
const start = new Date | |
function fetchDependencies(url) { | |
console.log('Fetching ' + url) | |
return axios.get(url).then(({data})=> { | |
return data.dependencies || [] | |
}, () => { | |
return [] | |
}).catch(() => { | |
return [] | |
}) | |
} | |
async function getAllDependencies(packageName) { | |
const dependencies = new Set() | |
const visited = new Set() | |
let started = 0 | |
let completed = 0 | |
return new Promise(resolve => { | |
function _fetch(name) { | |
const url = `http://registry.npmjs.org/${name}/latest` | |
if (visited.has(url)) { | |
return Promise.resolve() | |
} | |
visited.add(url) | |
started += 1 | |
const promise = fetchDependencies(url).then(deps => { | |
for (const key in deps) { | |
dependencies.add(key) | |
_fetch(key) | |
} | |
}).finally(() => { | |
completed += 1 | |
if (completed === started) { | |
resolve([...dependencies]) | |
} | |
}) | |
} | |
_fetch(packageName) | |
}) | |
} | |
getAllDependencies('forever').then(res => { | |
console.log(res) | |
console.log(res.length) | |
console.log((new Date - start) / 1000 + ' sec') | |
}).catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment