Skip to content
This repository has been archived by the owner on Jan 7, 2022. It is now read-only.

Commit

Permalink
Specify parent in arguments to Node/Link ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jul 6, 2019
1 parent 2374249 commit 49d2df1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
5 changes: 2 additions & 3 deletions lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 */
Expand Down
27 changes: 22 additions & 5 deletions lib/load-actual.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}

Expand Down
28 changes: 15 additions & 13 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ 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')

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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 49d2df1

Please sign in to comment.