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

Commit

Permalink
Implement peerDependenciesMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Aug 7, 2019
1 parent 67c9ef1 commit 5288b21
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
15 changes: 12 additions & 3 deletions lib/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const types = new Set([
'prod',
'dev',
'optional',
'peer'
'peer',
'peerOptional',
])

class Edge {
Expand All @@ -37,6 +38,14 @@ class Edge {
this[_setFrom](from)
}

get optional () {
return this[_type] === 'optional' || this[_type] === 'peerOptional'
}

get peer () {
return this[_type] === 'peer' || this[_type] === 'peerOptional'
}

get type () {
return this[_type]
}
Expand All @@ -58,8 +67,8 @@ class Edge {
return this[_error]

return this[_error] = (
!this[_to] ? (this[_type] === 'optional' ? null : 'MISSING')
: this.type === 'peer' &&
!this[_to] ? (this.optional ? null : 'MISSING')
: this.peer &&
this.from === this.to.parent &&
!this.from.isTop
? 'PEER LOCAL'
Expand Down
16 changes: 15 additions & 1 deletion lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,21 @@ class Node {
// to have a different spec for a devDep than production dep.
this[_loadDepType](this.package.optionalDependencies, 'optional')
this[_loadDepType](this.package.dependencies, 'prod')
this[_loadDepType](this.package.peerDependencies, 'peer')

const pd = this.package.peerDependencies
if (pd && typeof pd === 'object') {
const pm = this.package.peerDependenciesMeta || {}
const peerDependencies = {}
const peerOptional = {}
for (const [name, dep] of Object.entries(pd)) {
if (pm[name] && pm[name].optional)
peerOptional[name] = dep
else
peerDependencies[name] = dep
}
this[_loadDepType](peerDependencies, 'peer')
this[_loadDepType](peerOptional, 'peerOptional')
}

// Linked targets that are disconnected from the tree are tops,
// but don't have a 'path' field, only a 'realpath', because we
Expand Down
72 changes: 66 additions & 6 deletions tap-snapshots/test-node.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ Node {
"edgesIn": Set {
Edge {},
},
"edgesOut": Map {},
"edgesOut": Map {
"asdf" => Edge {},
},
"errors": Array [],
"extraneous": true,
"inBundle": false,
Expand Down Expand Up @@ -249,6 +251,14 @@ Node {
"package": Object {
"integrity": "newMeta",
"name": "meta",
"peerDependencies": Object {
"asdf": "",
},
"peerDependenciesMeta": Object {
"asdf": Object {
"optional": true,
},
},
"resolved": "newMeta",
"version": "2.3.4",
},
Expand Down Expand Up @@ -2518,7 +2528,9 @@ Node {
Edge {},
Edge {},
},
"edgesOut": Map {},
"edgesOut": Map {
"asdf" => Edge {},
},
"errors": Array [],
"extraneous": true,
"inBundle": false,
Expand Down Expand Up @@ -2572,6 +2584,14 @@ Node {
"package": Object {
"integrity": "newMeta",
"name": "meta",
"peerDependencies": Object {
"asdf": "",
},
"peerDependenciesMeta": Object {
"asdf": Object {
"optional": true,
},
},
"resolved": "newMeta",
"version": "2.3.4",
},
Expand Down Expand Up @@ -3197,7 +3217,9 @@ Node {
Edge {},
Edge {},
},
"edgesOut": Map {},
"edgesOut": Map {
"asdf" => Edge {},
},
"errors": Array [],
"extraneous": true,
"inBundle": false,
Expand Down Expand Up @@ -3251,6 +3273,14 @@ Node {
"package": Object {
"integrity": "newMeta",
"name": "meta",
"peerDependencies": Object {
"asdf": "",
},
"peerDependenciesMeta": Object {
"asdf": Object {
"optional": true,
},
},
"resolved": "newMeta",
"version": "2.3.4",
},
Expand Down Expand Up @@ -3417,7 +3447,9 @@ Node {
"edgesIn": Set {
Edge {},
},
"edgesOut": Map {},
"edgesOut": Map {
"asdf" => Edge {},
},
"errors": Array [],
"extraneous": true,
"inBundle": false,
Expand All @@ -3429,6 +3461,14 @@ Node {
"package": Object {
"integrity": "newMeta",
"name": "meta",
"peerDependencies": Object {
"asdf": "",
},
"peerDependenciesMeta": Object {
"asdf": Object {
"optional": true,
},
},
"resolved": "newMeta",
"version": "2.3.4",
},
Expand Down Expand Up @@ -4428,7 +4468,9 @@ Node {
Edge {},
Edge {},
},
"edgesOut": Map {},
"edgesOut": Map {
"asdf" => Edge {},
},
"errors": Array [],
"extraneous": true,
"inBundle": false,
Expand All @@ -4440,6 +4482,14 @@ Node {
"package": Object {
"integrity": "newMeta",
"name": "meta",
"peerDependencies": Object {
"asdf": "",
},
"peerDependenciesMeta": Object {
"asdf": Object {
"optional": true,
},
},
"resolved": "newMeta",
"version": "2.3.4",
},
Expand Down Expand Up @@ -4729,7 +4779,9 @@ Node {
Edge {},
Edge {},
},
"edgesOut": Map {},
"edgesOut": Map {
"asdf" => Edge {},
},
"errors": Array [],
"extraneous": true,
"inBundle": false,
Expand All @@ -4741,6 +4793,14 @@ Node {
"package": Object {
"integrity": "newMeta",
"name": "meta",
"peerDependencies": Object {
"asdf": "",
},
"peerDependenciesMeta": Object {
"asdf": Object {
"optional": true,
},
},
"resolved": "newMeta",
"version": "2.3.4",
},
Expand Down
2 changes: 1 addition & 1 deletion test/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ t.throws(() => new Edge({
from: top,
type: 'not a valid type',
name: 'yoinks',
}), new TypeError('invalid type: not a valid type\n(valid types are: prod, dev, optional, peer)'))
}), new TypeError('invalid type: not a valid type\n(valid types are: '))

t.throws(() => new Edge({
type: 'prod',
Expand Down
4 changes: 4 additions & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ t.test('testing with dep tree', t => {
version: '2.3.4',
resolved: 'newMeta',
integrity: 'newMeta',
peerDependencies: { asdf: '' },
peerDependenciesMeta: {
asdf: { optional: true },
},
},
path: './node_modules/prod/node_modules/meta',
realpath: '/home/user/projects/root/node_modules/prod/node_modules/meta',
Expand Down

0 comments on commit 5288b21

Please sign in to comment.