-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
58 lines (45 loc) · 1.23 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
var through = require('through2');
var gutil = require('gulp-util');
var path = require('path');
var csvjson = require('./lib/index');
var extendOptions = function(optionsObject) {
var defaultOptions = {
parserOptions: {
auto_parse: true
},
processValue: function(key, value) {
if (key !== '') {
return value;
}
}
};
if (typeof optionsObject !== 'object') {
return defaultOptions;
}
for (var k in optionsObject) {
defaultOptions[k] = optionsObject[k];
}
return defaultOptions;
};
module.exports = function(options) {
options = extendOptions(options);
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new gutil.PluginError('gulp-csv2json', 'Streaming not supported'));
}
if (['.csv'].indexOf(path.extname(file.path)) === -1) {
gutil.log('gulp-csv-to-json: Skipping unsupported csv ' + gutil.colors.blue(file.relative));
return cb(null, file);
}
csvjson.process(file.contents, options, function(err, sets) {
sets.forEach(function(set) {
file.contents = new Buffer(JSON.stringify(set.data), 'utf8');
file.path = gutil.replaceExtension(file.path, '.json');
cb(null, file);
});
});
});
};