-
Notifications
You must be signed in to change notification settings - Fork 56
/
stream.js
72 lines (60 loc) · 1.56 KB
/
stream.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
let path = require('path');
let through = require('through2');
let utils = require('./utils');
let vinyl = require('vinyl-fs');
export default function stream(parser) {
let data = [];
let deferred = utils.defer();
function transform(file, enc, cb) {
if (file.isBuffer()) {
// Synchronously merge data and pass to next chunk
data = data.concat(parseFile(file, enc, parser));
cb();
return;
}
if (!file.isDirectory()) {
// Don't know how to handle this object
cb(new Error('Unsupported stream object.'));
}
parseDir(file, parser).then(subData => {
// Merge data and pass to next chunk
data = data.concat(subData);
cb();
}, err => {
// Reject promise and cancel stream
deferred.reject(err);
cb(err);
});
}
function flush(cb) {
// End of stream, data is full
deferred.resolve(data);
cb();
}
let filter = through.obj(transform, flush);
filter.promise = deferred.promise;
return filter;
}
export function read(src) {
return vinyl.src(src);
}
function parseDir(dir, parser) {
/* global stream */
let filter = stream(parser);
read(path.resolve(dir.path, '**/*.scss')).pipe(filter);
return filter.promise;
}
function parseFile(file, enc, parser) {
let fileData = parser.parse(file.contents.toString(enc));
let data = [];
Object.keys(fileData).forEach(type => {
fileData[type].forEach(item => {
item.file = {
path: file.relative,
name: path.basename(file),
};
data.push(item);
});
});
return data;
}