js-deps
Simple Reg Exp based dependency parser for nodejs source files that is supposed to cover 99% of the cases. It supports cyclic dependencies.
Check gulp-js-deps out to see how you can use this to launch only the unit tests affected by the change.
It can parse require statements like this:
const Promise = ;const path = ;const fse = Promise;const request = ;const fs = ;const https = ;const Throttler = ;const Throttler = ;const isStream = ;const EventEmitter2 = EventEmitter2;const got = ;const got = ;const got2=;const got2= ;const got2 =;var got2 =;let got2 =;got2 =;//got2 =require('should not be found'); spacePadded = ;// Hellovar test1 = test2 = test3 = ;
The output will be:
'bluebird' 'path' 'fs-extra' 'request' 'fs' 'https' './throttler' './throttler.js' 'is-stream' 'eventemitter2' 'got' 'got.js' 'hello1' 'hello2' 'hello3' 'hello4' 'hello5' 'hello6' 'spacePadded' 'test1' 'test2' 'test3'
But it won't match:
If there's a circular dependency, it will cut off at the moment circular dependency is detected but won't issue any errors.
- index.js
|- lib/a.js <----|
|- lib/b.js |
| |- lib/a.js --|
+- lib/c.js
It will report:
$ js-deps index.js
/Users/engin/tmp/lib/a.js
/Users/engin/tmp/lib/b.js
/Users/engin/tmp/lib/c.js
Caveats
It doesn't support:
// Double requiresvar a = ;// module exportsmoduleexports = // just requires without assignment
While it is possible to add support for these scenarios, js-deps is supposed to be used in your build toolchain for your own code and it is very easy to re-write not supported formats into supported ones.
Install
npm install js-deps
Usage
var deps = ;// To parse a source code's requires and get the dependencies as an arraydeps;// To parse a JS file and walk its requires recursively.// Returns an array of absolute paths of all requires.deps;
analyze(filePath[, options])
filePath: string
Path of the JS source file
options: object
// If false, throws an exception when cyclic dependency is detected // Default: true ignoreCyclic: true // If false, throws an exception when a missing local require is detected // Default: true ignoreMissing: true
CLI
Install via:
npm install -g js-deps
Usage
This will output local requires (starting with ./) for the source.js
js-deps source.js
See help for all arguments
js-deps -h