This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathresolver.ts
77 lines (64 loc) · 2.08 KB
/
resolver.ts
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
const contract = require("@truffle/contract");
const expect = require("@truffle/expect");
const provision = require("@truffle/provisioner");
import { ResolverSource } from "./source";
import { EthPMv1, NPM, GlobalNPM, FS, Truffle, ABI } from "./sources";
export class Resolver {
options: any;
sources: ResolverSource[];
constructor(options: any, includeTruffleSource: boolean = false) {
expect.options(options, ["working_directory", "contracts_build_directory"]);
this.options = options;
this.sources = [
new EthPMv1(options.working_directory),
new NPM(options.working_directory),
new GlobalNPM(),
new ABI(options.working_directory, options.contracts_build_directory),
new FS(options.working_directory, options.contracts_build_directory)
];
if (includeTruffleSource) {
this.sources.unshift(new Truffle(options));
}
}
// This function might be doing too much. If so, too bad (for now).
require(import_path: string, search_path: string) {
let abstraction;
this.sources.forEach((source: ResolverSource) => {
const result = source.require(import_path, search_path);
if (result) {
abstraction = contract(result);
provision(abstraction, this.options);
}
});
if (abstraction) return abstraction;
throw new Error(
"Could not find artifacts for " + import_path + " from any sources"
);
}
async resolve(
importPath: string,
importedFrom: string
): Promise<{ body: string; filePath: string; source: ResolverSource }> {
let body: string | null = null;
let filePath: string | null = null;
let source: ResolverSource | null = null;
for (source of this.sources) {
({ body, filePath } = await source.resolve(importPath, importedFrom));
if (body) {
break;
}
}
if (!body) {
let message = `Could not find ${importPath} from any sources`;
if (importedFrom) {
message += "; imported from " + importedFrom;
}
throw new Error(message);
}
return {
body,
filePath,
source
};
}
}