diff --git a/lib/link.js b/lib/link.js index fa8293d..12babe8 100644 --- a/lib/link.js +++ b/lib/link.js @@ -3,7 +3,7 @@ const Node = require('./node.js') class Link extends Node { constructor (options) { - const { pkg, logical, physical, er, cache, realpath } = options + const { physical, cache, realpath } = options super(options) // if the target has started, but not completed, then @@ -12,9 +12,8 @@ class Link extends Node { if (cachedTarget && cachedTarget.then) cachedTarget.then(node => this.target = node) - this.target = cachedTarget || new Node({ ...options, physical: realpath }) + this.target = cachedTarget || new Node({ ...options, parent: null, physical: realpath }) this.realpath = realpath - this.error = er // convenience methods only /* istanbul ignore next */ diff --git a/lib/load-actual.js b/lib/load-actual.js index 224465c..5142327 100644 --- a/lib/load-actual.js +++ b/lib/load-actual.js @@ -21,7 +21,7 @@ const newNode = (options) => res(new Node(options)), 10)) : new Node(options) -const loadNode = ({ logical, physical, cache, rpcache, stcache }) => { +const loadNode = ({ logical, physical, cache, rpcache, stcache, parent }) => { // cache temporarily holds a promise placeholder so we // don't try to create the same node multiple times. // this is very rare to encounter, given the aggressive @@ -36,11 +36,26 @@ const loadNode = ({ logical, physical, cache, rpcache, stcache }) => { rpj(join(real, 'package.json')) .then(pkg => [pkg, null], er => [null, er]) .then(([pkg, er]) => - physical === real ? newNode({ pkg, logical, physical, er, cache }) - : new Link({ pkg, logical, physical, realpath: real, er, cache }) + physical === real ? newNode({ + pkg, + logical, + physical, + er, + cache, + parent, + }) + : new Link({ + pkg, + logical, + physical, + realpath: real, + er, + cache, + parent, + }) ), // if the realpath fails, don't bother with the rest - er => new Node({ logical, physical, er, cache })) + er => new Node({ logical, physical, er, cache, parent })) cache.set(physical, p) return p @@ -62,13 +77,15 @@ const loadChildren = (options) => { kids.filter(kid => kid.charAt(0) !== '.' && (!filterWith || filterWith(node, kid))) .map(kid => loadNode({ + parent: node, logical: join(nm, kid), physical: join(rm, kid), cache, rpcache, stcache, }))) - ).then(kidNodes => node.addChildren(kidNodes)) + ) + .then(() => node.sortChildren()) .catch(() => node) } diff --git a/lib/node.js b/lib/node.js index 4a27c69..01c3a12 100644 --- a/lib/node.js +++ b/lib/node.js @@ -7,9 +7,10 @@ const depValid = require('./dep-valid.js') let ID = 0 class Node { constructor (options) { - const { pkg, logical, physical, er, cache } = options + const { pkg, logical, physical, er, cache, parent } = options // should be impossible. const cached = cache.get(physical) + /* istanbul ignore next */ if (cached && !cached.then) throw new Error('re-creating already instantiated node') @@ -17,11 +18,12 @@ class Node { cache.set(physical, this) this[depinfoLoaded] = false - const parent = basename(dirname(logical)) - if (parent.charAt(0) === '@') - this.name = `${parent}/${basename(logical)}` + const parentDir = basename(dirname(logical)) + if (parentDir.charAt(0) === '@') + this.name = `${parentDir}/${basename(logical)}` else this.name = basename(logical) + this.path = logical this.realpath = physical this.error = er @@ -30,7 +32,10 @@ class Node { this.package = pkg || {} // physical parent dir - this.parent = null + this.parent = parent || null + if (this.parent) + this.parent.children.push(this) + this.warnings = [] // The contents of node_modules @@ -44,7 +49,9 @@ class Node { this.dependencies = new Map() this.missingPeerDeps = new Map() - this.location = '/' + const ploc = this.parent && this.parent.location + this.location = !this.parent ? '/' + : `${ploc === '/' ? '' : ploc}/${this.name}` this.extraneous = true this.dev = true @@ -114,13 +121,8 @@ class Node { return this[walk]({enter, exit, filter}, new Map(), node => node.children) } - addChildren (nodes) { - nodes.forEach(node => { - node.parent = this - const dir = this.location === '/' ? '' : this.location - node.location = `${dir}/${node.name}` - }) - this.children = nodes.sort(nodesort) + sortChildren () { + this.children = this.children.sort(nodesort) return this }