Last active
November 16, 2019 12:41
-
-
Save intrnl/88127a733897c24faba2e52ebc313c8b 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
function resolveDeps (pkgs, name) { | |
const resolved = []; | |
const missingRequired = []; | |
const missingOptional = []; | |
const queue = [name]; | |
while (queue.length) { | |
const curr = queue.shift(); | |
const pkg = pkgs[curr]; | |
if (!pkg) { | |
missingRequired.push(pkg); | |
continue; | |
} | |
const deps = pkg.dependencies || {}; | |
for (let dep in deps) { | |
const depOpts = deps[dep]; | |
const necessity = depOpts.type; | |
if (!pkgs[dep]) { | |
if (necessity === 'optional') missingOptional.push(dep); | |
else missingRequired.push(dep); | |
continue; | |
} | |
queue.push(dep); | |
} | |
resolved.push(curr); | |
} | |
return { | |
resolved: [...new Set(resolved.reverse())], | |
missingRequired: [...new Set(missingRequired)], | |
missingOptional: [...new Set(missingOptional)], | |
}; | |
} | |
const pkgs = { | |
'accord': { | |
dependencies: { | |
'dot-prop': { type: 'required' }, | |
'filenamify': { type: 'required' }, | |
'got': { type: 'optional' }, | |
'write-file-atomic': { type: 'required' }, | |
'example-package': { type: 'optional' }, | |
}, | |
}, | |
'dot-prop': { | |
dependencies: { | |
'is-obj': { type: 'required' }, | |
}, | |
}, | |
'filenamify': { | |
dependencies: { | |
'example-package': { type: 'required' }, | |
}, | |
}, | |
'is-obj': { | |
dependencies: {}, | |
}, | |
'example-package': { | |
dependencies: { | |
'is-obj': { type: 'required' }, | |
'non-existant-package': { type: 'required' }, | |
}, | |
}, | |
}; | |
const ret = resolveDeps(pkgs, 'accord'); | |
console.log(ret); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment