-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (129 loc) · 4.54 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var glob = require("glob");
var fs = require('fs');
var path = require('path');
var through = require('through2');
var PluginError = require('plugin-error');
var Vinyl = require('vinyl');
module.exports = function (params) {
params = params || {};
var currentFile;
var promises = [];
function isDirectory(file) {
var filename = file.split('/').pop();
return filename.search(/\./) === -1;
}
function getFile(file) {
return file.split('/').pop();
}
function getDirectory(file) {
var arr = file.split('/');
arr.pop();
return arr.join('/');
}
function normalizePath(target) {
if (target.match(/^\//)) {
return target;
}
target = target.replace(/^\.\//, '');
return path.resolve(getDirectory(currentFile) + '/' + target);
}
function copyFile(target, dest, callback, pos) {
var isDir = isDirectory(dest);
var files = glob.sync(target, {
root: process.cwd(),
cwd: getDirectory(currentFile),
nodir: true
});
if (!files.length) {
throw new PluginError('gulp-d3r-bundle', 'No files found matching pattern: ' + target);
}
files.forEach(function(input) {
promises.push(new Promise((resolve, reject) => {
var output = dest;
// Calculate diretory offset
var noglob = target.replace(/\*\*\/\*.*/, '');
output += '/' + input.replace(process.cwd(), '').replace(noglob, '');
var file = normalizePath(input);
fs.readFile(file, function (err, data) {
if (err) {
throw new PluginError('gulp-d3r-bundle', 'Error reading file: ' + output);
}
var vnl = new Vinyl({
path: output,
contents: data
});
callback.call(null, vnl, pos);
resolve();
});
}));
});
}
function copyFiles(target, dest, callback) {
var isDir = isDirectory(dest);
if (Array.isArray(target) && !isDir && target.length > 1) {
var partial = {
count: target.length,
fragments: [],
length: 0
};
var parts = [];
for (var i = 0; i < target.length; i++) {
parts.push(new Promise((resolve, reject) => {
copyFile(target[i], dest, function (vnl, pos) {
partial.fragments[pos] = vnl.contents;
partial.length += vnl.contents.length;
resolve();
}, i);
}));
}
promises.push(new Promise((resolve, reject) => {
Promise.all(parts).then(values => {
var compiled = new Vinyl({
path: dest,
contents: Buffer.concat(partial.fragments, partial.length)
});
callback.call(null, compiled);
resolve();
});
}));
} else {
copyFile(target, dest, callback);
}
}
return through.obj(function (file, enc, callback) {
currentFile = file.path;
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
throw new PluginError('gulp-d3r-bundle', 'stream not supported');
}
if (file.isBuffer()) {
var result, dest, target;
var that = this;
var returnFile = function(vnl) {
that.push(vnl);
};
try {
result = JSON.parse(String(file.contents));
} catch (err) {
throw new PluginError('gulp-d3r-bundle', 'Invalid JSON: ' + file.path);
}
for (dest in result.javascript) {
target = result.javascript[dest];
copyFiles(target, dest, returnFile);
}
for (dest in result.css) {
target = result.css[dest];
copyFiles(target, dest, returnFile);
}
for (dest in result.assets) {
target = result.assets[dest];
copyFiles(target, dest, returnFile);
}
}
return Promise.all(promises).then(values => {
return callback(null, null);
});
});
};