UNPKG

asset-rack

Version:

Static Web Framework for Nodejs

144 lines (122 loc) 3.94 kB
// Generated by CoffeeScript 1.6.3 (function() { var Buffer, EventEmitter, async, fs, pathutil, _, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; EventEmitter = require('events').EventEmitter; Buffer = require('buffer').Buffer; _ = require('underscore'); fs = require('fs'); pathutil = require('path'); async = require('async'); exports.BufferStream = (function(_super) { __extends(BufferStream, _super); function BufferStream(buffer) { this.data = new Buffer(buffer); BufferStream.__super__.constructor.call(this); } BufferStream.prototype.pipe = function(destination) { destination.write(this.data); destination.end(); this.emit('close'); return this.emit('end'); }; BufferStream.prototype.pause = function() {}; BufferStream.prototype.resume = function() {}; BufferStream.prototype.destroy = function() {}; BufferStream.prototype.readable = true; return BufferStream; })(EventEmitter); exports.extend = function(object) { var Asset, key, value, _ref; Asset = (function(_super) { __extends(Asset, _super); function Asset() { _ref = Asset.__super__.constructor.apply(this, arguments); return _ref; } return Asset; })(this); for (key in object) { value = object[key]; Asset.prototype[key] = value; } return Asset; }; exports.walk = function(root, options, iterator, cb) { var filter, ignoreFolders, readdir; if (_.isFunction(options)) { cb = iterator; iterator = options; options = {}; } if (cb == null) { cb = function() {}; } ignoreFolders = options.ignoreFolders || false; filter = options.filter || function() { return true; }; if (_.isString(filter)) { ignoreFolders = true; filter = (function(ext) { ext = ext[0] === '.' ? ext : '.' + ext; return function(file) { return file.stats.isDirectory() || file.ext === ext; }; })(filter); } readdir = function(dir, cb) { return fs.readdir(dir, function(err, files) { var iter; if (err != null) { return cb(err); } iter = function(file, done) { var path; path = pathutil.join(dir, file); return fs.stat(path, function(err, stats) { var fobj, skip; if (err != null) { return done(err); } fobj = { name: pathutil.basename(file), namenoext: pathutil.basename(file, pathutil.extname(file)), relpath: pathutil.relative(root, path), path: path, dirname: pathutil.dirname(path), ext: pathutil.extname(file), stats: stats }; skip = !filter(fobj); if (stats.isDirectory()) { if (skip) { return done(); } else { return readdir(path, function(err) { if (err != null) { return done(err); } if (ignoreFolders) { return done(); } else { return iterator(fobj, done); } }); } } else { if (skip) { return done(); } else { return iterator(fobj, done); } } }); }; return async.forEach(files, iter, cb); }); }; return readdir(root, cb); }; }).call(this);