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

Commit

Permalink
loadActual: add transplantFilter method to filter what gets transplanted
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed May 22, 2020
1 parent 9b97984 commit 5ee0d16
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/arborist/load-actual.js
Expand Up @@ -27,6 +27,7 @@ const _cache = Symbol('nodeLoadingCache')
const _loadActualVirtually = Symbol('loadActualVirtually')
const _loadActualActually = Symbol('loadActualActually')
const _transplant = Symbol('transplant')
const _transplantFilter = Symbol('transplantFilter')

const _filter = Symbol('filter')
const _global = Symbol.for('global')
Expand Down Expand Up @@ -69,8 +70,14 @@ module.exports = cls => class ActualLoader extends cls {
if (this.actualTree)
return this.actualTree

const { global = false, filter = () => true, root = null } = options
const {
global = false,
filter = () => true,
root = null,
transplantFilter = () => true,
} = options
this[_filter] = filter
this[_transplantFilter] = transplantFilter
if (global) {
const real = await realpath(this.path, this[_rpcache], this[_stcache])
const newNodeOrLink = this.path === real ? _newNode : _newLink
Expand Down Expand Up @@ -132,8 +139,8 @@ module.exports = cls => class ActualLoader extends cls {
await this[_loadFSTree](this.actualTree)
this[_findFSParents]()
this[_transplant](root)
// only reset the flags if we're not re-rooting, otherwise leave as-is
calcDepFlags(this.actualTree, root === null)
// only reset root flags if we're not re-rooting, otherwise leave as-is
calcDepFlags(this.actualTree, !root)
return this.actualTree
}

Expand All @@ -148,7 +155,8 @@ module.exports = cls => class ActualLoader extends cls {
node.fsParent = root
}
for (const node of this.actualTree.children.values()) {
node.parent = root
if (this[_transplantFilter](node))
node.parent = root
}
this.actualTree = root
}
Expand Down
23 changes: 23 additions & 0 deletions test/arborist/load-actual.js
Expand Up @@ -133,6 +133,29 @@ t.test('load a tree rooted on a different node', async t => {

// should look the same, once we strip off the other/fixture paths
t.equal(format(printTree(actual)), format(printTree(transp)), 'similar trees')

// now try with a transplant filter that keeps out the 'a' module
const rootFiltered = new Node({
meta: await Shrinkwrap.reset({path: other}),
path: other,
realpath: other,
pkg: require(path + '/package.json'),
})
rootFiltered.extraneous = false
rootFiltered.dev = false
rootFiltered.devOptional = false
rootFiltered.optional = false
rootFiltered.peer = false
const transpFilter = await new Arborist({path}).loadActual({
root: rootFiltered,
transplantFilter: n => n.name !== 'a'
})
t.equal(transpFilter.children.get('a'), undefined)
t.equal(transpFilter.children.get('b').path, resolve(other, 'node_modules/b'))
t.equal(transpFilter.children.get('c').path, resolve(other, 'node_modules/c'))
t.equal(transpFilter.children.get('a'), undefined)
t.equal(transpFilter.children.get('b').realpath, resolve(other, 'packages/b'))
t.equal(transpFilter.children.get('c').realpath, resolve(other, 'packages/c'))
})

t.test('looking outside of cwd', t => {
Expand Down

0 comments on commit 5ee0d16

Please sign in to comment.