-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
93 lines (77 loc) · 2.4 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
87
88
89
90
91
92
93
"use strict";
var through = require('through');
var path = require('path');
var gutil = require('gulp-util');
var jsonlint = require("jsonlint");
var PluginError = gutil.PluginError;
var File = gutil.File;
var c = gutil.colors;
module.exports = function (fileName, converter) {
if (!fileName) {
throw new PluginError('gulp-jsonconcat', 'Missing fileName option for gulp-jsonconcat');
}
if (!converter) {
throw new PluginError('gulp-jsonconcat', 'Missing converter option for gulp-jsonconcat');
}
var data = {};
var firstFile = null;
//We keep track of when we should skip the conversion for error cases
var skipConversion = false;
function bufferContents(file) {
try {
jsonlint.parse(String(file.contents));
}
catch (err) {
gutil.log(c.yellow('Error on file ') + file.path);
gutil.log(c.red(err.message));
gutil.log(c.yellow('Your JSON file was not generated. Please correct your JSON file.'));
return false;
}
if (!firstFile) {
firstFile = file;
}
if (file.isNull()) {
return; // ignore
}
if (file.isStream()) {
skipConversion = true;
return this.emit('error', new PluginError('gulp-jsonconcat', 'Streaming not supported'));
}
try {
// Remove subfolders name in the file.relative value
if (file.relative.indexOf('\\') >= 0) {
var fileFlat = path.parse(file.relative);
var fileContent = file.contents;
var file = new gutil.File({
cwd: "",
base: "",
path: fileFlat.base,
contents: fileContent
})
}
data[path.basename(file.relative, ".json")] = JSON.parse(file.contents.toString());
} catch (err) {
skipConversion = true;
return this.emit('error',
new PluginError('gulp-jsonconcat', 'Error parsing JSON: ' + err + ', file: ' + file.path.slice(file.base.length)));
}
}
function endStream() {
if (firstFile && !skipConversion) {
var joinedPath = path.join(firstFile.base, fileName);
try {
var joinedFile = new File({
cwd: firstFile.cwd,
base: firstFile.base,
path: joinedPath,
contents: converter(data)
});
this.emit('data', joinedFile);
} catch (e) {
return this.emit('error', new PluginError('gulp-jsonconcat', e, { showStack: true }));
}
}
this.emit('end');
}
return through(bufferContents, endStream);
};