-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (68 loc) · 2.23 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
var EPMSource = require("./epm");
var NPMSource = require("./npm");
var FSSource = require("./fs");
var whilst = require("async/whilst");
var contract = require("moxie-contract");
var expect = require("moxie-expect");
var provision = require("moxie-provisioner");
function Resolver(options) {
expect.options(options, [
"working_directory",
"contracts_build_directory",
]);
this.options = options;
this.sources = [
new EPMSource(options.working_directory, options.contracts_build_directory),
new NPMSource(options.working_directory),
new FSSource(options.working_directory, options.contracts_build_directory)
];
};
// This function might be doing too much. If so, too bad (for now).
Resolver.prototype.require = function(import_path, search_path) {
var self = this;
for (var i = 0; i < this.sources.length; i++) {
var source = this.sources[i];
var result = source.require(import_path, search_path);
if (result) {
var abstraction = contract(result);
provision(abstraction, self.options);
return abstraction;
}
}
throw new Error("Could not find artifacts for " + import_path + " from any sources");
};
Resolver.prototype.resolve = function(import_path, imported_from, callback) {
var self = this;
if (typeof imported_from == "function") {
callback = imported_from;
imported_from = null;
}
var resolved_body = null;
var resolved_path = null;
var current_index = -1;
var current_source;
whilst(function() {
return !resolved_body && current_index < self.sources.length - 1;
}, function(next) {
current_index += 1;
current_source = self.sources[current_index];
current_source.resolve(import_path, imported_from, function(err, body, file_path) {
if (!err && body) {
resolved_body = body;
resolved_path = file_path;
}
next(err);
});
}, function(err) {
if (err) return callback(err);
if (!resolved_body) {
var message = "Could not find " + import_path + " from any sources";
if (imported_from) {
message += "; imported from " + imported_from;
}
return callback(new Error(message));
}
callback(null, resolved_body, resolved_path, current_source);
})
};
module.exports = Resolver;