Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
428 lines (355 sloc) 72.9 KB
<!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">&#182;</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">&#39;util&#39;</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">&#39;path&#39;</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">&#39;fs&#39;</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">&#39;path&#39;</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">&#39;events&#39;</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">&#39;child_process&#39;</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">&#39;npm&#39;</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">&#39;npm/lib/utils/output&#39;</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">&#39;npm/lib/utils/read-installed&#39;</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">&#39;semver&#39;</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">&#39;findit&#39;</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">&#182;</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">&#39;natives&#39;</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">&#182;</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">&#182;</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">&#39;options and options.target are required&#39;</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">&#182;</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">&#39; is not a file or a directory.&#39;</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">&#39;childError&#39;</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">&#182;</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">&#39;dependencies&#39;</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">&#182;</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">&#39;search&#39;</span><span class="p">,</span> <span class="s1">&#39;reduce&#39;</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">&#182;</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">&#182;</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">&#182;</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">&#39;function&#39;</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">&#182;</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">&#39;search&#39;</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">&amp;&amp;</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">&#39;reduce&#39;</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">&#182;</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">&#182;</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">&#182;</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">&#39;package.json&#39;</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">&#182;</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">&#182;</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">&amp;&amp;</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">&#39;file&#39;</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">&#182;</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">&#39;.js&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">ext</span> <span class="o">!==</span> <span class="s1">&#39;.coffee&#39;</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">&#39;end&#39;</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">&#182;</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">&#182;</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">&#39;package.json&#39;</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">&#182;</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">&#182;</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">&#39;package.json must have a `main` property.&#39;</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">&#182;</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">&#182;</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">&#182;</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">&#39;Errors received when analyzing &#39;</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">&#39;node&#39;</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">&#39;..&#39;</span><span class="p">,</span> <span class="s1">&#39;bin&#39;</span><span class="p">,</span> <span class="s1">&#39;find-dependencies&#39;</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">&#39;&#39;</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">&#39;\n&#39;</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">&#39;&#39;</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">&#39;&#39;</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">&#39;data&#39;</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">&#182;</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">&#39;__!load::&#39;</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">&#39;data&#39;</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">&#39;__!err::&#39;</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">&#182;</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">&#182;</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">&#39;exit&#39;</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">&#182;</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">&#182;</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">&#39;.&#39;</span> <span class="o">&amp;&amp;</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">&#39;/&#39;</span> <span class="o">&amp;&amp;</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">&#39;/&#39;</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">&gt;</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">&#39;\n&#39;</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">&#182;</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">&#39;node_modules&#39;</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">&#182;</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">&#182;</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">&#182;</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">&#39;.&#39;</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">&#39;x&#39;</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">&#39;&gt;= &#39;</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">&#39;.&#39;</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">&#182;</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">&#182;</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">&#182;</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">/\&lt;|\&gt;|\=|\s/ig</span><span class="p">,</span> <span class="s1">&#39;&#39;</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">/\&lt;|\&gt;|\=|\s/ig</span><span class="p">,</span> <span class="s1">&#39;&#39;</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>
Something went wrong with that request. Please try again.