Skip to content

Commit 0ac03d3

Browse files
committed
chore: dont redownload metamask if its present
Signed-off-by: Jakub Mucha <[email protected]>
1 parent 79ad2c3 commit 0ac03d3

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

helpers.js

+30-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = {
4747
return path.dirname(require.resolve(packageJson.name));
4848
}
4949
},
50-
checkDirExist: async path => {
50+
createDirIfNotExist: async path => {
5151
try {
5252
await fs.access(path);
5353
} catch (e) {
@@ -60,6 +60,20 @@ module.exports = {
6060
}
6161
}
6262
},
63+
checkDirOrFileExist: async path => {
64+
try {
65+
await fs.access(path);
66+
return true;
67+
} catch (e) {
68+
if (e.code === 'ENOENT') {
69+
return false;
70+
} else {
71+
throw new Error(
72+
`[checkDirOrFileExist] Unhandled error from fs.access() with following error:\n${e}`,
73+
);
74+
}
75+
}
76+
},
6377
getMetamaskReleases: async version => {
6478
let filename;
6579
let downloadUrl;
@@ -91,8 +105,6 @@ module.exports = {
91105
},
92106
download: async (url, destination) => {
93107
try {
94-
// todo: check if zip file exists, if yes then remove it before
95-
// todo: if directory exists, dont overwrite
96108
await download(url, destination, { extract: true });
97109
} catch (e) {
98110
throw new Error(
@@ -103,9 +115,22 @@ module.exports = {
103115
prepareMetamask: async version => {
104116
const release = await module.exports.getMetamaskReleases(version);
105117
const downloadsDirectory = path.resolve(__dirname, 'downloads');
106-
await module.exports.dirExists(downloadsDirectory);
118+
await module.exports.createDirIfNotExist(downloadsDirectory);
107119
const metamaskDirectory = path.join(downloadsDirectory, release.tagName);
108-
await module.exports.download(release.downloadUrl, metamaskDirectory);
120+
const metamaskDirectoryExists = await module.exports.checkDirOrFileExist(
121+
metamaskDirectory,
122+
);
123+
const metamaskManifestFilePath = path.join(
124+
downloadsDirectory,
125+
release.tagName,
126+
'manifest.json',
127+
);
128+
const metamaskManifestFileExists = await module.exports.checkDirOrFileExist(
129+
metamaskManifestFilePath,
130+
);
131+
if (!metamaskDirectoryExists && !metamaskManifestFileExists) {
132+
await module.exports.download(release.downloadUrl, metamaskDirectory);
133+
}
109134
return metamaskDirectory;
110135
},
111136
};

0 commit comments

Comments
 (0)