-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
65 lines (54 loc) · 1.42 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
var extend = require('util-extend');
var through = require('through2');
var PluginError = require('plugin-error');
module.exports = function(data) {
'use strict';
// if necessary check for required param(s), e.g. options hash, etc.
if (!data) {
throw new PluginError('gulp-data', 'No data supplied');
}
// see 'Writing a plugin'
// https://github.com/gulpjs/gulp/blob/master/docs/writing-a-plugin/README.md
function gulpData(file, enc, callback) {
/*jshint validthis:true*/
var self = this, called = false;
function handle(err, result){
// extra guard in case
if (called) {
return;
}
called = true;
if (err) {
self.emit('error', new PluginError('gulp-data', { message: err }));
return callback();
}
file.data = extend(file.data || {}, result);
self.push(file);
callback();
}
function local(data) {
if (data && typeof data.then === 'function') {
data.then(function(data){
return handle(undefined, data);
}, function(err) { return handle(err); });
}
else {
handle(undefined, data);
}
}
var res = null;
if (typeof data === 'function') {
try {
res = data(file, handle);
} catch(e) {
handle(e);
}
if (data.length <= 1) {
local(res);
}
return;
}
local(data);
}
return through.obj(gulpData);
};