forked from ck86/gulp-bower-files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (78 loc) · 3.37 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
var fs = require("fs");
var path = require("path");
var gulp = require("gulp");
var gutil = require("gulp-util");
var PluginError = gutil.PluginError
const PLUGIN_NAME = "gulp-bower-files";
var readMainFilesFromDependency = function(dependencyConfig) {
jsonPath = firstExistingFile([path.join(dependencyConfig.basePath, "bower.json"),
path.join(dependencyConfig.basePath, "package.json")]);
if(!jsonPath){
throw new PluginError(PLUGIN_NAME, "The bower package " + dependencyConfig.name + " has no bower.json or package.json, use the overrides property in your bower.json");
}
var json = JSON.parse(fs.readFileSync(jsonPath))
if(!json.main){
throw new PluginError(PLUGIN_NAME, "The bower package " + dependencyConfig.name + " has no main file(s), use the overrides property in your bower.json");
}
return json.main;
}
/**
* Given a list of paths, return the first path that exists.
* @param paths {array[string]} ordered array of paths to check.
* @return {string} First path that exists or null if none exist.
*/
var firstExistingFile = function(paths) {
return paths.reduce(function(prev, curr) {
if (prev) return prev;
return fs.existsSync(curr)? curr : null;
}, null);
}
var addToSrcs = function(srcs, basePath, main){
if(Array.isArray(main)){
return main.forEach(function(item){
addToSrcs(srcs, basePath, item);
});
}
var basename = path.basename(main);
srcs.push(path.join(basePath, "**", basename));
}
var gulpBowerFiles = function(opts){
opts = opts || {};
if(!opts.paths)
opts.paths = {}
var srcs = [];
var bowerJsonPath = opts.paths.bowerJson || "./bower.json";
var bowerrcPath = opts.paths.bowerrc || "./.bowerrc";
var bowerDirectory = "./bower_components";
if(fs.existsSync(bowerrcPath)){
bowerDirectory = path.dirname(bowerrcPath);
bowerDirectory = path.join(bowerDirectory, "/", (JSON.parse(fs.readFileSync(bowerrcPath))).directory);
}
if(!bowerJsonPath || !fs.existsSync(bowerJsonPath)){
throw new PluginError(PLUGIN_NAME, "bower.json file does not exist at "+bowerJsonPath);
}
if(!bowerDirectory || !fs.existsSync(bowerDirectory)){
throw new PluginError(PLUGIN_NAME, "Bower components directory does not exist at "+bowerDirectory);
}
try {
var bowerJson = JSON.parse(fs.readFileSync(bowerJsonPath));
} catch (e) {
throw new PluginError(PLUGIN_NAME, "The bower.json file at " + bowerJsonPath + " is not valid JSON. ");
}
if(!bowerJson.dependencies){
throw new PluginError(PLUGIN_NAME, "The project bower.json has no dependencies listed. This plugin has nothing to do!");
}
var packageJson = bowerJson.overrides || {};
for(var dependency in bowerJson.dependencies){
var dependencyConfig = packageJson[dependency] || {};
if(dependencyConfig.ignore === true){
continue;
}
dependencyConfig.name = dependency;
dependencyConfig.basePath = dependencyConfig.basePath || path.join(bowerDirectory, dependency);
dependencyConfig.main = dependencyConfig.main || readMainFilesFromDependency(dependencyConfig);
addToSrcs(srcs, dependencyConfig.basePath, dependencyConfig.main);
}
return gulp.src(srcs);
}
module.exports = gulpBowerFiles