-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-graph.js
150 lines (135 loc) · 4 KB
/
module-graph.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
/**
* Copyright 2012 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Module for module graphs and dependency calculation.
*/
var fs = require('fs');
/**
* Make a ModuleGraph from an object as it is output by closure compiler
* when passed the TODO compiler flag.
* @param {Array.<Object>} modules Array of module definitions.
* @return {ModuleGraph}
*/
exports.fromSerialization = function(modules) {
return new ModuleGraph(modules);
};
// TODO: Should this exist. Nice for convenience, but there should almost
// certainly not be more data access methods in this module.
/**
* Make a ModuleGraph from a file as it is output by closure compiler
* when passed the TODO compiler flag.
* @param {string} filename Filename of the module graph file.
* @apram {function(Error, ModuleGraph)}
* @return {ModuleGraph}
*/
exports.fromFilename = function(filename, cb) {
fs.readFile(filename, 'utf8', function(err, str) {
if (err) {
return cb(err);
}
cb(null, new ModuleGraph(JSON.parse(str)));
});
};
function Module(info) {
this.name = info.name;
this.inputs = info.inputs;
this.transitiveDeps = info['transitive-dependencies'].reverse();
}
Module.prototype.toString = function() {
return '<Module: ' + this.name + '>';
};
/**
* A module graph class.
* @constructor
*/
function ModuleGraph(moduleData) {
var self = this;
var modules = {};
var moduleNameList = [];
moduleData.forEach(function(info) {
var m = new Module(info);
modules[info.name] = m;
moduleNameList.push(info.name);
});
/**
* Get a list of all module names.
* @return {Array.<string>} List of module names.
*/
this.getAllModules = function() {
return moduleNameList;
};
/**
* Get transitive dependencies for a given module
* @param {string} name Name of the module.
* @return {Array.<string>} List of module names.
*/
this.getTransitiveDependencies = function(name) {
var m = getModule(name);
return m.transitiveDeps || [];
};
function getModule(name) {
var m = modules[name];
if (!m) {
throw new self.NotFoundException(name);
}
return m;
}
/**
* Error object used when a requested module cannot be found.
* @constructor
*/
this.NotFoundException = function(name) {
var msg = 'Unknown module: ' + name;
this.name = name;
this.message = msg;
Error.call(this, msg);
};
this.NotFoundException.prototype = new Error();
this.NotFoundException.prototype.statusCode = 404;
/**
* Gets the requested module names and their transitive deps minus the
* transitive deps (and the) of the optional excluded names.
* @param {Array.<string>} moduleNames List of module names.
* @apram {Array.<string>} opt_excludeNames List of module names to exclude.
* @return {Array.<string>} List of module names.
*/
this.getModules = function(moduleNames, opt_excludeNames) {
var list = [];
var seen = {};
var exclude = {};
if (opt_excludeNames) {
opt_excludeNames.forEach(function(name) {
exclude[name] = true;
var m = getModule(name);
m.transitiveDeps.forEach(function(name) {
exclude[name] = true;
});
});
}
function addModule(name) {
if (!seen[name] && !exclude[name]) {
seen[name] = true;
list.push(name)
}
}
moduleNames.forEach(function(name) {
var m = getModule(name);
m.transitiveDeps.forEach(addModule);
addModule(m.name);
});
return list;
};
}