Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80657 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
/**
18
* A map holding resource by id
19
* @param {Array.<Resource>} resources
20
*/
21
function ResourceMap(resources, typeToMap) {
22
this.resourceCache = null;
23
this.inferredProjectPaths = null;
24
this.configurationTrie = null;
25
this.resourceMap = {};
26
this.resourcePathMap = {};
27
this.typeToMap = typeToMap || {};
28
this.inferredProjectPaths = null;
29
resources && resources.forEach(this.addResource, this);
30
}
31
32
ResourceMap.prototype.getResource = function(type, id) {
33
type = this.typeToMap[type] || type;
34
var typeMap = this.resourceMap[type];
35
return typeMap && typeMap[id];
36
};
37
38
39
/**
40
* Node-haste allows defining arbitrary search paths, and will recurse
41
* directories to find files/projects. Think of it like a shortcut to having to
42
* manually set up all the NODE_PATH variables every time you add a new project
43
* somewhere on the file system. This function extracts out a list of those
44
* automatically created project roots.
45
*
46
* WARNING: Do not call this frequently, only once/twice per entire postProcess,
47
* definitely never on a single module load.
48
*
49
* @param {ResourceMap} resourceMap Resource map to extract project paths from.
50
* @return {Array<string>} List of absolute paths to project roots that are
51
* inferred from loaded resources that assume the role of a "project".
52
*/
53
ResourceMap.prototype.getAllInferredProjectPaths = function() {
54
if (!this.inferredProjectPaths) {
55
var found = {};
56
this.getAllResources().forEach(function(resource) {
57
if (resource.getInferredProjectPath) {
58
found[resource.getInferredProjectPath()] = true;
59
}
60
}, this);
61
this.inferredProjectPaths = Object.keys(found);
62
}
63
return this.inferredProjectPaths;
64
};
65
66
67
ResourceMap.prototype.getConfigurationForResource = function(resource) {
68
return this.getConfigurationByPath(resource.path);
69
};
70
71
ResourceMap.prototype.getConfigurationByPath = function(path) {
72
if (!this.configurationTrie) {
73
var ConfigurationTrie = require('./ConfigurationTrie');
74
this.configurationTrie = new ConfigurationTrie(
75
this.getAllResourcesByType('ProjectConfiguration'));
76
}
77
return this.configurationTrie.findConfiguration(path);
78
};
79
80
ResourceMap.prototype.getResourceByPath = function(path) {
81
return this.resourcePathMap[path];
82
};
83
84
ResourceMap.prototype.getAllResources = function() {
85
if (!this.resourceCache) {
86
var cache = [];
87
var map = this.resourcePathMap;
88
Object.keys(map).forEach(function(k) {
89
map[k] && cache.push(map[k]);
90
}, this);
91
this.resourceCache = cache;
92
}
93
return this.resourceCache;
94
};
95
96
ResourceMap.prototype.getAllResourcesByType = function(type) {
97
type = this.typeToMap[type] || type;
98
if (!this.resourceMap[type]) {
99
return [];
100
}
101
return Object.keys(this.resourceMap[type]).map(function(key) {
102
return this.resourceMap[type][key];
103
}, this).filter(function(r) {
104
return r;
105
});
106
};
107
108
ResourceMap.prototype.addResource = function(resource) {
109
this.configurationTrie = this.resourceCache = null;
110
this.inferredProjectPaths = null;
111
var type = this.typeToMap[resource.type] || resource.type;
112
if (!this.resourceMap[type]) {
113
this.resourceMap[type] = {};
114
}
115
this.resourcePathMap[resource.path] = resource;
116
this.resourceMap[type][resource.id] = resource;
117
};
118
119
ResourceMap.prototype.updateResource = function(oldResource, newResource) {
120
this.configurationTrie = this.resourceCache = null;
121
this.inferredProjectPaths = null;
122
this.removeResource(oldResource);
123
this.addResource(newResource);
124
};
125
126
ResourceMap.prototype.removeResource = function(resource) {
127
var type = this.typeToMap[resource.type] || resource.type;
128
this.configurationTrie = this.resourceCache = null;
129
this.inferredProjectPaths = null;
130
this.resourcePathMap[resource.path] = undefined;
131
if (this.resourceMap[type] && this.resourceMap[type][resource.id]) {
132
this.resourceMap[type][resource.id] = undefined;
133
}
134
};
135
136
137
module.exports = ResourceMap;
138
139