| 1 |
# detective |
| 2 |
|
| 3 |
find all calls to `require()` by walking the AST |
| 4 |
|
| 5 |
[](http://travis-ci.org/browserify/detective) |
| 6 |
|
| 7 |
# example |
| 8 |
|
| 9 |
## strings |
| 10 |
|
| 11 |
strings_src.js: |
| 12 |
|
| 13 |
``` js |
| 14 |
var a = require('a'); |
| 15 |
var b = require('b'); |
| 16 |
var c = require('c'); |
| 17 |
``` |
| 18 |
|
| 19 |
strings.js: |
| 20 |
|
| 21 |
``` js |
| 22 |
var detective = require('detective'); |
| 23 |
var fs = require('fs'); |
| 24 |
|
| 25 |
var src = fs.readFileSync(__dirname + '/strings_src.js'); |
| 26 |
var requires = detective(src); |
| 27 |
console.dir(requires); |
| 28 |
``` |
| 29 |
|
| 30 |
output: |
| 31 |
|
| 32 |
``` |
| 33 |
$ node examples/strings.js |
| 34 |
[ 'a', 'b', 'c' ] |
| 35 |
``` |
| 36 |
|
| 37 |
# methods |
| 38 |
|
| 39 |
``` js |
| 40 |
var detective = require('detective'); |
| 41 |
``` |
| 42 |
|
| 43 |
## detective(src, opts) |
| 44 |
|
| 45 |
Give some source body `src`, return an array of all the `require()` calls with |
| 46 |
string arguments. |
| 47 |
|
| 48 |
The options parameter `opts` is passed along to `detective.find()`. |
| 49 |
|
| 50 |
## var found = detective.find(src, opts) |
| 51 |
|
| 52 |
Give some source body `src`, return `found` with: |
| 53 |
|
| 54 |
* `found.strings` - an array of each string found in a `require()` |
| 55 |
* `found.expressions` - an array of each stringified expression found in a |
| 56 |
`require()` call |
| 57 |
* `found.nodes` (when `opts.nodes === true`) - an array of AST nodes for each |
| 58 |
argument found in a `require()` call |
| 59 |
|
| 60 |
Optionally: |
| 61 |
|
| 62 |
* `opts.word` - specify a different function name instead of `"require"` |
| 63 |
* `opts.nodes` - when `true`, populate `found.nodes` |
| 64 |
* `opts.isRequire(node)` - a function returning whether an AST `CallExpression` |
| 65 |
node is a require call |
| 66 |
* `opts.parse` - supply options directly to |
| 67 |
[acorn](https://npmjs.org/package/acorn) with some support for esprima-style |
| 68 |
options `range` and `loc` |
| 69 |
* `opts.ecmaVersion` - default: 9 |
| 70 |
|
| 71 |
# install |
| 72 |
|
| 73 |
With [npm](https://npmjs.org) do: |
| 74 |
|
| 75 |
``` |
| 76 |
npm install detective |
| 77 |
``` |
| 78 |
|
| 79 |
# license |
| 80 |
|
| 81 |
MIT |