-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
94 lines (75 loc) · 2.3 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
"use strict";
var gulp = require("gulp"),
path = require("path"),
gutil = require("gulp-util"),
through2 = require("through2"),
util = require("util"),
fs = require("fs"),
toposort = require("toposort-class"),
concat = require("gulp-concat");
module.exports = function(filename, sources) {
if (!filename) {
throw new gutil.PluginError("gulp-concat-vendor", "Required parameter filename is missing");
}
var mySources = {},
myLibs = [],
myFiles = [],
myInfo = [];
var bufferContents = function(file, enc, callback) {
if(file.isStream()) {
this.emit("error", new gutil.PluginError("gulp-concat-vendor", "Streaming not supported"));
return callback();
}
if(!file.isNull()) {
myFiles.push(file.path);
return callback();
}
var myPath = util.format("%s/.bower.json", file.path);
fs.exists(myPath, function(exists) {
if (exists) {
fs.readFile(myPath, "utf8", function(error, data) {
if(!!error || !data) {
return callback();
}
var myData = JSON.parse(data);
if(!!myData.main && (path.extname( myData.main ) == '.js' || myData.main.constructor === Array)) {
var myMain = [].concat(myData.main),
mySourcePath = util.format("%s/%s", file.path, myMain[0]);
myInfo.push(myData);
mySources[myData.name] = mySourcePath;
callback();
}
else {
console.log(util.format("Skipping library @ %s. Bower.js is missing 'main' property or it is not a JS filetype.", file.path));
callback();
}
});
} else {
console.log(util.format("Skipping library @ %s. Couldn't find %s", file.path, myPath));
callback();
}
});
};
var endStream = function() {
var mySort = new toposort();
myInfo.forEach(function(data) {
var myDependencies = [];
if(!!data.dependencies) {
myDependencies = Object.keys(data.dependencies);
}
mySort.add(data.name, myDependencies);
});
myLibs.push.apply(myLibs, mySort.sort().reverse().map(function(name) {
return mySources[name];
}));
myLibs = myLibs.filter(function(n){ return n != undefined });
myLibs = myLibs.concat(myFiles);
gulp.src(myLibs)
.pipe(concat(filename))
.on("data", function(data) {
this.push(data);
this.emit("end");
}.bind(this));
}
return through2.obj(bufferContents, endStream);
};