Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
152 lines (110 sloc) 6.93 KB
<!DOCTYPE html>
<html>
<head>
<title>watcher_lib.coffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="resources/docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>watcher_lib.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">&#182;</a>
</div>
<p>Require external dependencies (only node.js libs for now).</p>
</div>
<div class="content"><div class='highlight'><pre>execute = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>).exec
fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>)
partial = <span class="hljs-built_in">require</span>(<span class="hljs-string">'partial'</span>)</pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">&#182;</a>
</div>
<p>Searches through a directory structure for <code>fileext</code> files using <code>find</code>.
For each <code>fileext</code> file it runs <code>fnCompile</code> to compile the file if it’s modified.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.<span class="hljs-function"><span class="hljs-title">findFiles</span> = <span class="hljs-params">(fileext, dir, fnCompile)</span> -&gt;</span>
execute(<span class="hljs-string">"find <span class="hljs-subst">#{dir}</span> -name '<span class="hljs-subst">#{fileext}</span>' -print"</span>,
<span class="hljs-function"><span class="hljs-params">(error, stdout, stderr)</span> -&gt;</span>
count = <span class="hljs-number">0</span>
timeout = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> file <span class="hljs-keyword">in</span> stdout.split(<span class="hljs-string">'\n'</span>)
<span class="hljs-keyword">if</span> file
count += <span class="hljs-number">1</span>
<span class="hljs-keyword">if</span> count % <span class="hljs-number">20</span> == <span class="hljs-number">0</span>
timeout += <span class="hljs-number">1000</span>
setTimeout(partial(fnCompile, file), timeout)
)</pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">&#182;</a>
</div>
<p>Keeps a track of modified times for files in a in-memory object (<code>watched_files</code>),
if a file is modified it recompiles it using fnCompileFile.</p>
<p>When starting the script all files will be recompiled.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.<span class="hljs-function"><span class="hljs-title">compileIfNeeded</span> = <span class="hljs-params">(watched_files, file, fnCompileFile)</span> -&gt;</span>
fs.stat<span class="hljs-function"><span class="hljs-params">(file, (err, stats) -&gt;
old_mtime = watched_files[file]
new_mtime = <span class="hljs-keyword">new</span> Date(stats.mtime)
<span class="hljs-keyword">if</span> !old_mtime
should_compile = <span class="hljs-literal">true</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> new_mtime &gt; old_mtime
should_compile = <span class="hljs-literal">true</span>
<span class="hljs-keyword">else</span>
should_compile = <span class="hljs-literal">false</span>
watched_files[file] = new_mtime
fnCompileFile file <span class="hljs-keyword">if</span> should_compile
)</span>
</span></pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">&#182;</a>
</div>
<p>Compiles a file using <code>command</code>.</p>
<p>Compilation errors are printed out to stdout.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.<span class="hljs-function"><span class="hljs-title">compileFile</span> = <span class="hljs-params">(command, file, fnGetOutputfile)</span> -&gt;</span>
execute<span class="hljs-function"><span class="hljs-params">(command, {<span class="hljs-string">'maxBuffer'</span>: <span class="hljs-number">800</span>*<span class="hljs-number">1024</span>}, (error, stdout, stderr) -&gt;
<span class="hljs-keyword">if</span> error <span class="hljs-keyword">isnt</span> <span class="hljs-literal">null</span>
<span class="hljs-built_in">console</span>.log error.message
<span class="hljs-keyword">else</span>
output_filename = fnGetOutputfile(file)
fs.writeFile(output_filename, stdout.toString(), (err) -&gt;
<span class="hljs-keyword">throw</span> err <span class="hljs-keyword">if</span> err
<span class="hljs-built_in">console</span>.log <span class="hljs-string">"Compiled <span class="hljs-subst">#{file}</span> to <span class="hljs-subst">#{output_filename}</span>"</span>
)
)</span>
</span></pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">&#182;</a>
</div>
<p>Starts a poller that polls each second in a directory</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.<span class="hljs-function"><span class="hljs-title">startDirectoryPoll</span> = <span class="hljs-params">(dir, fnFindFiles)</span> -&gt;</span>
<span class="hljs-function"><span class="hljs-title">directoryPoll</span> = -&gt;</span> fnFindFiles(dir)
directoryPoll()
setInterval directoryPoll, <span class="hljs-number">1000</span></pre></div></div>
</li>
</ul>
</div>
</body>
</html>