@@ -17,9 +17,13 @@ process.on('uncaughtException', error => {
17
17
throw error ;
18
18
} ) ;
19
19
20
- exports . download = async ( url , targetFile , filePrefix , force ) => {
21
- const { platform, arch } = process ;
22
-
20
+ /**
21
+ * @param url {string} Download URL
22
+ * @param targetFile {string} Path to the file to copy from the decompressed archive
23
+ * @param filePrefix {string} Prefix of the file name found in the archive
24
+ * @param force {boolean} Whether to download even if the target file exists
25
+ */
26
+ exports . downloadUnzipFile = async ( url , targetFile , filePrefix , force ) => {
23
27
if ( fs . existsSync ( targetFile ) && ! force ) {
24
28
shell . echo ( `Skipping download because file already exists: ${ targetFile } ` ) ;
25
29
return ;
@@ -65,7 +69,48 @@ exports.download = async (url, targetFile, filePrefix, force) => {
65
69
if ( ! fs . existsSync ( targetFile ) ) {
66
70
shell . echo ( `Could not find file: ${ targetFile } ` ) ;
67
71
shell . exit ( 1 ) ;
68
- } else {
69
- shell . echo ( `Done: ${ targetFile } ` ) ;
70
72
}
73
+ shell . echo ( `Done: ${ targetFile } ` ) ;
74
+ }
75
+
76
+ /**
77
+ * @param url {string} Download URL
78
+ * @param targetDir {string} Directory into which to decompress the archive
79
+ * @param targetFile {string} Path to the main file expected after decompressing
80
+ * @param force {boolean} Whether to download even if the target file exists
81
+ */
82
+ exports . downloadUnzipAll = async ( url , targetDir , targetFile , force ) => {
83
+ if ( fs . existsSync ( targetFile ) && ! force ) {
84
+ shell . echo ( `Skipping download because file already exists: ${ targetFile } ` ) ;
85
+ return ;
86
+ }
87
+ if ( ! fs . existsSync ( targetDir ) ) {
88
+ if ( shell . mkdir ( '-p' , targetDir ) . code !== 0 ) {
89
+ shell . echo ( 'Could not create new directory.' ) ;
90
+ shell . exit ( 1 ) ;
91
+ }
92
+ }
93
+
94
+ shell . echo ( `>>> Downloading from '${ url } '...` ) ;
95
+ const data = await download ( url ) ;
96
+ shell . echo ( `<<< Download succeeded.` ) ;
97
+
98
+ shell . echo ( '>>> Decompressing...' ) ;
99
+ const files = await decompress ( data , targetDir , {
100
+ plugins : [
101
+ unzip ( ) ,
102
+ untargz ( )
103
+ ]
104
+ } ) ;
105
+ if ( files . length === 0 ) {
106
+ shell . echo ( 'Error ocurred while decompressing the archive.' ) ;
107
+ shell . exit ( 1 ) ;
108
+ }
109
+ shell . echo ( '<<< Decompressing succeeded.' ) ;
110
+
111
+ if ( ! fs . existsSync ( targetFile ) ) {
112
+ shell . echo ( `Could not find file: ${ targetFile } ` ) ;
113
+ shell . exit ( 1 ) ;
114
+ }
115
+ shell . echo ( `Done: ${ targetFile } ` ) ;
71
116
}
0 commit comments