-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
readers.js
106 lines (96 loc) · 2.66 KB
/
readers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
module.exports = function (app) {
app.visit('mixin', {
/**
* Asynchronously glob files or directories that match
* the given `pattern`.
*
* ```js
* var glob = require('glob-fs')({ gitignore: true });
*
* glob.readdir('*.js', function (err, files) {
* //=> do stuff with `files`
* });
* ```
*
* @name .readdir
* @param {String} `pattern` Glob pattern
* @param {Object} `options`
* @param {Function} `cb` Callback
* @api public
*/
readdir: function(pattern, options, cb) {
if (typeof options === 'function') {
return this.readdir(pattern, {}, options);
}
this.emit('read');
this.setPattern(pattern, options);
this.iteratorAsync(this.pattern.base, function (err) {
if (err) return cb(err);
this.emit('end', this.files);
cb.call(this, null, this.files);
}.bind(this));
return this;
},
/**
* Synchronously glob files or directories that match
* the given `pattern`.
*
* ```js
* var glob = require('glob-fs')({ gitignore: true });
*
* var files = glob.readdirSync('*.js');
* //=> do stuff with `files`
* ```
*
* @name .readdirSync
* @param {String} `pattern` Glob pattern
* @param {Object} `options`
* @returns {Array} Returns an array of files.
* @api public
*/
readdirSync: function(pattern, options) {
this.emit('read');
this.setPattern(pattern, options);
this.iteratorSync(this.pattern.base);
this.emit('end', this.files);
return this.files;
},
/**
* Stream files or directories that match the given glob `pattern`.
*
* ```js
* var glob = require('glob-fs')({ gitignore: true });
*
* glob.readdirStream('*.js')
* .on('data', function (file) {
* console.log(file.path);
* })
* .on('error', console.error)
* .on('end', function () {
* console.log('end');
* });
* ```
*
* @name .readdirStream
* @param {String} `pattern` Glob pattern
* @param {Object} `options`
* @returns {Stream}
* @api public
*/
readdirStream: function(pattern, options) {
this.emit('read');
this.setPattern(pattern, options);
var res = this.iteratorStream(this.pattern.base);
this.emit('end', this.files);
return res;
},
readdirPromise: function(pattern, options) {
this.emit('read');
this.setPattern(pattern, options);
var res = this.iteratorPromise(this.pattern.base);
this.emit('end', this.files);
return res;
}
});
};