forked from Doist/coffee-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
67 lines (53 loc) · 8.33 KB
/
index.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
<!DOCTYPE html> <html> <head> <title>coffee-watcher.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="resources/docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> coffee-watcher.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">¶</a> </div> <p><strong>coffee-watcher</strong> is a script that can watch
a directory and recompile your <a href="http://jashkenas.github.com/coffee-script/">.coffee scripts</a> if they change.
It's very useful for development - you basically don't need to think about
recompiling your CoffeeScript files. Search is done in a recursive manner
so sub-directories are handled as well.</p>
<p>Installing coffee-watcher is easy with <a href="http://npmjs.org/">npm</a>:</p>
<pre><code> sudo npm install coffee-watcher
</code></pre>
<p>Run this to watch for changes in the current working directory:</p>
<pre><code> coffee-watcher
</code></pre>
<p>Run this to watch for changes in a specified directory:</p>
<pre><code> coffee-watcher ~/Desktop/my_project
</code></pre>
<p>coffee-watcher requires:</p>
<ul>
<li><a href="http://nodejs.org/">node.js</a> </li>
<li><a href="http://en.wikipedia.org/wiki/Find">find</a></li>
<li><a href="http://jashkenas.github.com/coffee-script/">CoffeeScript</a></li>
</ul> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>Searches through a directory structure for *.coffee files using <code>find</code>.
For each .coffee file it runs <code>compileIfNeeded</code> to compile the file if it's modified.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">findCoffeeFiles = </span><span class="nf">(dir) -></span>
<span class="nx">execute</span><span class="p">(</span><span class="s2">"find #{dir} -name '*.coffee' -print"</span><span class="p">,</span>
<span class="nf">(error, stdout, stderr) -></span>
<span class="k">for</span> <span class="nx">file</span> <span class="k">in</span> <span class="nx">stdout</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">compileIfNeeded</span> <span class="nx">file</span> <span class="k">if</span> <span class="nx">file</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> <p>Keeps a track of modified times for .coffee files in a in-memory object,
if a .coffee file is modified it recompiles it using compileCoffeeScript.</p>
<p>When starting the script all files will be recompiled.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">WATCHED_FILES = </span><span class="p">{}</span>
<span class="nv">compileIfNeeded = </span><span class="nf">(file) -></span>
<span class="nx">fs</span><span class="p">.</span><span class="nx">stat</span><span class="p">(</span><span class="nx">file</span><span class="p">,</span> <span class="nf">(err, stats) -></span>
<span class="nv">old_mtime = </span><span class="nx">WATCHED_FILES</span><span class="p">[</span><span class="nx">file</span><span class="p">]</span>
<span class="nv">new_mtime = </span><span class="k">new</span> <span class="nb">Date</span><span class="p">(</span><span class="nx">stats</span><span class="p">.</span><span class="nx">mtime</span><span class="p">)</span>
<span class="k">if</span> <span class="o">!</span><span class="nx">old_mtime</span>
<span class="nv">should_compile = </span><span class="kc">true</span>
<span class="k">else</span> <span class="k">if</span> <span class="nx">new_mtime</span> <span class="o">></span> <span class="nx">old_mtime</span>
<span class="nv">should_compile = </span><span class="kc">true</span>
<span class="k">else</span>
<span class="nv">should_compile = </span><span class="kc">false</span>
<span class="nx">WATCHED_FILES</span><span class="p">[</span><span class="nx">file</span><span class="p">]</span> <span class="o">=</span> <span class="nx">new_mtime</span>
<span class="nx">compileCoffeeScript</span> <span class="nx">file</span> <span class="k">if</span> <span class="nx">should_compile</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>Compiles a file using <code>coffee -c</code>.</p>
<p>Compilation errors are printed out to stdout.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">compileCoffeeScript = </span><span class="nf">(file) -></span>
<span class="nx">execute</span><span class="p">(</span><span class="s2">"coffee -c #{ file }"</span><span class="p">,</span> <span class="nf">(error, stdout, stderr) -></span>
<span class="k">if</span> <span class="nx">error</span> <span class="o">isnt</span> <span class="kc">null</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="nx">error</span><span class="p">.</span><span class="nx">message</span>
<span class="k">else</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s2">"Compiled #{ file }"</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>Require external dependencies (only node.js for now)</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">execute = </span><span class="nx">require</span><span class="p">(</span><span class="s1">'child_process'</span><span class="p">).</span><span class="nx">exec</span>
<span class="nv">fs = </span><span class="nx">require</span><span class="p">(</span><span class="s1">'fs'</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>Starts a poller that polls each second in a directory that's
either by default the current working directory or a directory that's passed through process arguments.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">directoryPoll = </span><span class="o">-></span> <span class="nx">findCoffeeFiles</span><span class="p">(</span><span class="nx">process</span><span class="p">.</span><span class="nx">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">or</span> <span class="s1">'.'</span><span class="p">)</span>
<span class="nx">directoryPoll</span><span class="p">()</span>
<span class="nx">setInterval</span> <span class="nx">directoryPoll</span><span class="p">,</span> <span class="mi">1000</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>