hem
Version:
stitches CommonJS, and ties up other lose ends of web-app development.
122 lines (100 loc) • 2.88 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var Module, Stitch, _createModule, _modules, _walk, compilers, flatten, fs, modulerize, npath;
npath = require('path');
fs = require('fs');
compilers = require('./compilers');
modulerize = require('./resolve').modulerize;
flatten = require('./utils').flatten;
_modules = {};
_walk = function(path, parent, result) {
var child, i, len, module, ref, stat;
if (parent == null) {
parent = path;
}
if (result == null) {
result = [];
}
if (!fs.existsSync(path)) {
return;
}
ref = fs.readdirSync(path);
for (i = 0, len = ref.length; i < len; i++) {
child = ref[i];
child = npath.join(path, child);
stat = fs.statSync(child);
if (stat.isDirectory()) {
_walk(child, parent, result);
} else {
module = _createModule(child, parent);
if (module.valid()) {
result.push(module);
}
}
}
return result;
};
_createModule = function(child, parent) {
if (!_modules[child]) {
_modules[child] = new Module(child, parent);
}
return _modules[child];
};
Stitch = (function() {
Stitch.template = function(identifier, modules) {
var context;
context = {
identifier: identifier,
modules: modules
};
return require('./utils').tmpl("stitch", context);
};
Stitch.clear = function(filename) {
return delete _modules[npath.resolve(filename)];
};
function Stitch(paths) {
var path;
this.paths = paths != null ? paths : [];
this.paths = (function() {
var i, len, ref, results;
ref = this.paths;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
path = ref[i];
results.push(npath.resolve(path));
}
return results;
}).call(this);
}
Stitch.prototype.resolve = function() {
var path;
return flatten((function() {
var i, len, ref, results;
ref = this.paths;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
path = ref[i];
results.push(_walk(path));
}
return results;
}).call(this));
};
return Stitch;
})();
Module = (function() {
function Module(filename1, parent1) {
this.filename = filename1;
this.parent = parent1;
this.ext = npath.extname(this.filename).slice(1);
this.id = modulerize(this.filename.replace(npath.join(this.parent, npath.sep), ''));
}
Module.prototype.compile = function() {
return this._compiled || (this._compiled = compilers[this.ext](this.filename));
};
Module.prototype.valid = function() {
return !!compilers[this.ext];
};
return Module;
})();
module.exports = Stitch;
}).call(this);