-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.js
91 lines (87 loc) · 2.62 KB
/
component.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
var path = require('path');
var fs = require('fs');
var bower = require('bower');
function Component (data){
this.name = data.name;
this.version = data.version || "0.0.0";
if(data.main){
this.main = (typeof data.main === 'string' ? [data.main] : data.main);
} else {
data.main = [];
}
this.basedir = data.basedir || path.join(bower.config.directory, this.name);
this.assets = data.assets || [];
this.files = function(){
return this.main.concat(this.assets);
};
this.includes = {};
if ( data.includes ){
data.includes.forEach(function(inc){
if(!Component.dict[inc]){
Component.fromName(inc);
}
this.includes[inc] = Component.dict[inc];
this.includes[inc].includedIn = this;
}.bind(this));
}
this.dependencies = {};
if (data.dependencies) {
for (var dep in data.dependencies) {
this.dependencies[dep] = Component.fromName(dep);
}
}
}
Component.prototype = {
eachInclude: function(fn){
for(var inc in this.includes){
this.includes[inc].eachInclude(fn);
fn.call(this, this.includes[inc]);
}
},
eachDependency: function(fn){
for(var dep in this.dependencies){
this.dependencies[dep].eachDependency(fn);
fn.call(this, this.dependencies[dep]);
}
for(var inc in this.includes){
inc.eachDependency(fn);
}
}
};
Component.fromName = function(dep){
var depPath = path.join(bower.config.directory, dep);
var compPath = path.join(depPath, 'component.urturn.json');
var bowerPath = path.join(depPath, 'bower.json');
var legacyBowerPath = path.join(depPath, 'component.json');
if( fs.existsSync(compPath)) {
return Component.fromOptions(JSON.parse(fs.readFileSync(compPath)));
} else if(fs.existsSync(bowerPath)){
return Component.fromBower(dep, JSON.parse(fs.readFileSync(bowerPath)));
} else if (fs.existsSync(legacyBowerPath)){
return Component.fromBower(dep, JSON.parse(fs.readFileSync(legacyBowerPath)));
} else {
throw new Error("No json descriptor found for " + dep);
}
};
Component.fromOptions = function(info){
if(!Component.dict[info.name]){
Component.dict[info.name] = new Component(info);
Component.list.push(Component.dict[info.name]);
}
return Component.dict[info.name];
};
Component.fromBower = function(name, info){
if(!Component.dict[name]){
Component.dict[name] = new Component({
name: info.name,
version: info.version,
dependencies: info.dependencies || {},
main: info.main
});
Component.list.push(Component.dict[info.name]);
}
return Component.dict[name];
};
Component.list = [];
Component.dict = {};
module.exports = Component;