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 | __extends(BufferStream, _super);
|
21 |
|
22 | function BufferStream(buffer) {
|
23 | this.data = new Buffer(buffer);
|
24 | BufferStream.__super__.constructor.call(this);
|
25 | }
|
26 |
|
27 | BufferStream.prototype.pipe = function(destination) {
|
28 | destination.write(this.data);
|
29 | destination.end();
|
30 | this.emit('close');
|
31 | return this.emit('end');
|
32 | };
|
33 |
|
34 | BufferStream.prototype.pause = function() {};
|
35 |
|
36 | BufferStream.prototype.resume = function() {};
|
37 |
|
38 | BufferStream.prototype.destroy = function() {};
|
39 |
|
40 | BufferStream.prototype.readable = true;
|
41 |
|
42 | return BufferStream;
|
43 |
|
44 | })(EventEmitter);
|
45 |
|
46 | exports.extend = function(object) {
|
47 | var Asset, key, value, _ref;
|
48 |
|
49 | Asset = (function(_super) {
|
50 | __extends(Asset, _super);
|
51 |
|
52 | function Asset() {
|
53 | _ref = Asset.__super__.constructor.apply(this, arguments);
|
54 | return _ref;
|
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 |
|
70 | if (_.isFunction(options)) {
|
71 | cb = iterator;
|
72 | iterator = options;
|
73 | options = {};
|
74 | }
|
75 | if (cb == null) {
|
76 | cb = function() {};
|
77 | }
|
78 | ignoreFolders = options.ignoreFolders || false;
|
79 | filter = options.filter || function() {
|
80 | return true;
|
81 | };
|
82 | if (_.isString(filter)) {
|
83 | ignoreFolders = true;
|
84 | filter = (function(ext) {
|
85 | ext = ext[0] === '.' ? ext : '.' + ext;
|
86 | return function(file) {
|
87 | return file.stats.isDirectory() || file.ext === ext;
|
88 | };
|
89 | })(filter);
|
90 | }
|
91 | readdir = function(dir, cb) {
|
92 | return fs.readdir(dir, function(err, files) {
|
93 | var iter;
|
94 |
|
95 | if (err != null) {
|
96 | return cb(err);
|
97 | }
|
98 | iter = function(file, done) {
|
99 | var path;
|
100 |
|
101 | path = pathutil.join(dir, file);
|
102 | return fs.stat(path, function(err, stats) {
|
103 | var fobj, skip;
|
104 |
|
105 | if (err != null) {
|
106 | return done(err);
|
107 | }
|
108 | fobj = {
|
109 | name: pathutil.basename(file),
|
110 | namenoext: pathutil.basename(file, pathutil.extname(file)),
|
111 | relpath: pathutil.relative(root, path),
|
112 | path: path,
|
113 | dirname: pathutil.dirname(path),
|
114 | ext: pathutil.extname(file),
|
115 | stats: stats
|
116 | };
|
117 | skip = !filter(fobj);
|
118 | if (stats.isDirectory()) {
|
119 | if (skip) {
|
120 | return done();
|
121 | } else {
|
122 | return readdir(path, function(err) {
|
123 | if (err != null) {
|
124 | return done(err);
|
125 | }
|
126 | if (ignoreFolders) {
|
127 | return done();
|
128 | } else {
|
129 | return iterator(fobj, done);
|
130 | }
|
131 | });
|
132 | }
|
133 | } else {
|
134 | if (skip) {
|
135 | return done();
|
136 | } else {
|
137 | return iterator(fobj, done);
|
138 | }
|
139 | }
|
140 | });
|
141 | };
|
142 | return async.forEach(files, iter, cb);
|
143 | });
|
144 | };
|
145 | return readdir(root, cb);
|
146 | };
|
147 |
|
148 | }).call(this);
|