-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolc-manager.js
More file actions
66 lines (57 loc) · 1.82 KB
/
solc-manager.js
File metadata and controls
66 lines (57 loc) · 1.82 KB
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
var fs = require('fs');
var path = require('path');
var os = require('os');
let osType = os.type().toLowerCase();
const binaryDirectory = path.join(__dirname, 'bin');
const getBinaryDirectory = function () {
if (!fs.existsSync(binaryDirectory)) {
try {
fs.mkdirSync(binaryDirectory);
} catch(e) {
}
if (!osType.startsWith('windows')) {
try {
fs.chmodSync(binaryDirectory, '0777');
} catch(e) {
}
}
}
return binaryDirectory;
};
const getInstalledVersion = function () {
if (!fs.existsSync(getBinaryDirectory())) {
return [];
}
let files = fs.readdirSync(binaryDirectory);
const binFiles = files.filter((item) => {
return item.match(/^(solc-\d+\.\d+\.\d+)$/) || item.match(/^(solc-\d+\.\d+\.\d+.exe)$/);
});
const versions = binFiles.map((item) => {
if (item.endsWith(".exe")) {
return item.substring(5, item.length - 4);
}
return item.substring(5, item.length);
});
return versions;
};
const removeBinary = function (version) {
if (!fs.existsSync(binaryDirectory)) return;
fs.unlinkSync(path.join(binaryDirectory, binaryName(version)));
};
function binaryName (version) {
if (osType.startsWith('windows')) {
return 'solc-' + version + '.exe';
}
return 'solc-' + version;
}
function getBinary(version) {
var binaryLocation = path.join(binaryDirectory, binaryName(version));
if(osType.startsWith("windows") && hasBinaryVersion(version) && !fs.existsSync(binaryLocation)) {
binaryLocation = path.join(binaryDirectory, `solc-${version}`, "solc.exe");
}
return binaryLocation;
}
function hasBinaryVersion(version) {
return getInstalledVersion().filter(it => it.toLowerCase().indexOf(version) !== -1).length > 0;
}
module.exports = { getBinaryDirectory, hasBinaryVersion, getInstalledVersion, binaryName, removeBinary, getBinary };