forked from Synthetixio/synpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
96 lines (91 loc) · 2.94 KB
/
helpers.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
const axios = require('axios');
const fs = require('fs');
const zip = require('cross-zip');
const path = require('path');
let networkName = 'mainnet';
let networkId = 1;
let isTestnet = false;
module.exports = {
setNetwork: network => {
if (network === 'mainnet') {
networkName = 'mainnet';
networkId = 1;
isTestnet = false;
} else if (network === 'ropsten') {
networkName = 'ropsten';
networkId = 3;
isTestnet = true;
} else if (network === 'kovan') {
networkName = 'kovan';
networkId = 42;
isTestnet = true;
} else if (network === 'rinkeby') {
networkName = 'rinkeby';
networkId = 4;
isTestnet = true;
} else if (network === 'goerli') {
networkName = 'goerli';
networkId = 5;
isTestnet = true;
} else if (typeof network === 'object') {
networkName = network.networkName;
networkId = Number(network.chainId);
isTestnet = network.isTestnet;
}
// todo: handle a case when setNetwork() is triggered by changeNetwork() with a string of already added custom networks
},
getNetwork: () => {
return { networkName, networkId, isTestnet };
},
getSynpressPath: () => {
if (process.env.SYNPRESS_LOCAL_TEST) {
return '.';
} else {
return path.dirname(require.resolve(`@synthetixio/synpress`));
}
},
getMetamaskReleases: async version => {
let filename;
let downloadUrl;
const response = await axios.get(
'https://api.github.com/repos/metamask/metamask-extension/releases',
);
if (version === 'latest' || !version) {
filename = response.data[0].assets[0].name;
downloadUrl = response.data[0].assets[0].browser_download_url;
} else if (version) {
filename = `metamask-chrome-${version}.zip`;
downloadUrl = `https://github.com/MetaMask/metamask-extension/releases/download/v${version}/metamask-chrome-${version}.zip`;
}
return {
filename,
downloadUrl,
};
},
download: async (url, destination) => {
const writer = fs.createWriteStream(destination);
const result = await axios({
url,
method: 'GET',
responseType: 'stream',
});
await new Promise(resolve =>
result.data.pipe(writer).on('finish', resolve),
);
},
extract: async (file, destination) => {
await zip.unzip(file, destination);
},
prepareMetamask: async version => {
const release = await module.exports.getMetamaskReleases(version);
const downloadsDirectory = path.resolve(__dirname, 'downloads');
if (!fs.existsSync(downloadsDirectory)) {
fs.mkdirSync(downloadsDirectory);
}
const downloadDestination = path.join(downloadsDirectory, release.filename);
await module.exports.download(release.downloadUrl, downloadDestination);
const metamaskDirectory = path.join(downloadsDirectory, 'metamask');
await module.exports.extract(downloadDestination, metamaskDirectory);
return metamaskDirectory;
},
};