Skip to content

Commit

Permalink
feat: add support to filtering by workspaces
Browse files Browse the repository at this point in the history
Adds support to a new `workspaces` option, that accepts an array of
strings, allowing users to filter the resulting fund info to a specific
set of workspaces and their dependencies.

Relates to: npm/statusboard#301
  • Loading branch information
ruyadorno committed May 13, 2021
1 parent 4ec0c7c commit 4eaa940
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ Options:
- `countOnly`: Uses the tree-traversal logic from **npm fund** but skips over
any obj definition and just returns an obj containing `{ length }` - useful for
things such as printing a `6 packages are looking for funding` msg.
- `path`: Location to current working directory
- `workspaces`: `Array<String>` List of workspaces names to filter for,
the result will only include a subset of the resulting tree that includes
only the nodes that are children of the listed workspaces names.
- `path`, `registry` and more [Arborist](https://github.com/npm/arborist/) options.

##### <a name="fund.readTree"></a> `> fund.readTree(tree, [opts]) -> Promise<Object>`

Expand Down
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ function readTree (tree, opts) {
const { countOnly } = opts || {}
const _trailingDependencies = Symbol('trailingDependencies')

let filterSet

if (opts && opts.workspaces && opts.workspaces.length) {
const arb = new Arborist(opts)
filterSet = arb.workspaceDependencySet(tree, opts.workspaces)
}

function tracked (name, version) {
const key = String(name) + String(version)
if (seen.has(key))
Expand Down Expand Up @@ -93,6 +100,9 @@ function readTree (tree, opts) {
if (!node.package)
return empty()

if (filterSet && filterSet.size > 0 && !filterSet.has(node))
return empty()

const { name, funding, version } = node.package

// avoids duplicated items within the funding tree
Expand Down Expand Up @@ -174,8 +184,7 @@ function readTree (tree, opts) {

async function read (opts) {
const arb = new Arborist(opts)
const tree = await arb.loadActual()

const tree = await arb.loadActual(opts)
return readTree(tree, opts)
}

Expand Down
95 changes: 95 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1261,3 +1261,98 @@ t.test('invalid funding objects', (t) => {
)
t.end()
})

t.test('workspaces', t => {
t.test('filter by workspace', async t => {
const path = t.testdir({
'package.json': JSON.stringify({
name: 'root',
version: '1.0.0',
workspaces: [
'packages/*',
],
dependencies: {
'@npmcli/baz': '^1.0.0',
},
}),
packages: {
a: {
'package.json': JSON.stringify({
name: 'a',
version: '1.0.0',
funding: 'http://example.com/a',
dependencies: {
'@npmcli/foo': '^1.0.0',
},
}),
},
b: {
'package.json': JSON.stringify({
name: 'b',
version: '1.0.0',
devDependencies: {
'@npmcli/bar': '^1.0.0',
},
}),
},
},
node_modules: {
a: t.fixture('symlink', '../packages/a'),
b: t.fixture('symlink', '../packages/b'),
'@npmcli': {
foo: {
'package.json': JSON.stringify({
name: '@npmcli/foo',
version: '1.0.0',
funding: 'http://example.com/foo',
}),
},
bar: {
'package.json': JSON.stringify({
name: '@npmcli/bar',
version: '1.0.0',
funding: 'http://example.com/bar',
}),
},
baz: {
'package.json': JSON.stringify({
name: '@npmcli/baz',
version: '1.0.0',
funding: 'http://example.com/baz',
}),
},
},
},
})

const expected = {
dependencies: {
a: {
funding: {
url: 'http://example.com/a',
},
version: '1.0.0',
dependencies: {
'@npmcli/foo': {
funding: {
url: 'http://example.com/foo',
},
version: '1.0.0',
},
},
},
},
length: 2,
name: 'root',
version: '1.0.0',
}

t.same(
await read({ path, workspaces: ['a'] }),
expected,
'should filter by workspace name'
)
})

t.end()
})

0 comments on commit 4eaa940

Please sign in to comment.