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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 |
10×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
8×
15×
15×
1×
1×
1×
3×
1×
362×
1×
14×
1×
14×
14×
1×
13×
8×
5×
1×
22×
1×
22×
1×
15×
1×
13×
1×
1×
1×
15×
15×
15×
14×
14×
14×
14×
14×
1×
30×
3×
3×
3×
3×
3×
27×
30×
61×
31×
28×
13×
13×
13×
22×
22×
1×
13×
14×
13×
13×
13×
13×
13×
4×
13×
30×
30×
30×
30×
30×
365×
365×
365×
46×
365×
20×
20×
30×
30×
30×
30×
30×
13×
1×
1×
14×
14×
14×
14×
14×
30×
15×
1×
1× | 'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (EprotoProps) defineProperties(Constructor.prototype, protoProps); Eif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _bluebird = require('bluebird');
var _bluebird2 = _interopRequireDefault(_bluebird);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _filehound = require('filehound');
var _filehound2 = _interopRequireDefault(_filehound);
var _events = require('events');
var _byline = require('byline');
var _byline2 = _interopRequireDefault(_byline);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _binary = require('./binary');
var _binary2 = _interopRequireDefault(_binary);
var _zlib = require('zlib');
var _zlib2 = _interopRequireDefault(_zlib);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (I!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (I!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (Itypeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var LineStream = require('byline').LineStream;
function stringMatch(data, string) {
return data.indexOf(string) !== -1;
}
function regExpMatch(data, pattern) {
return pattern.test(data);
}
function invalidInputSource(source) {
return !(source instanceof _filehound2.default) && !_lodash2.default.isString(source) && !_lodash2.default.isArray(source);
}
function from(args) {
var arg = args[0];
if (invalidInputSource(arg)) {
throw new Error('Invalid input source');
}
if (arg instanceof _filehound2.default || _lodash2.default.isArray(arg)) {
return arg;
}
return Array.prototype.slice.call(args);
}
function getStats(file) {
return _fs2.default.statSync(file);
}
function isDirectory(file) {
return getStats(file).isDirectory();
}
function getSource(args) {
return args.length === 0 ? [process.cwd()] : from(arguments[0]);
}
function flatten(a, b) {
return a.concat(b);
}
var FileSniffer = function (_EventEmitter) {
_inherits(FileSniffer, _EventEmitter);
function FileSniffer(args) {
_classCallCheck(this, FileSniffer);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(FileSniffer).call(this));
_this.inputSource = getSource(args);
_this.filenames = [];
_this.pending = 0;
_this.processed = 0;
_this.gzipMode = false;
return _this;
}
_createClass(FileSniffer, [{
key: '_createStream',
value: function _createStream(file) {
if (this._handleGzip(file)) {
var fstream = _fs2.default.createReadStream(file);
var unzipStream = _zlib2.default.createGunzip();
var lineStream = new LineStream();
fstream.pipe(unzipStream).pipe(lineStream);
return lineStream;
}
return (0, _byline2.default)(_fs2.default.createReadStream(file, {
encoding: 'utf-8'
}));
}
}, {
key: '_createMatcher',
value: function _createMatcher(pattern) {
return _lodash2.default.isString(pattern) ? stringMatch : regExpMatch;
}
}, {
key: '_handleGzip',
value: function _handleGzip(file) {
return _path2.default.extname(file) === '.gz' && this.gzipMode;
}
}, {
key: '_notBinaryFile',
value: function _notBinaryFile(file) {
if (this._handleGzip(file)) return true;
return !(0, _binary2.default)(file);
}
}, {
key: '_groupByFileType',
value: function _groupByFileType(paths) {
var dirs = [];
var files = [];
for (var i = 0; i < paths.length; i++) {
try {
isDirectory(paths[i]) ? dirs.push(paths[i]) : files.push(paths[i]);
} catch (err) {
this.emit('error', err);
}
}
return {
files: files,
dirs: dirs
};
}
}, {
key: '_getFiles',
value: function _getFiles() {
if (this.inputSource instanceof _filehound2.default) return this.inputSource.find();
if (E_lodash2.default.isArray(this.inputSource)) {
var fileTypes = this._groupByFileType(this.inputSource);
var allFiles = _bluebird2.default.resolve(fileTypes.files);
var allDirs = _bluebird2.default.resolve([]);
if (fileTypes.dirs.length > 0) {
allDirs = _filehound2.default.create().depth(0).ignoreHiddenFiles().paths(fileTypes.dirs).find();
}
return _bluebird2.default.join(allFiles, allDirs, flatten);
}
}
}, {
key: '_search',
value: function _search(file, pattern) {
var _this2 = this;
var snifferStream = this._createStream(file);
var isMatch = this._createMatcher(pattern);
var matched = false;
snifferStream.on('readable', function () {
var line = void 0;
while (null !== (line = snifferStream.read())) {
if (line instanceof Buffer) {
line = line.toString('utf8');
}
if (isMatch(line, pattern)) {
matched = true;
_this2.emit('match', file, line);
}
}
});
snifferStream.on('end', function () {
_this2.pending--;
_this2.emit('eof', file);
if (matched) _this2.filenames.push(file);
if (_this2.pending === 0) {
_this2.emit('end', _this2.filenames);
}
});
}
}, {
key: 'gzip',
value: function gzip() {
this.gzipMode = true;
return this;
}
}, {
key: 'find',
value: function find(pattern) {
var _this3 = this;
var notBinaryFile = this._notBinaryFile.bind(this);
this._getFiles().filter(notBinaryFile).then(function (files) {
_this3.pending = files.length;
return files;
}).each(function (file) {
_this3._search(file, pattern);
});
}
}], [{
key: 'create',
value: function create() {
return new FileSniffer(arguments);
}
}]);
return FileSniffer;
}(_events.EventEmitter);
module.exports = FileSniffer; |