forked from patrickpietens/gulp-concat-vendor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (95 loc) · 3.03 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
'use strict';
const fs = require('fs').promises;
const path = require("path");
const PluginError = require("plugin-error");
const Toposort = require("toposort-class");
const Transform = require('readable-stream/transform');
const util = require("util");
/**
* Takes a list of 'folders' container packages installed with bower, and returns the contents of mains for all
* packages in dependency order.
*/
class BowerMains extends Transform {
constructor() {
super();
this.toposort = new Toposort();
this.packageMeta = [];
}
/**
* Index and build the dependency graph for the bower packages.
*
* @param {*} file
* @param {*} encoding
* @param {*} callback
*/
_transform(file, encoding, callback) {
if(file.isStream()) {
this.emit("error", new PluginError("gulp-concat-vendor", "Streaming not supported"));
return callback();
}
// skips files in the stream, we only want to operate on directories.
// we want to iterate over the folders where we've installed modules
// and look for the .bower.json
if(!file.isDirectory()) {
this.files.push(file.path);
return callback();
}
let bowerJsonPath = path.join(file.path, '.bower.json');
fs.open(bowerJsonPath)
.catch((e) => {
console.log(util.format("Skipping library @ %s. Couldn't find %s. (e: %s)", file.path, bowerJsonPath, e));
return callback();
})
.then((handle) => fs.readFile(handle, {encoding}))
.catch((e) => {
console.log(util.format("Skipping library @ %s. Couldn't read %s. (e: %s)", file.path, bowerJsonPath, e));
return callback();
})
.then((data) => JSON.parse(data))
.catch((e) => {
console.log(util.format("Skipping library @ %s. Couldn't parse %s. (e: %s)", file.path, bowerJsonPath, e));
return callback();
})
.then((bower) => {
if (!bower) throw new Error("No data")
return bower;
})
.catch((e) => {
console.log(util.format("Skipping library @ %s. No data in %s. (e: %s)", file.path, bowerJsonPath, e));
return callback();
})
.then((bower) => {
if (!bower.main) throw new Error("No main")
return bower;
})
.catch((e) => {
console.log(util.format("Skipping library @ %s because .bower.json is missing 'main' property.", file.path));
return callback();
})
.then((bower) => {
// store meta data and build dependency tree.
this.packageMeta[bower.name] = bower;
let dependencies = data.dependencies && Object.keys(bower.dependencies) || [];
this.toposort.add(bower.name, dependencies);
return callback();
});
}
// inject all the main files in reverse dependency order for concatenation.
_flush() {
let orderdPackages = this.toposort.sort().reverse();
orderdPackages.forEach((name) => {
let bower = this.packageMeta[name];
// normalize singular and arrays as an array.
let mains = [].concat(bower.mains)
// inject each main into gulp stream.
mains.forEach((main) => {
let file = new Vinyl({ path: main });
this.push(file)
});
});
}
}
function factory(filename) {
return new BowerMains();
}
module.exports = factory;