Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (96 sloc) 20.9 KB
<!DOCTYPE html> <html> <head> <title>by-write2js.coffee</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> by-write2js.coffee </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="nv">fs = </span><span class="nx">require</span><span class="p">(</span><span class="s">&#39;fs&#39;</span><span class="p">)</span>
<span class="nv">coffee = </span><span class="nx">require</span><span class="p">(</span><span class="s">&#39;coffee-script&#39;</span><span class="p">)</span>
<span class="nv">EventEmitter = </span><span class="nx">require</span><span class="p">(</span><span class="s">&#39;events&#39;</span><span class="p">).</span><span class="nx">EventEmitter</span>
<span class="nv">colors = </span><span class="nx">require</span><span class="p">(</span><span class="s">&#39;colors&#39;</span><span class="p">)</span>
<span class="nv">path = </span><span class="nx">require</span><span class="p">(</span><span class="s">&#39;path&#39;</span><span class="p">)</span>
<span class="nv">util = </span><span class="nx">require</span><span class="p">(</span><span class="s">&#39;util&#39;</span><span class="p">)</span>
<span class="nv">_ = </span><span class="nx">require</span><span class="p">(</span><span class="s">&#39;underscore&#39;</span><span class="p">)</span>
<span class="nv">minimatch = </span><span class="nx">require</span><span class="p">(</span><span class="s">&#39;minimatch&#39;</span><span class="p">)</span>
<span class="nv">module.exports = </span><span class="k">class</span> <span class="nx">ByWrite2JS</span> <span class="k">extends</span> <span class="nx">EventEmitter</span>
<span class="nv">constructor: </span><span class="nf">(@opts = {}) -&gt;</span>
<span class="vi">@mapper = </span><span class="nx">@opts</span><span class="o">?</span><span class="p">.</span><span class="nx">mapper</span> <span class="o">?</span> <span class="p">{}</span>
<span class="nx">@opts</span><span class="p">.</span><span class="nx">bin</span> <span class="o">?=</span> <span class="kc">false</span>
<span class="nx">@opts</span><span class="p">.</span><span class="nx">binDir</span> <span class="o">?=</span> <span class="s">&quot;</span><span class="si">#{</span><span class="nx">@opts</span><span class="p">.</span><span class="nx">root</span><span class="si">}</span><span class="s">/bin&quot;</span>
<span class="vi">@noWriteFiles = </span><span class="p">[]</span>
<span class="k">if</span> <span class="nx">@opts</span><span class="o">?</span><span class="p">.</span><span class="nx">noWrite</span><span class="o">?</span>
<span class="nx">@_setNoWriteFiles</span><span class="p">(</span><span class="nx">@opts</span><span class="p">.</span><span class="nx">noWrite</span><span class="p">)</span>
<span class="nv">_setListeners: </span><span class="nf">(@bystander) -&gt;</span>
<span class="nx">@bystander</span><span class="p">.</span><span class="k">by</span><span class="p">.</span><span class="nx">coffeescript</span><span class="p">.</span><span class="kc">on</span><span class="p">(</span><span class="s">&#39;compiled&#39;</span><span class="p">,</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="k">if</span> <span class="o">not</span> <span class="nx">@_isNoWrite</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">file</span><span class="p">)</span>
<span class="nv">data.jsfile = </span><span class="nx">@_getJSPath</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">file</span><span class="p">,</span> <span class="nx">@mapper</span><span class="p">)</span>
<span class="nx">@_writeJS</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span>
<span class="p">)</span>
<span class="nx">@bystander</span><span class="p">.</span><span class="k">by</span><span class="p">.</span><span class="nx">coffeescript</span><span class="p">.</span><span class="kc">on</span><span class="p">(</span><span class="s">&#39;coffee removed&#39;</span><span class="p">,</span> <span class="p">(</span><span class="nx">file</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="k">if</span> <span class="o">not</span> <span class="nx">@_isNoWrite</span><span class="p">(</span><span class="nx">file</span><span class="p">)</span>
<span class="nv">jsfile = </span><span class="nx">@_getJSPath</span><span class="p">(</span><span class="nx">file</span><span class="p">,</span> <span class="nx">@mapper</span><span class="p">)</span>
<span class="nx">@rmJS</span><span class="p">({</span><span class="nv">file: </span><span class="nx">file</span><span class="p">,</span> <span class="nv">jsfile: </span><span class="nx">jsfile</span><span class="p">})</span>
<span class="p">)</span>
<span class="nv">_setNoWriteFiles: </span><span class="nf">(newFiles) -&gt;</span>
<span class="vi">@noWriteFiles = </span><span class="nx">_</span><span class="p">(</span><span class="nx">@noWriteFiles</span><span class="p">).</span><span class="nx">union</span><span class="p">(</span><span class="nx">newFiles</span><span class="p">)</span>
<span class="nv">_isNoWrite: </span><span class="nf">(file) -&gt;</span>
<span class="k">for</span> <span class="nx">v</span> <span class="k">in</span> <span class="nx">@noWriteFiles</span>
<span class="k">if</span> <span class="nx">minimatch</span><span class="p">(</span><span class="nx">file</span><span class="p">,</span> <span class="nx">v</span><span class="p">,</span> <span class="p">{</span><span class="nv">dot : </span><span class="kc">true</span><span class="p">})</span>
<span class="k">return</span> <span class="kc">true</span>
<span class="k">return</span> <span class="kc">false</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <h4>Replace file extension</h4>
<p><code>txt (String)</code> : a file path to replace <br />
<code>ext (String)</code> : a new extension to replace with</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_replaceExt: </span><span class="nf">(txt, ext = &#39;js&#39;) -&gt;</span>
<span class="k">return</span> <span class="nx">txt</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\.coffee$/</span><span class="p">,</span> <span class="s">&quot;.&quot;</span> <span class="o">+</span> <span class="nx">ext</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> <h4>Get destination jsfile path to save compiled JS code to</h4>
<p><code>csfile (String)</code> : a path to the original CoffeeScript file <br />
<code>mapper (Object)</code> : rules to map <code>csfile</code> path to output js file path </p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_getJSPath: </span><span class="nf">(csfile, mapper = {}) -&gt;</span>
<span class="nv">filebase = </span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">csfile</span><span class="p">)</span>
<span class="nv">dirname = </span><span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span><span class="p">(</span><span class="nx">csfile</span><span class="p">)</span>
<span class="nv">dirdir = </span><span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span><span class="p">(</span><span class="nx">dirname</span><span class="p">)</span>
<span class="nv">basename = </span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">dirname</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">@opts</span><span class="p">.</span><span class="nx">bin</span> <span class="o">and</span> <span class="nx">filebase</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/\.bin\.coffee$/i</span><span class="p">)</span> <span class="o">isnt</span> <span class="kc">null</span>
<span class="k">return</span> <span class="s">&quot;</span><span class="si">#{</span><span class="nx">@opts</span><span class="p">.</span><span class="nx">binDir</span><span class="si">}</span><span class="s">/&quot;</span> <span class="o">+</span> <span class="nx">filebase</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\.bin\.coffee$/i</span><span class="p">,</span><span class="s">&#39;&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="k">typeof</span><span class="p">(</span><span class="nx">mapper</span><span class="p">)</span> <span class="o">is</span> <span class="s">&#39;object&#39;</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">util</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">mapper</span><span class="p">)</span>
<span class="k">for</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span> <span class="k">of</span> <span class="nx">mapper</span>
<span class="k">try</span>
<span class="nv">m = </span><span class="nx">minimatch</span><span class="p">(</span><span class="nx">csfile</span><span class="p">,</span> <span class="nx">k</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">m</span>
<span class="k">if</span> <span class="nx">util</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">v</span><span class="p">)</span>
<span class="k">return</span> <span class="nx">@_replaceExt</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">csfile</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">v</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="nx">v</span><span class="p">[</span><span class="mi">1</span><span class="p">])))</span>
<span class="k">else</span> <span class="k">if</span> <span class="k">typeof</span><span class="p">(</span><span class="nx">v</span><span class="p">)</span> <span class="o">is</span> <span class="s">&#39;function&#39;</span>
<span class="k">return</span> <span class="nx">@_replaceExt</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">v</span><span class="p">(</span><span class="nx">csfile</span><span class="p">)))</span>
<span class="k">catch</span> <span class="nx">e</span>
<span class="k">return</span> <span class="s">&quot;</span><span class="si">#{</span><span class="nx">dirname</span><span class="si">}</span><span class="s">/&quot;</span> <span class="o">+</span> <span class="nx">@_replaceExt</span><span class="p">(</span><span class="nx">filebase</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> <h4>Write compiled code to JS file</h4> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_writeJS: </span><span class="nf">(data) -&gt;</span>
<span class="nv">version = </span><span class="s">&#39;// Generated by CoffeeScript &#39;</span> <span class="o">+</span> <span class="nx">coffee</span><span class="p">.</span><span class="nx">VERSION</span>
<span class="k">if</span> <span class="nx">@opts</span><span class="p">.</span><span class="nx">bin</span> <span class="o">and</span> <span class="nx">data</span><span class="p">.</span><span class="nx">file</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/\.bin\.coffee$/i</span><span class="p">)</span> <span class="o">isnt</span> <span class="kc">null</span>
<span class="nv">version = </span><span class="s">&#39;#!/usr/bin/env node&#39;</span>
<span class="nx">fs</span><span class="p">.</span><span class="nx">writeFile</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">jsfile</span><span class="p">,</span> <span class="p">[</span><span class="nx">version</span><span class="p">,</span> <span class="nx">data</span><span class="p">.</span><span class="nx">compiled</span><span class="p">].</span><span class="nx">join</span><span class="p">(</span><span class="s">&#39;\n&#39;</span><span class="p">),</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nx">@_emitter</span><span class="p">(</span><span class="nx">err</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-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <h4>Emit an event based on JS writeErr, pass on lint result</h4> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_emitter: </span><span class="nf">(writeErr, data) -&gt;</span>
<span class="k">if</span> <span class="nx">writeErr</span>
<span class="nx">@_emitWriteError</span><span class="p">(</span><span class="nx">writeErr</span><span class="p">,</span><span class="nx">data</span><span class="p">)</span>
<span class="k">else</span>
<span class="nx">@_emitWrote</span><span class="p">(</span><span class="nx">data</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> <h4>Emit compiled event with lint result</h4> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_emitWrote: </span><span class="nf">(data) -&gt;</span>
<span class="nx">@emit</span><span class="p">(</span>
<span class="s">&#39;wrote2js&#39;</span><span class="p">,</span>
<span class="p">{</span><span class="nv">file: </span><span class="nx">data</span><span class="p">.</span><span class="nx">file</span><span class="p">,</span> <span class="nv">jsfile: </span><span class="nx">data</span><span class="p">.</span><span class="nx">jsfile</span><span class="p">,</span> <span class="nv">compiled: </span><span class="nx">data</span><span class="p">.</span><span class="nx">compiled</span><span class="p">,</span> <span class="nv">code: </span><span class="nx">data</span><span class="p">.</span><span class="nx">code</span><span class="p">}</span>
<span class="p">)</span>
<span class="nx">unless</span> <span class="nx">@opts</span><span class="p">.</span><span class="nx">nolog</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s">&quot;</span><span class="si">#{</span><span class="nx">data</span><span class="p">.</span><span class="nx">file</span><span class="si">}</span><span class="s"> =&gt; </span><span class="si">#{</span><span class="nx">data</span><span class="p">.</span><span class="nx">jsfile</span><span class="si">}</span><span class="s">&quot;</span><span class="p">.</span><span class="nx">grey</span> <span class="o">+</span> <span class="s">&#39;\n&#39;</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> <h4>Emit write error event with lint result</h4> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_emitWriteError: </span><span class="nf">(err, data) -&gt;</span>
<span class="nv">message = </span><span class="p">[</span>
<span class="s">&#39;fail to write js file&#39;</span><span class="p">.</span><span class="nx">yellow</span><span class="p">,</span>
<span class="s">&quot; - </span><span class="si">#{</span><span class="nx">data</span><span class="p">.</span><span class="nx">file</span><span class="si">}</span><span class="s">&quot;</span><span class="p">,</span>
<span class="s">&quot; -&gt; </span><span class="si">#{</span><span class="nx">data</span><span class="p">.</span><span class="nx">jsfile</span><span class="si">}</span><span class="s">&quot;</span>
<span class="p">]</span>
<span class="nx">unless</span> <span class="nx">@opts</span><span class="p">.</span><span class="nx">nolog</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">message</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">),</span> <span class="s">&#39;\n&#39;</span><span class="p">)</span>
<span class="nx">@emit</span><span class="p">(</span>
<span class="s">&#39;write2js error&#39;</span><span class="p">,</span>
<span class="p">{</span><span class="nv">file: </span><span class="nx">data</span><span class="p">.</span><span class="nx">file</span><span class="p">,</span> <span class="nv">jsfile: </span><span class="nx">data</span><span class="p">.</span><span class="nx">jsfile</span><span class="p">,</span> <span class="nv">err: </span><span class="nx">err</span><span class="p">,</span> <span class="nv">compiled: </span><span class="nx">data</span><span class="p">.</span><span class="nx">compiled</span><span class="p">,</span> <span class="nv">code: </span><span class="nx">data</span><span class="p">.</span><span class="nx">code</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> <h4>Remove the destination JS file and emit an event accordingly</h4> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">rmJS: </span><span class="nf">(data) -&gt;</span>
<span class="nx">fs</span><span class="p">.</span><span class="nx">unlink</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">jsfile</span><span class="p">,</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="k">if</span> <span class="nx">err</span>
<span class="nx">@emit</span><span class="p">(</span>
<span class="s">&#39;unlink error&#39;</span><span class="p">,</span>
<span class="p">{</span><span class="nv">file: </span><span class="nx">data</span><span class="p">.</span><span class="nx">file</span><span class="p">,</span> <span class="nv">jsfile: </span><span class="nx">data</span><span class="p">.</span><span class="nx">jsfile</span><span class="p">,</span> <span class="nv">err: </span><span class="nx">err</span><span class="p">}</span>
<span class="p">)</span>
<span class="k">else</span>
<span class="nx">@emit</span><span class="p">(</span>
<span class="s">&#39;js removed&#39;</span><span class="p">,</span>
<span class="nx">data</span>
<span class="p">)</span>
<span class="p">)</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>