-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add annotated source
- Loading branch information
Niall O'Higgins
committed
Aug 1, 2012
1 parent
95b3a7c
commit 3ec7b39
Showing
2 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
<!DOCTYPE html> | ||
<!-- Generated by litjs - https://github.com/apres/lit.js | ||
Part of the Apres suite - http://apres.github.com/ --> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="chrome=1"> | ||
<title>main.js</title> | ||
<link href='http://apres.github.com/lit.js/css/lit-columns.css' rel='stylesheet' type='text/css'> | ||
<link href='http://apres.github.com/lit.js/css/apres.css' rel='stylesheet' type='text/css'> | ||
</head> | ||
<body> | ||
<div class="lit"> | ||
<section> | ||
<div class="lit-comment"> | ||
<h1>main.js</h1> | ||
|
||
<p>Loader for Strider extension modules.</p> | ||
</div> | ||
<code class="lit-code"> | ||
<span class="keyword">var</span> fs = require(<span class="string">'fs'</span>), | ||
path = require(<span class="string">'path'</span>), | ||
Step = require(<span class="string">'step'</span>); | ||
</code> | ||
</section> | ||
<section> | ||
<div class="lit-comment"> | ||
<h3>Locate Strider Extensions</h3> | ||
|
||
<p>Under a specified path <strong>dir</strong> [by default, process.cwd()/node_modules] look<br />for directories containing file 'strider.json'. These are considered<br />Strider modules.</p> | ||
|
||
<p><strong>cb</strong> is a function of signature cb(err, extensions) where extensions is an<br />array of filesystems path on success and err is an error on failure.</p> | ||
</div> | ||
<code class="lit-code"> | ||
<span class="function"><span class="keyword">function</span> <span class="title">findExtensions</span><span class="params">(dir, cb)</span> {</span> | ||
</code> | ||
</section> | ||
<section> | ||
<div class="lit-comment"> | ||
<p>XXX May not be sane default when installed globally</p> | ||
</div> | ||
<code class="lit-code"> | ||
<span class="keyword">var</span> dir = dir || path.join(process.cwd(), <span class="string">"node_modules"</span>); | ||
|
||
<span class="keyword">var</span> filename = <span class="string">"strider.json"</span>; | ||
|
||
<span class="keyword">var</span> extensions = []; | ||
|
||
Step( | ||
<span class="keyword">function</span>() { | ||
</code> | ||
</section> | ||
<section> | ||
<div class="lit-comment"> | ||
<p>find top-level module dirs</p> | ||
</div> | ||
<code class="lit-code"> | ||
fs.readdir(dir, <span class="keyword">this</span>); | ||
}, | ||
<span class="keyword">function</span>(err, entries) { | ||
<span class="keyword">if</span> (err) { | ||
<span class="keyword">throw</span> err; | ||
} | ||
</code> | ||
</section> | ||
<section> | ||
<div class="lit-comment"> | ||
<p>Stat extension files in parallel</p> | ||
</div> | ||
<code class="lit-code"> | ||
<span class="keyword">var</span> group = <span class="keyword">this</span>.group(); | ||
entries.forEach(<span class="keyword">function</span>(module) { | ||
<span class="keyword">var</span> p = path.join(dir, module, filename); | ||
<span class="keyword">var</span> cb = group(); | ||
fs.stat(p, <span class="keyword">function</span>(err, stat) { | ||
cb(err, | ||
{stat:stat, path:p}); | ||
}); | ||
}); | ||
}, | ||
<span class="keyword">function</span>(err, results) { | ||
results.forEach(<span class="keyword">function</span>(r) { | ||
<span class="keyword">if</span> (!r.stat) { | ||
<span class="keyword">return</span>; | ||
} | ||
</code> | ||
</section> | ||
<section> | ||
<div class="lit-comment"> | ||
<p>Ensure they are of type file not something else</p> | ||
</div> | ||
<code class="lit-code"> | ||
<span class="keyword">if</span> (r.stat.isFile()) { | ||
extensions.push(r.path); | ||
} | ||
}); | ||
cb(err, extensions); | ||
} | ||
); | ||
} | ||
</code> | ||
</section> | ||
<section> | ||
<div class="lit-comment"> | ||
<h3>Load a Strider extension</h3> | ||
|
||
<p><strong>filename</strong> is a filesystem location to a strider.json file. Extension is<br />assumed to be contained in same directory as strider.json.</p> | ||
|
||
<p><strong>cb</strong> is a function of signature function(err, extension) where extension<br />is an extension object on success and err is an error on failure.</p> | ||
|
||
<p>Note that this function does not initialize the extension.</p> | ||
</div> | ||
<code class="lit-code"> | ||
<span class="function"><span class="keyword">function</span> <span class="title">loadExtension</span><span class="params">(filename, cb)</span> {</span> | ||
Step( | ||
<span class="keyword">function</span>() { | ||
fs.readFile(filename, <span class="keyword">this</span>); | ||
}, | ||
<span class="keyword">function</span>(err, data) { | ||
<span class="keyword">if</span> (err) { | ||
<span class="keyword">return</span> cb(err, <span class="literal">null</span>); | ||
} | ||
</code> | ||
</section> | ||
<section> | ||
<div class="lit-comment"> | ||
<p>Parse extension JSON</p> | ||
</div> | ||
<code class="lit-code"> | ||
<span class="keyword">try</span> { | ||
<span class="keyword">var</span> extensionConfig = JSON.parse(data); | ||
} <span class="keyword">catch</span>(e) { | ||
<span class="keyword">return</span> cb(e, <span class="literal">null</span>); | ||
} | ||
</code> | ||
</section> | ||
<section> | ||
<div class="lit-comment"> | ||
<p>Build require'able path to extension sources</p> | ||
</div> | ||
<code class="lit-code"> | ||
<span class="keyword">var</span> extension = { | ||
webapp: require(filename.replace(<span class="string">'strider.json'</span>, | ||
extensionConfig.webapp)), | ||
worker: require(filename.replace(<span class="string">'strider.json'</span>, | ||
extensionConfig.worker)), | ||
}; | ||
|
||
cb(<span class="literal">null</span>, extension); | ||
} | ||
); | ||
} | ||
</code> | ||
</section> | ||
<section> | ||
<div class="lit-comment"> | ||
<p>Exported functions</p> | ||
</div> | ||
<code class="lit-code"> | ||
module.exports = { | ||
findExtensions: findExtensions, | ||
loadExtension: loadExtension | ||
}; | ||
</code> | ||
</section> | ||
</div> | ||
|
||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters