node/deps/npm/lib/install/flatten-tree.js
Rebecca Turner 507fc53e37 deps: upgrade npm to 3.3.10
PR-URL: https://github.com/nodejs/node/pull/3599
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
2015-11-02 14:25:04 -05:00

31 lines
745 B
JavaScript

'use strict'
var validate = require('aproba')
var moduleName = require('../utils/module-name.js')
module.exports = function (tree) {
validate('O', arguments)
var seen = {}
var flat = {}
var todo = [[tree, '/']]
while (todo.length) {
var next = todo.shift()
var pkg = next[0]
seen[pkg.path] = true
var path = next[1]
flat[path] = pkg
if (path !== '/') path += '/'
for (var ii = 0; ii < pkg.children.length; ++ii) {
var child = pkg.children[ii]
if (!seen[child.path]) {
todo.push([child, flatName(path, child)])
}
}
}
return flat
}
var flatName = module.exports.flatName = function (path, child) {
validate('SO', arguments)
return path + (moduleName(child) || 'TOP')
}