-
Notifications
You must be signed in to change notification settings - Fork 10
/
require-analyzer.html
428 lines (355 loc) · 72.9 KB
/
require-analyzer.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<!DOCTYPE html> <html> <head> <title>require-analyzer.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> require-analyzer.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="cm">/*</span>
<span class="cm"> * require-analyzer.js: Determine dependencies for a given node.js file, directory tree, or module.</span>
<span class="cm"> *</span>
<span class="cm"> * (C) 2010, Nodejitsu Inc.</span>
<span class="cm"> *</span>
<span class="cm"> */</span>
<span class="kd">var</span> <span class="nx">util</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'util'</span><span class="p">),</span>
<span class="nx">path</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'path'</span><span class="p">),</span>
<span class="nx">fs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'fs'</span><span class="p">),</span>
<span class="nx">path</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'path'</span><span class="p">),</span>
<span class="nx">events</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'events'</span><span class="p">),</span>
<span class="nx">spawn</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'child_process'</span><span class="p">).</span><span class="nx">spawn</span><span class="p">,</span>
<span class="nx">npm</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'npm'</span><span class="p">),</span>
<span class="nx">npmout</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'npm/lib/utils/output'</span><span class="p">),</span>
<span class="nx">npmls</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'npm/lib/utils/read-installed'</span><span class="p">),</span>
<span class="nx">semver</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'semver'</span><span class="p">),</span>
<span class="nx">findit</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'findit'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">analyzer</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">,</span>
<span class="nx">_write</span> <span class="o">=</span> <span class="nx">npmout</span><span class="p">.</span><span class="nx">write</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>Create the list of <code>core</code> node.js modules for referencing later.
Map the array of all <code>core</code> modules into an object for easy access.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">core</span> <span class="o">=</span> <span class="p">{};</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">process</span><span class="p">.</span><span class="nx">binding</span><span class="p">(</span><span class="s1">'natives'</span><span class="p">)).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">mod</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">core</span><span class="p">[</span><span class="nx">mod</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="p">});</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <h3>function analyze (options, callback)</h3>
<h4>@options {Object} Options to analyze against</h4>
<h4>@callback {function} Continuation to respond to when complete.</h4>
<p>Calls the appropriate <code>require-analyzer</code> method based on the result
from <code>fs.stats()</code> on <code>options.target</code>. When dependencies are returned,
<code>npmAnalyze()</code> is called with <code>options</code> and the resulting object is
returned to <code>callback</code>. Also returns an event emitter which outputs
data at various stages of completion with events:</p>
<pre><code>dependencies: Results from .dir(), .package(), or .file()
search: Results from initial npm search in .npmAnalyze()
reduce: Results from .npmAnalyze() if options.reduce is true.
</code></pre> </td> <td class="code"> <div class="highlight"><pre><span class="nx">analyzer</span><span class="p">.</span><span class="nx">analyze</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">emitter</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">events</span><span class="p">.</span><span class="nx">EventEmitter</span><span class="p">();</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">options</span> <span class="o">||</span> <span class="o">!</span><span class="nx">options</span><span class="p">.</span><span class="nx">target</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>If there are no <code>options</code> and no <code>options.target</code> property
respond with the appropriate error.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">callback</span><span class="p">(</span><span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'options and options.target are required'</span><span class="p">));</span>
<span class="k">return</span> <span class="nx">emitter</span><span class="p">;</span>
<span class="p">}</span>
</pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>Stat the directory and call the appropriate method
on <code>analyzer</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">fs</span><span class="p">.</span><span class="nx">stat</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">target</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">stats</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">analyzeFn</span><span class="p">,</span> <span class="nx">rootDir</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">stats</span><span class="p">.</span><span class="nx">isDirectory</span><span class="p">())</span> <span class="p">{</span>
<span class="nx">analyzeFn</span> <span class="o">=</span> <span class="nx">analyzer</span><span class="p">.</span><span class="nx">dir</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">stats</span><span class="p">.</span><span class="nx">isFile</span><span class="p">())</span> <span class="p">{</span>
<span class="nx">analyzeFn</span> <span class="o">=</span> <span class="nx">analyzer</span><span class="p">.</span><span class="nx">file</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="nx">target</span> <span class="o">+</span> <span class="s1">' is not a file or a directory.'</span><span class="p">));</span>
<span class="p">}</span>
<span class="nx">analyzeFn</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">options</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">deps</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">emitter</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'childError'</span><span class="p">,</span> <span class="nx">err</span><span class="p">);</span>
<span class="p">}</span>
</pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>Emit the <code>dependencies</code> event for streaming results.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">emitter</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'dependencies'</span><span class="p">,</span> <span class="nx">deps</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">npm</span> <span class="o">===</span> <span class="kc">false</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">deps</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">npmEmitter</span> <span class="o">=</span> <span class="nx">analyzer</span><span class="p">.</span><span class="nx">npmAnalyze</span><span class="p">(</span><span class="nx">deps</span><span class="p">,</span> <span class="nx">options</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">nerr</span><span class="p">,</span> <span class="nx">reduced</span><span class="p">,</span> <span class="nx">suspect</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span> <span class="o">||</span> <span class="nx">nerr</span><span class="p">,</span> <span class="nx">reduced</span><span class="p">,</span> <span class="nx">suspect</span><span class="p">);</span>
<span class="p">});</span>
</pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>Re-emit the <code>search</code> and <code>reduce</code> events from the <code>npmEmitter</code>
for streaming results.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="p">[</span><span class="s1">'search'</span><span class="p">,</span> <span class="s1">'reduce'</span><span class="p">].</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">ev</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">npmEmitter</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="nx">ev</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">);</span>
<span class="nx">args</span><span class="p">.</span><span class="nx">unshift</span><span class="p">(</span><span class="nx">ev</span><span class="p">);</span>
<span class="nx">emitter</span><span class="p">.</span><span class="nx">emit</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">emitter</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span>
<span class="p">});</span>
<span class="p">});</span>
<span class="p">});</span>
<span class="p">});</span>
<span class="k">return</span> <span class="nx">emitter</span><span class="p">;</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <h3>function npmAnalyze (deps, options, callback)</h3>
<h4>@deps {Array} List of dependencies to analyze.</h4>
<h4>@options {Object} Set of options to analyze with.</h4>
<h4>@callback {function} Continuation to respond to when complete.</h4>
<p>Analyzes the list of dependencies using <code>npm</code>, consumes the options:</p>
<pre><code>options.reduce: Will remove deps consumed by sibling deps
</code></pre> </td> <td class="code"> <div class="highlight"><pre><span class="nx">analyzer</span><span class="p">.</span><span class="nx">npmAnalyze</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">deps</span><span class="p">,</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">emitter</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">events</span><span class="p">.</span><span class="nx">EventEmitter</span><span class="p">(),</span>
<span class="nx">pkgs</span> <span class="o">=</span> <span class="p">{};</span>
<span class="nx">analyzer</span><span class="p">.</span><span class="nx">findModulesDir</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">target</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">root</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <p>Setup npm options</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">options</span><span class="p">.</span><span class="nx">npm</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">prefix</span><span class="o">:</span> <span class="nx">root</span><span class="p">,</span>
<span class="nx">exit</span><span class="o">:</span> <span class="kc">false</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>Monkey patch <code>npmout.write()</code> so that we don't need log or out files</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">npmout</span><span class="p">.</span><span class="nx">write</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">),</span>
<span class="nx">callback</span><span class="p">;</span>
<span class="nx">args</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">arg</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">arg</span> <span class="o">===</span> <span class="s1">'function'</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span> <span class="o">=</span> <span class="nx">arg</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="nx">callback</span><span class="p">();</span>
<span class="p">};</span>
<span class="nx">npm</span><span class="p">.</span><span class="nx">load</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">npm</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">npm</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <p>Analyze dependencies by searching for all installed locally via npm.
Then see if it depends on any other dependencies that are in the
list so those dependencies may be removed (only if <code>options.reduce</code> is set).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">npmls</span><span class="p">(</span><span class="nx">root</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">result</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">result</span> <span class="o">||</span> <span class="o">!</span><span class="nx">result</span><span class="p">.</span><span class="nx">dependencies</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span>
<span class="p">}</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">result</span><span class="p">.</span><span class="nx">dependencies</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">pkg</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">deps</span> <span class="o">||</span> <span class="nx">deps</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">pkg</span><span class="p">)</span> <span class="o">!==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">pkgs</span><span class="p">[</span><span class="nx">pkg</span><span class="p">]</span> <span class="o">=</span> <span class="nx">result</span><span class="p">.</span><span class="nx">dependencies</span><span class="p">[</span><span class="nx">pkg</span><span class="p">];</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="nx">emitter</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'search'</span><span class="p">,</span> <span class="nx">pkgs</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">options</span><span class="p">.</span><span class="nx">reduce</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">npmout</span><span class="p">.</span><span class="nx">write</span> <span class="o">=</span> <span class="nx">_write</span><span class="p">;</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">pkgs</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">reduced</span> <span class="o">=</span> <span class="nx">analyzer</span><span class="p">.</span><span class="nx">merge</span><span class="p">({},</span> <span class="nx">pkgs</span><span class="p">),</span>
<span class="nx">suspect</span> <span class="o">=</span> <span class="p">{};</span>
<span class="nx">deps</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">dep</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">pkgs</span><span class="p">[</span><span class="nx">dep</span><span class="p">]</span> <span class="o">&&</span> <span class="nx">pkgs</span><span class="p">[</span><span class="nx">dep</span><span class="p">].</span><span class="nx">dependencies</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">pkgs</span><span class="p">[</span><span class="nx">dep</span><span class="p">].</span><span class="nx">dependencies</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">cdep</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">reduced</span><span class="p">[</span><span class="nx">cdep</span><span class="p">])</span> <span class="p">{</span>
<span class="nx">suspect</span><span class="p">[</span><span class="nx">cdep</span><span class="p">]</span> <span class="o">=</span> <span class="nx">pkgs</span><span class="p">[</span><span class="nx">cdep</span><span class="p">];</span>
<span class="k">delete</span> <span class="nx">reduced</span><span class="p">[</span><span class="nx">cdep</span><span class="p">];</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="nx">emitter</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'reduce'</span><span class="p">,</span> <span class="nx">reduced</span><span class="p">,</span> <span class="nx">suspect</span><span class="p">);</span>
<span class="nx">npmout</span><span class="p">.</span><span class="nx">write</span> <span class="o">=</span> <span class="nx">_write</span><span class="p">;</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">reduced</span><span class="p">,</span> <span class="nx">suspect</span><span class="p">);</span>
<span class="p">});</span>
<span class="p">});</span>
<span class="p">});</span>
<span class="k">return</span> <span class="nx">emitter</span><span class="p">;</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <h3>function package (dir, callback)</h3>
<h4>@dir {string} Parent directory to analyze</h4>
<h4>@callback {function} Continuation to respond to when complete.</h4>
<p>Checks for the existance of a package.json in the specified <code>dir</code>
running <code>analyzer.package()</code> if it exists. Otherwise attempts to run
<code>analyzer.file()</code> on all files in the source tree.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">analyzer</span><span class="p">.</span><span class="nx">dir</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span> </pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>Read the target directory </p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">fs</span><span class="p">.</span><span class="nx">readdir</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">target</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">files</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
<span class="p">}</span>
</pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <p>If there is a package.json in the directory
then analyze the require(s) based on <code>package.main</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">files</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">'package.json'</span><span class="p">)</span> <span class="o">!==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">analyzer</span><span class="p">.</span><span class="kr">package</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">callback</span><span class="p">);</span>
<span class="p">}</span>
</pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p>Otherwise find all files in the directory tree
and attempt to run <code>analyzer.file()</code> on each of them
in parallel.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">files</span> <span class="o">=</span> <span class="p">[],</span>
<span class="nx">done</span> <span class="o">=</span> <span class="p">[],</span>
<span class="nx">packages</span> <span class="o">=</span> <span class="p">{},</span>
<span class="nx">traversed</span> <span class="o">=</span> <span class="kc">false</span><span class="p">,</span>
<span class="nx">finder</span> <span class="o">=</span> <span class="nx">findit</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">target</span><span class="p">);</span>
<span class="kd">function</span> <span class="nx">onRequired</span> <span class="p">()</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <p>Respond to the <code>callback</code> if all files have been traversed
and all files have been executed via <code>analyzer.file()</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">traversed</span> <span class="o">&&</span> <span class="nx">files</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="nx">done</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">packages</span><span class="p">));</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nx">finder</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'file'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">file</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div> <p>If the file is not <code>.js</code> or <code>.coffee</code> do no analyze it</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">ext</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">extname</span><span class="p">(</span><span class="nx">file</span><span class="p">),</span>
<span class="nx">clone</span> <span class="o">=</span> <span class="nx">analyzer</span><span class="p">.</span><span class="nx">merge</span><span class="p">({},</span> <span class="nx">options</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">ext</span> <span class="o">!==</span> <span class="s1">'.js'</span> <span class="o">&&</span> <span class="nx">ext</span> <span class="o">!==</span> <span class="s1">'.coffee'</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">files</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">file</span><span class="p">);</span>
<span class="nx">clone</span><span class="p">.</span><span class="nx">target</span> <span class="o">=</span> <span class="nx">file</span><span class="p">;</span>
<span class="nx">analyzer</span><span class="p">.</span><span class="nx">file</span><span class="p">(</span><span class="nx">clone</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">deps</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">deps</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">dep</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">packages</span><span class="p">[</span><span class="nx">dep</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="p">});</span>
<span class="nx">done</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">file</span><span class="p">);</span>
<span class="nx">onRequired</span><span class="p">();</span>
<span class="p">});</span>
<span class="p">});</span>
<span class="nx">finder</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'end'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="nx">traversed</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="nx">onRequired</span><span class="p">();</span>
<span class="p">});</span>
<span class="p">});</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <h3>function package (dir, callback)</h3>
<h4>@dir {string} Parent path of the package.json to analyze</h4>
<h4>@callback {function} Continuation to respond to when complete.</h4>
<p>Attempts to read the package.json in the specified <code>dir</code> and then analyzes
the require statements in the script located at <code>package.main</code></p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">analyzer</span><span class="p">.</span><span class="kr">package</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div> <p>Attempt to read the package.json in the current directory </p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">fs</span><span class="p">.</span><span class="nx">readFile</span><span class="p">(</span><span class="nx">path</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">target</span><span class="p">,</span> <span class="s1">'package.json'</span><span class="p">),</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">pkg</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">try</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</a> </div> <p>Attempt to read the package.json data.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">pkg</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">pkg</span><span class="p">.</span><span class="nx">toString</span><span class="p">());</span>
</pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">¶</a> </div> <p>TODO (indexzero): Support more than <code>main</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">pkg</span><span class="p">.</span><span class="nx">main</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'package.json must have a `main` property.'</span><span class="p">));</span>
<span class="p">}</span>
</pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <p>Analyze the require(s) based on the <code>main</code> property of the package.json</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">options</span> <span class="o">=</span> <span class="nx">analyzer</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">options</span><span class="p">);</span>
<span class="nx">options</span><span class="p">.</span><span class="nx">target</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">target</span><span class="p">,</span> <span class="nx">path</span><span class="p">.</span><span class="nx">normalize</span><span class="p">(</span><span class="nx">pkg</span><span class="p">.</span><span class="nx">main</span><span class="p">));</span>
<span class="nx">analyzer</span><span class="p">.</span><span class="nx">file</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">deps</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">deps</span> <span class="o">=</span> <span class="nx">deps</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">d</span> <span class="o">!==</span> <span class="nx">pkg</span><span class="p">.</span><span class="nx">name</span> <span class="p">});</span>
<span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">deps</span><span class="p">);</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="k">catch</span> <span class="p">(</span><span class="nx">ex</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">ex</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <h3>function file (file, callback)</h3>
<h4>@file {string} Path of the node script to analyze</h4>
<h4>@callback {callback} Continuation to respond to when complete.</h4>
<p>Attempts to find the packages required by the node script located at
<code>file</code> by spawning an instance of the <code>find-dependencies</code> helper
script and parsing the output.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">analyzer</span><span class="p">.</span><span class="nx">file</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">¶</a> </div> <p>Spawn the <code>find-dependencies</code> bin helper to ensure that we are able to
bypass any modules which have already been required in the current process. </p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">packages</span> <span class="o">=</span> <span class="p">{},</span>
<span class="nx">merged</span> <span class="o">=</span> <span class="p">{},</span>
<span class="nx">errs</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'Errors received when analyzing '</span> <span class="o">+</span> <span class="nx">options</span><span class="p">.</span><span class="nx">target</span><span class="p">],</span>
<span class="nx">deps</span> <span class="o">=</span> <span class="nx">spawn</span><span class="p">(</span><span class="s1">'node'</span><span class="p">,</span> <span class="p">[</span><span class="nx">path</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">__dirname</span><span class="p">,</span> <span class="s1">'..'</span><span class="p">,</span> <span class="s1">'bin'</span><span class="p">,</span> <span class="s1">'find-dependencies'</span><span class="p">),</span> <span class="nx">options</span><span class="p">.</span><span class="nx">target</span><span class="p">]);</span>
<span class="kd">function</span> <span class="nx">parseLines</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="nx">prefix</span><span class="p">,</span> <span class="nx">fn</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">data</span> <span class="o">=</span> <span class="nx">data</span><span class="p">.</span><span class="nx">toString</span><span class="p">();</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">data</span> <span class="o">!==</span> <span class="s1">''</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">data</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">split</span><span class="p">(</span><span class="s1">'\n'</span><span class="p">).</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">line</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">line</span> <span class="o">!==</span> <span class="s1">''</span><span class="p">;</span>
<span class="p">}).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">line</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">line</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">prefix</span><span class="p">)</span> <span class="o">!==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">line</span> <span class="o">=</span> <span class="nx">line</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">prefix</span><span class="p">,</span> <span class="s1">''</span><span class="p">);</span>
<span class="nx">fn</span><span class="p">(</span><span class="nx">line</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nx">deps</span><span class="p">.</span><span class="nx">stdout</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'data'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">¶</a> </div> <p>For each line of data output from the child process remove empty
lines and then add the specified packages to list of known packages.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">parseLines</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="s1">'__!load::'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">dep</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">packages</span><span class="p">[</span><span class="nx">dep</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="p">});</span>
<span class="p">});</span>
<span class="nx">deps</span><span class="p">.</span><span class="nx">stderr</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'data'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">parseLines</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="s1">'__!err::'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">line</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">errs</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">line</span><span class="p">);</span>
<span class="p">});</span>
<span class="p">});</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">¶</a> </div> <p>Set the default timeout to <code>5 seconds</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">options</span><span class="p">.</span><span class="nx">timeout</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">timeout</span> <span class="o">||</span> <span class="mi">5000</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">¶</a> </div> <p>If a timeout has been set then exit the
process after the specified timespan</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">timeoutId</span> <span class="o">=</span> <span class="nx">setTimeout</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="nx">deps</span><span class="p">.</span><span class="nx">kill</span><span class="p">();</span>
<span class="p">},</span> <span class="nx">options</span><span class="p">.</span><span class="nx">timeout</span><span class="p">);</span>
<span class="nx">deps</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'exit'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">¶</a> </div> <p>Remove the timeout now that we have exited.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">clearTimeout</span><span class="p">(</span><span class="nx">timeoutId</span><span class="p">);</span>
</pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">¶</a> </div> <p>When the process is complete remove any <code>core</code> node.js packages
(i.e. packages in <code>process.bindings('natives')</code>) and any packages
which are required with a relative directory (i.e. <code>require('./package')</code>). </p>
<p>Include any packages which may be of the form <code>require('package/relative/dir')</code>
because those relative directories are still supported by npm:
e.g.: <code>require('socket.io/lib/socket.io/utils')</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">packages</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">packages</span><span class="p">);</span>
<span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">raw</span> <span class="o">?</span> <span class="nx">packages</span> <span class="o">:</span> <span class="nx">packages</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">pkg</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">pkg</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!==</span> <span class="s1">'.'</span> <span class="o">&&</span> <span class="nx">pkg</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!==</span> <span class="s1">'/'</span> <span class="o">&&</span> <span class="o">!</span><span class="nx">core</span><span class="p">[</span><span class="nx">pkg</span><span class="p">];</span>
<span class="p">}).</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">pkg</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">pkg</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)[</span><span class="mi">0</span><span class="p">];</span>
<span class="p">})).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">pkg</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">merged</span><span class="p">[</span><span class="nx">pkg</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="p">});</span>
<span class="k">return</span> <span class="nx">errs</span><span class="p">.</span><span class="nx">length</span> <span class="o">></span> <span class="mi">1</span>
<span class="o">?</span> <span class="nx">callback</span><span class="p">(</span><span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="nx">errs</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">'\n'</span><span class="p">)),</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">merged</span><span class="p">))</span>
<span class="o">:</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">merged</span><span class="p">));</span>
<span class="p">});</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">¶</a> </div> <h3>function findModulesDir (target)</h3>
<h4>@target {string} The directory (or file) to search up from</h4>
<p>Searches up from the specified <code>target</code> until it finds a directory which contains
a folder called <code>node_modules</code></p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">analyzer</span><span class="p">.</span><span class="nx">findModulesDir</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">target</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">fs</span><span class="p">.</span><span class="nx">stat</span><span class="p">(</span><span class="nx">target</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">stats</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">stats</span><span class="p">.</span><span class="nx">isDirectory</span><span class="p">())</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">readdir</span><span class="p">(</span><span class="nx">target</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">files</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">files</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">'node_modules'</span><span class="p">)</span> <span class="o">!==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">target</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">stats</span><span class="p">.</span><span class="nx">isFile</span><span class="p">())</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">analyzer</span><span class="p">.</span><span class="nx">findModulesDir</span><span class="p">(</span><span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span><span class="p">(</span><span class="nx">target</span><span class="p">),</span> <span class="nx">callback</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">¶</a> </div> <h3>function (target [arg1, arg2, ...])</h3>
<h4>@target {Object} Object to merge into</h4>
<p>Merges all properties in <code>arg1 ... argn</code>
into the <code>target</code> object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">analyzer</span><span class="p">.</span><span class="nx">merge</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">target</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">objs</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="nx">objs</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">o</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">attr</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="nx">o</span><span class="p">.</span><span class="nx">__lookupGetter__</span><span class="p">(</span><span class="nx">attr</span><span class="p">))</span> <span class="p">{</span>
<span class="nx">target</span><span class="p">[</span><span class="nx">attr</span><span class="p">]</span> <span class="o">=</span> <span class="nx">o</span><span class="p">[</span><span class="nx">attr</span><span class="p">];</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">});</span>
<span class="k">return</span> <span class="nx">target</span><span class="p">;</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">¶</a> </div> <h3>function clone (object)</h3>
<h4>@object {Object} Object to clone.</h4>
<p>Shallow clones the target <code>object</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">analyzer</span><span class="p">.</span><span class="nx">clone</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">object</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">object</span><span class="p">).</span><span class="nx">reduce</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">k</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">obj</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="nx">object</span><span class="p">[</span><span class="nx">k</span><span class="p">];</span>
<span class="k">return</span> <span class="nx">obj</span><span class="p">;</span>
<span class="p">},</span> <span class="p">{});</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</a> </div> <h3>function extractVersions (dependencies)</h3>
<h4>@dependencies {Object} Set of dependencies to transform</h4>
<p>Transforms the <code>dependencies</code> object into the format that
package.json files accept.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">analyzer</span><span class="p">.</span><span class="nx">extractVersions</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">dependencies</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">all</span> <span class="o">=</span> <span class="p">{};</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">dependencies</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">pkg</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">version</span> <span class="o">=</span> <span class="nx">dependencies</span><span class="p">[</span><span class="nx">pkg</span><span class="p">].</span><span class="nx">version</span><span class="p">.</span><span class="nx">trim</span><span class="p">().</span><span class="nx">split</span><span class="p">(</span><span class="s1">'.'</span><span class="p">),</span>
<span class="nx">build</span> <span class="o">=</span> <span class="nx">version</span><span class="p">[</span><span class="mi">2</span><span class="p">].</span><span class="nx">match</span><span class="p">(</span><span class="sr">/^\d+(\-?[\w|\-]+)/</span><span class="p">);</span>
<span class="nx">version</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="nx">build</span> <span class="o">?</span> <span class="nx">version</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">:</span> <span class="s1">'x'</span><span class="p">;</span>
<span class="nx">all</span><span class="p">[</span><span class="nx">pkg</span><span class="p">]</span> <span class="o">=</span> <span class="nx">build</span> <span class="o">?</span> <span class="s1">'>= '</span> <span class="o">+</span> <span class="nx">dependencies</span><span class="p">[</span><span class="nx">pkg</span><span class="p">].</span><span class="nx">version</span> <span class="o">:</span> <span class="nx">version</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">'.'</span><span class="p">);</span>
<span class="p">});</span>
<span class="k">return</span> <span class="nx">all</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">¶</a> </div> <h3>function updates (current, updated)</h3>
<h4>@current {Object} Current dependencies</h4>
<h4>@updated {Object} Updated dependencies</h4>
<p>Compares the <code>current</code> dependencies against the
<code>updated</code> dependencies and returns an object
with the differences</p>
<pre><code>{
added: { /* Intersection of updated / current */ }
updated: { /* Union of updated / current with new versions */ }
}
</code></pre> </td> <td class="code"> <div class="highlight"><pre><span class="nx">analyzer</span><span class="p">.</span><span class="nx">updates</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">current</span><span class="p">,</span> <span class="nx">updated</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">updates</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">added</span><span class="o">:</span> <span class="p">{},</span>
<span class="nx">updated</span><span class="o">:</span> <span class="p">{}</span>
<span class="p">};</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">current</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">updates</span><span class="p">.</span><span class="nx">updated</span> <span class="o">=</span> <span class="nx">updated</span> <span class="o">||</span> <span class="p">{};</span>
<span class="k">return</span> <span class="nx">updates</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">updated</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">updates</span><span class="p">;</span>
<span class="p">}</span>
</pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">¶</a> </div> <p>Get the list of all added dependencies</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">updated</span><span class="p">).</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="o">!</span><span class="nx">current</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
<span class="p">}).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">updates</span><span class="p">.</span><span class="nx">added</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">updated</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
<span class="p">});</span>
</pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">¶</a> </div> <p>Get the list of all dependencies that have been updated</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">updated</span><span class="p">).</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">current</span><span class="p">[</span><span class="nx">key</span><span class="p">])</span> <span class="p">{</span>
<span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">left</span> <span class="o">=</span> <span class="nx">updated</span><span class="p">[</span><span class="nx">key</span><span class="p">].</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\<|\>|\=|\s/ig</span><span class="p">,</span> <span class="s1">''</span><span class="p">),</span>
<span class="nx">right</span> <span class="o">=</span> <span class="nx">current</span><span class="p">[</span><span class="nx">key</span><span class="p">].</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\<|\>|\=|\s/ig</span><span class="p">,</span> <span class="s1">''</span><span class="p">);</span>
<span class="k">return</span> <span class="nx">semver</span><span class="p">.</span><span class="nx">gt</span><span class="p">(</span><span class="nx">left</span><span class="p">,</span> <span class="nx">right</span><span class="p">);</span>
<span class="p">}).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">updates</span><span class="p">.</span><span class="nx">updated</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">updated</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
<span class="p">})</span>
<span class="k">return</span> <span class="nx">updates</span><span class="p">;</span>
<span class="p">};</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>