1
1
const axios = require ( 'axios' ) ;
2
- const zip = require ( 'cross-zip' ) ;
3
2
const fs = require ( 'fs' ) . promises ;
4
3
const path = require ( 'path' ) ;
4
+ const download = require ( 'download' ) ;
5
5
const packageJson = require ( './package.json' ) ;
6
- const { pipeline } = require ( 'stream/promises' ) ;
7
6
8
7
let networkName = 'mainnet' ;
9
8
let networkId = 1 ;
@@ -92,25 +91,9 @@ module.exports = {
92
91
} ,
93
92
download : async ( url , destination ) => {
94
93
try {
95
- const writer = await pipeline ( fs . createWriteStream ( destination ) ) ;
96
- const result = await axios ( {
97
- url,
98
- method : 'GET' ,
99
- responseType : 'stream' ,
100
- } ) ;
101
- await new Promise ( resolve =>
102
- result . data . pipe ( writer ) . on ( 'finish' , resolve ) ,
103
- ) ;
104
- } catch ( e ) {
105
- throw new Error (
106
- `[download] Unable to download metamask release from: ${ url } to: ${ destination } ` ,
107
- e ,
108
- ) ;
109
- }
110
- } ,
111
- extract : async ( file , destination ) => {
112
- try {
113
- await zip . unzip ( file , destination ) ;
94
+ // todo: check if zip file exists, if yes then remove it before
95
+ // todo: if directory exists, dont overwrite
96
+ await download ( url , destination , { extract : true } ) ;
114
97
} catch ( e ) {
115
98
throw new Error (
116
99
`[download] Unable to download metamask release from: ${ url } to: ${ destination } with following error:\n${ e } ` ,
@@ -120,13 +103,9 @@ module.exports = {
120
103
prepareMetamask : async version => {
121
104
const release = await module . exports . getMetamaskReleases ( version ) ;
122
105
const downloadsDirectory = path . resolve ( __dirname , 'downloads' ) ;
123
- if ( ! fs . existsSync ( downloadsDirectory ) ) {
124
- fs . mkdirSync ( downloadsDirectory ) ;
125
- }
126
- const downloadDestination = path . join ( downloadsDirectory , release . filename ) ;
127
- await module . exports . download ( release . downloadUrl , downloadDestination ) ;
128
- const metamaskDirectory = path . join ( downloadsDirectory , 'metamask' ) ;
129
- await module . exports . extract ( downloadDestination , metamaskDirectory ) ;
106
+ await module . exports . dirExists ( downloadsDirectory ) ;
107
+ const metamaskDirectory = path . join ( downloadsDirectory , release . tagName ) ;
108
+ await module . exports . download ( release . downloadUrl , metamaskDirectory ) ;
130
109
return metamaskDirectory ;
131
110
} ,
132
111
} ;
0 commit comments