-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathindex.js
86 lines (69 loc) · 2.02 KB
/
index.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
'use strict';
var es = require('event-stream');
var _ = require("lodash");
var vfs = require('vinyl-fs');
var through2 = require('through2');
var gutil = require('gulp-util');
var JadeInheritance = require('jade-inheritance');
var PLUGIN_NAME = 'gulp-jade-inheritance';
var stream;
var errors = {};
function gulpJadeInheritance(options) {
options = options || {};
var files = [];
function writeStream(currentFile) {
if (currentFile && currentFile.contents.length) {
files.push(currentFile);
}
}
function endStream() {
if (files.length) {
var jadeInheritanceFiles = [];
var filesPaths = [];
options = _.defaults(options, {'basedir': process.cwd()});
_.forEach(files, function(file) {
try {
var jadeInheritance = new JadeInheritance(file.path, options.basedir, options);
} catch (e) {
// prevent multiple errors on the same file
var alreadyShown;
if (errors[e.message]) {
alreadyShown = true;
}
clearTimeout(errors[e.message]);
errors[e.message] = setTimeout(function () {
delete errors[e.message];
}, 500); //debounce
if (alreadyShown) {
return;
}
var err = new gutil.PluginError(PLUGIN_NAME, e);
stream.emit("error", err);
return;
}
var fullpaths = _.map(jadeInheritance.files, function (file) {
return options.basedir + "/" + file;
});
filesPaths = _.union(filesPaths, fullpaths);
});
if(filesPaths.length) {
vfs.src(filesPaths, {'base': options.basedir})
.pipe(es.through(
function (f) {
stream.emit('data', f);
},
function () {
stream.emit('end');
}
));
} else {
stream.emit('end');
}
} else {
stream.emit('end');
}
}
stream = es.through(writeStream, endStream);
return stream;
};
module.exports = gulpJadeInheritance;