summaryrefslogtreecommitdiff
path: root/deps/npm/lib/install/copy-tree.js
blob: 67a9c687a22d2596f72476a4fc18c7b80f6341da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
'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
}