node/deps/npm/lib/install/copy-tree.js
Rebecca Turner 41923c0c07 deps: upgrade npm to 3.3.6
PR-URL: https://github.com/nodejs/node/pull/3310
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
2015-10-22 13:56:09 -04:00

26 lines
625 B
JavaScript

'use strict'
module.exports = function (tree) {
return copyTree(tree, {})
}
function copyTree (tree, cache) {
if (cache[tree.path]) return cache[tree.path]
var newTree = cache[tree.path] = Object.create(tree)
copyModuleList(newTree, 'children', cache)
newTree.children.forEach(function (child) {
child.parent = newTree
})
copyModuleList(newTree, 'requires', cache)
copyModuleList(newTree, 'requiredBy', cache)
return newTree
}
function copyModuleList (tree, key, cache) {
var newList = []
tree[key].forEach(function (child) {
newList.push(copyTree(child, cache))
})
tree[key] = newList
}