1 |
|
2 | (function() {
|
3 | var Buffer, EventEmitter, async, fs, pathutil, _,
|
4 | __hasProp = {}.hasOwnProperty,
|
5 | __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; };
|
6 |
|
7 | EventEmitter = require('events').EventEmitter;
|
8 |
|
9 | Buffer = require('buffer').Buffer;
|
10 |
|
11 | _ = require('underscore');
|
12 |
|
13 | fs = require('fs');
|
14 |
|
15 | pathutil = require('path');
|
16 |
|
17 | async = require('async');
|
18 |
|
19 | exports.BufferStream = (function(_super) {
|
20 |
|
21 | __extends(BufferStream, _super);
|
22 |
|
23 | function BufferStream(buffer) {
|
24 | this.data = new Buffer(buffer);
|
25 | BufferStream.__super__.constructor.call(this);
|
26 | }
|
27 |
|
28 | BufferStream.prototype.pipe = function(destination) {
|
29 | destination.write(this.data);
|
30 | destination.end();
|
31 | this.emit('close');
|
32 | return this.emit('end');
|
33 | };
|
34 |
|
35 | BufferStream.prototype.pause = function() {};
|
36 |
|
37 | BufferStream.prototype.resume = function() {};
|
38 |
|
39 | BufferStream.prototype.destroy = function() {};
|
40 |
|
41 | BufferStream.prototype.readable = true;
|
42 |
|
43 | return BufferStream;
|
44 |
|
45 | })(EventEmitter);
|
46 |
|
47 | exports.extend = function(object) {
|
48 | var Asset, key, value;
|
49 | Asset = (function(_super) {
|
50 |
|
51 | __extends(Asset, _super);
|
52 |
|
53 | function Asset() {
|
54 | return Asset.__super__.constructor.apply(this, arguments);
|
55 | }
|
56 |
|
57 | return Asset;
|
58 |
|
59 | })(this);
|
60 | for (key in object) {
|
61 | value = object[key];
|
62 | Asset.prototype[key] = value;
|
63 | }
|
64 | return Asset;
|
65 | };
|
66 |
|
67 | exports.walk = function(root, options, iterator, cb) {
|
68 | var filter, ignoreFolders, readdir;
|
69 | if (_.isFunction(options)) {
|
70 | cb = iterator;
|
71 | iterator = options;
|
72 | options = {};
|
73 | }
|
74 | if (cb == null) {
|
75 | cb = function() {};
|
76 | }
|
77 | ignoreFolders = options.ignoreFolders || false;
|
78 | filter = options.filter || function() {
|
79 | return true;
|
80 | };
|
81 | if (_.isString(filter)) {
|
82 | filter = (function(ext) {
|
83 | ext = ext[0] === '.' ? ext : '.' + ext;
|
84 | return function(file) {
|
85 | return file.ext === ext;
|
86 | };
|
87 | })(filter);
|
88 | }
|
89 | readdir = function(dir, cb) {
|
90 | return fs.readdir(dir, function(err, files) {
|
91 | var iter;
|
92 | if (err != null) {
|
93 | return cb(err);
|
94 | }
|
95 | iter = function(file, done) {
|
96 | var path;
|
97 | path = pathutil.join(dir, file);
|
98 | return fs.stat(path, function(err, stats) {
|
99 | var fobj, skip;
|
100 | if (err != null) {
|
101 | return done(err);
|
102 | }
|
103 | fobj = {
|
104 | name: pathutil.basename(file),
|
105 | namenoext: pathutil.basename(file, pathutil.extname(file)),
|
106 | relpath: pathutil.relative(root, path),
|
107 | path: path,
|
108 | ext: pathutil.extname(file),
|
109 | stats: stats
|
110 | };
|
111 | skip = (ignoreFolders && stats.isDirectory()) || !filter(fobj);
|
112 | if (stats.isDirectory()) {
|
113 | return readdir(path, function(err) {
|
114 | if (err != null) {
|
115 | return done(err);
|
116 | }
|
117 | if (skip) {
|
118 | return done();
|
119 | } else {
|
120 | return iterator(fobj, done);
|
121 | }
|
122 | });
|
123 | } else {
|
124 | if (skip) {
|
125 | return done();
|
126 | } else {
|
127 | return iterator(fobj, done);
|
128 | }
|
129 | }
|
130 | });
|
131 | };
|
132 | return async.forEach(files, iter, cb);
|
133 | });
|
134 | };
|
135 | return readdir(root, cb);
|
136 | };
|
137 |
|
138 | }).call(this);
|