This repository has been 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 pathindex.js
104 lines (89 loc) · 3.06 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const path = require("path");
const fs = require("fs");
const semver = require("semver");
const { Docker, Local, Native, VersionRange } = require("./loadingStrategies");
class CompilerSupplier {
constructor(_config) {
_config = _config || {};
const defaultConfig = { version: "0.5.8" };
this.config = Object.assign({}, defaultConfig, _config);
this.strategyOptions = { version: this.config.version };
}
badInputError(userSpecification) {
const message =
`Could not find a compiler version matching ${userSpecification}. ` +
`compilers.solc.version option must be a string specifying:\n` +
` - a path to a locally installed solcjs\n` +
` - a solc version or range (ex: '0.4.22' or '^0.5.0')\n` +
` - a docker image name (ex: 'stable')\n` +
` - 'native' to use natively installed solc\n`;
return new Error(message);
}
async downloadAndCacheSolc(version) {
if (semver.validRange(version)) {
return await new VersionRange(this.strategyOptions).getSolcFromCacheOrUrl(
version
);
}
const message =
`You must specify a valid solc version to download` +
`Please ensure that the version you entered, ` +
`${version}, is valid.`;
throw new Error(message);
}
load() {
const userSpecification = this.config.version;
return new Promise(async (resolve, reject) => {
let strategy;
const useDocker = this.config.docker;
const useNative = userSpecification === "native";
const useSpecifiedLocal =
userSpecification && this.fileExists(userSpecification);
const isValidVersionRange = semver.validRange(userSpecification);
if (useDocker) {
strategy = new Docker(this.strategyOptions);
} else if (useNative) {
strategy = new Native(this.strategyOptions);
} else if (useSpecifiedLocal) {
strategy = new Local(this.strategyOptions);
} else if (isValidVersionRange) {
if (this.config.compilerRoots) {
this.strategyOptions.compilerRoots = this.config.compilerRoots;
}
strategy = new VersionRange(this.strategyOptions);
}
if (strategy) {
try {
const solc = await strategy.load(userSpecification);
resolve(solc);
} catch (error) {
reject(error);
}
} else {
reject(this.badInputError(userSpecification));
}
});
}
fileExists(localPath) {
return fs.existsSync(localPath) || path.isAbsolute(localPath);
}
getDockerTags() {
return new Docker(this.strategyOptions).getDockerTags();
}
getReleases() {
return new VersionRange(this.strategyOptions)
.getSolcVersions()
.then(list => {
const prereleases = list.builds
.filter(build => build["prerelease"])
.map(build => build["longVersion"]);
const releases = Object.keys(list.releases);
return {
prereleases: prereleases,
releases: releases,
latestRelease: list.latestRelease
};
});
}
}
module.exports = CompilerSupplier;