forked from eugeneware/bower-resolve
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
154 lines (129 loc) · 5.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
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
var path = require('path');
var fs = require('fs');
var path = require('path')
var bower = require('bower');
var bowerModules;
function readBowerModules(cb) {
bower.commands.list({map: true},{offline: module.exports.offline})
.on('end', function (map) {
bowerModules = map;
cb(null, map);
});
}
function bowerRequire(moduleName, options) {
if (typeof bowerModules === 'undefined') throw new Error('You must call the #init method first');
if (moduleName && moduleName in bowerModules.dependencies) {
var module = bowerModules.dependencies[moduleName];
if (module) {
var mainModule;
var pkgMeta = module.pkgMeta;
if (pkgMeta && pkgMeta.main) {
mainModule = Array.isArray(pkgMeta.main) ? pkgMeta.main.filter(function (file) { return /\.js$/.test(file); })[0] : pkgMeta.main;
} else {
// if 'main' wasn't specified by this component, let's try
// guessing that the main file is moduleName.js
mainModule = moduleName + '.js';
}
var fullModulePath = path.resolve(path.join(module.canonicalDir, mainModule));
return path.join(process.cwd(), path.relative(path.dirname(moduleName), fullModulePath));
}
}
}
function fastReadBowerModules(moduleArg, opts, cb){
//Seems hacky, but just wrap the async method. It increases code reuse and simplifies the library
//and should be fine since the algorithm is typically rarely used and already relatively quick.
setTimeout(function(){
cb(bowerResolveSync(moduleArg, opts));
}, 0)
}
function bowerResolveSync(moduleArg, opts){
var opts = opts || {},
moduleName = moduleArg,
fileExts = opts.extensions || ['js'], //if no extension on moduleArg, assume javascript
bowerDirRelPath = 'bower_components',
found = false,
basePath = opts.basedir ? path.resolve(process.cwd(), opts.basedir) : process.cwd(),
pathAsArr = basePath.split(/[\\\/]/),
returnPath,
basePath;
if(moduleName.split(/[\\\/]/).length > 1){
throw new Error('Bower resolve cannot resolve relative paths. Please pass a single filename with an optional extension')
}
//If extension type is on module is present, change moduleName and force that extension
if(containsExtension(moduleName, fileExts)){
moduleName = moduleName.split('.').slice(0, -1).join('.');
fileExts = [moduleName.split('.').pop()];
}
//traverse upwards checking for existence of bower identifiers at each level. Break when found
while(pathAsArr.length){
basePath = pathAsArr.join(path.sep);
var files = fs.readdirSync(basePath)
if(files.indexOf('bower.json') !== -1
|| files.indexOf('.bowerrc') !== -1
|| files.indexOf('bower_components') !== -1)
{
found = true;
if(files.indexOf('.bowerrc') !== -1){
var temp = fs.readFileSync(basePath + "/" + '.bowerrc');
if(temp) bowerDirRelPath = JSON.parse(temp).directory || bowerDirRelPath;
}
break;
}
pathAsArr.pop();
}
if(found){
//This is a niche case. Any consuming module must expect * to bower resolve to an array, not a string
if(moduleName === "*"){
returnPath = [];
var modules = fs.readdirSync([basePath, bowerDirRelPath].join('/'))
modules.forEach(function(thisModuleName){
returnPath.push(getModulePath(thisModuleName))
})
} else{
returnPath = getModulePath(moduleName);
}
function getModulePath(thisModuleName){
var moduleConfig;
if ( fs.existsSync( [basePath, bowerDirRelPath, thisModuleName, 'bower.json'].join('/') ) ) {
moduleConfig = fs.readFileSync([basePath, bowerDirRelPath, thisModuleName, 'bower.json'].join('/'));
} else {
moduleConfig = fs.readFileSync([basePath, bowerDirRelPath, thisModuleName, '.bower.json'].join('/'));
}
var relFilePath = thisModuleName + "." + fileExts[0];
if(moduleConfig){
moduleConfig = JSON.parse(moduleConfig).main;
if(typeof moduleConfig == 'object'){
var temp;
for(var j = 0; j < fileExts.length; j++){
temp = arrFind(moduleConfig, new RegExp("." + fileExts[j] + "$"));
if(temp){
relFilePath = temp;
break;
}
}
} else if(typeof moduleConfig === 'string'){
relFilePath = moduleConfig;
}
}
return path.join(basePath, bowerDirRelPath, thisModuleName, relFilePath);
}
}
return returnPath
}
function arrFind(arr, test){
for(var i = 0; i < arr.length; i++){
if(test.test(arr[i]))
return arr[i];
}
return null;
}
function containsExtension (moduleName, fileExts) {
var splittedName = moduleName.split('.');
var lastChunk = moduleName.split('.')[splittedName.length -1 ];
return arrFind(fileExts, new RegExp("." + lastChunk + "$"));
}
module.exports = bowerRequire;
module.exports.init = readBowerModules;
module.exports.fastRead = fastReadBowerModules;
module.exports.fastReadSync = bowerResolveSync;
module.exports.offline = false;