filter-dependent
v2.2.3
Published
Filters a list of files, leaving only those which transitively dependent on any of the files from the second list
Downloads
215
Readme
Filters a list of files, leaving only those which transitively dependent on any of the files from the second list.
Why this package was written?
Because I can! But also, because I failed to find anything like that, except jest-resolve-dependencies
, which is not exactly what I need, and also have not very good algorithmic complexity (btw mine is O(n), where n is a number of nodes in a dependency graph fragment between sources and targers).
What problem it solves?
Similiar to changedSince – it allows you to find all files dependent from other files. And, therefore, skip other files.
For example, you can:
- Find all test files, which affected by git changeset, and should be running.
- Find all affected .stories.js files to build storybook only with affected components.
By doing that, you skip non-affected files and speed up your CI/build.
Features
- Supports js, jsx, ts, tsx.
- Fast.
- Resolves all symlinks to real filenames.
- Skip node_modules by default.
Example
Lets say we have four files:
a.js
depends onb.js
andc.js
b.js
depends ond.js
Then:
import filterDependent from 'filter-dependent'
const filteredFiles = filterDependent([
'./a.js',
'./b.js',
'./c.js',
'./d.js',
], [
'a.js',
'c.js',
])
// → ['/abs/path/to/a.js', '/abs/path/to/b.js', '/abs/path/to/c.js']
// because `d.js` does not depend on `c.js` nor `a.js`
API
import filterDependent from 'filter-dependent'
const filteredFiles = filterDependent(sources, targets, options)
Where
sources
– an array of file paths to be filtered.
targets
– an array of file paths to be used for filtering out sources.
options.filter
– function, which will be called on each found file (including sources and targers) to filter out some. (f: string) => f.indexOf('node_modules') === -1 && !f.endsWith('.css')
by default.
options.onMiss
– function, which will be called instead of throwing an error. Arguments are: filename and dep – absolute path of processing file and unresolved dependendy in it
note:
filter-dependent
fails on first unresolved dependency (by default).