@@ -5,16 +5,17 @@ const download = require('download');
55const decompress = require ( 'decompress' ) ;
66const unzip = require ( 'decompress-unzip' ) ;
77const untargz = require ( 'decompress-targz' ) ;
8+ const untarbz2 = require ( 'decompress-tarbz2' ) ;
89
910process . on ( 'unhandledRejection' , ( reason , _ ) => {
10- shell . echo ( String ( reason ) ) ;
11- shell . exit ( 1 ) ;
12- throw reason ;
11+ shell . echo ( String ( reason ) ) ;
12+ shell . exit ( 1 ) ;
13+ throw reason ;
1314} ) ;
14- process . on ( 'uncaughtException' , error => {
15- shell . echo ( String ( error ) ) ;
16- shell . exit ( 1 ) ;
17- throw error ;
15+ process . on ( 'uncaughtException' , ( error ) => {
16+ shell . echo ( String ( error ) ) ;
17+ shell . exit ( 1 ) ;
18+ throw error ;
1819} ) ;
1920
2021/**
@@ -23,98 +24,108 @@ process.on('uncaughtException', error => {
2324 * @param filePrefix {string} Prefix of the file name found in the archive
2425 * @param force {boolean} Whether to download even if the target file exists. `false` by default.
2526 */
26- exports . downloadUnzipFile = async ( url , targetFile , filePrefix , force = false ) => {
27- if ( fs . existsSync ( targetFile ) && ! force ) {
28- shell . echo ( `Skipping download because file already exists: ${ targetFile } ` ) ;
29- return ;
30- }
31- if ( ! fs . existsSync ( path . dirname ( targetFile ) ) ) {
32- if ( shell . mkdir ( '-p' , path . dirname ( targetFile ) ) . code !== 0 ) {
33- shell . echo ( 'Could not create new directory.' ) ;
34- shell . exit ( 1 ) ;
35- }
27+ exports . downloadUnzipFile = async (
28+ url ,
29+ targetFile ,
30+ filePrefix ,
31+ force = false
32+ ) => {
33+ if ( fs . existsSync ( targetFile ) && ! force ) {
34+ shell . echo ( `Skipping download because file already exists: ${ targetFile } ` ) ;
35+ return ;
36+ }
37+ if ( ! fs . existsSync ( path . dirname ( targetFile ) ) ) {
38+ if ( shell . mkdir ( '-p' , path . dirname ( targetFile ) ) . code !== 0 ) {
39+ shell . echo ( 'Could not create new directory.' ) ;
40+ shell . exit ( 1 ) ;
3641 }
42+ }
3743
38- const downloads = path . join ( __dirname , '..' , 'downloads' ) ;
39- if ( shell . rm ( '-rf' , targetFile , downloads ) . code !== 0 ) {
40- shell . exit ( 1 ) ;
41- }
44+ const downloads = path . join ( __dirname , '..' , 'downloads' ) ;
45+ if ( shell . rm ( '-rf' , targetFile , downloads ) . code !== 0 ) {
46+ shell . exit ( 1 ) ;
47+ }
4248
43- shell . echo ( `>>> Downloading from '${ url } '...` ) ;
44- const data = await download ( url ) ;
45- shell . echo ( `<<< Download succeeded.` ) ;
49+ shell . echo ( `>>> Downloading from '${ url } '...` ) ;
50+ const data = await download ( url ) ;
51+ shell . echo ( `<<< Download succeeded.` ) ;
4652
47- shell . echo ( '>>> Decompressing...' ) ;
48- const files = await decompress ( data , downloads , {
49- plugins : [
50- unzip ( ) ,
51- untargz ( )
52- ]
53- } ) ;
54- if ( files . length === 0 ) {
55- shell . echo ( 'Error ocurred while decompressing the archive.' ) ;
56- shell . exit ( 1 ) ;
57- }
58- const fileIndex = files . findIndex ( f => f . path . startsWith ( filePrefix ) ) ;
59- if ( fileIndex === - 1 ) {
60- shell . echo ( `The downloaded artifact does not contain any file with prefix ${ filePrefix } .` ) ;
61- shell . exit ( 1 ) ;
62- }
63- shell . echo ( '<<< Decompressing succeeded.' ) ;
53+ shell . echo ( '>>> Decompressing...' ) ;
54+ const files = await decompress ( data , downloads , {
55+ plugins : [ unzip ( ) , untargz ( ) , untarbz2 ( ) ] ,
56+ } ) ;
57+ if ( files . length === 0 ) {
58+ shell . echo ( 'Error ocurred while decompressing the archive.' ) ;
59+ shell . exit ( 1 ) ;
60+ }
61+ const fileIndex = files . findIndex ( ( f ) => f . path . startsWith ( filePrefix ) ) ;
62+ if ( fileIndex === - 1 ) {
63+ shell . echo (
64+ `The downloaded artifact does not contain any file with prefix ${ filePrefix } .`
65+ ) ;
66+ shell . exit ( 1 ) ;
67+ }
68+ shell . echo ( '<<< Decompressing succeeded.' ) ;
6469
65- if ( shell . mv ( '-f' , path . join ( downloads , files [ fileIndex ] . path ) , targetFile ) . code !== 0 ) {
66- shell . echo ( `Could not move file to target path: ${ targetFile } ` ) ;
67- shell . exit ( 1 ) ;
68- }
69- if ( ! fs . existsSync ( targetFile ) ) {
70- shell . echo ( `Could not find file: ${ targetFile } ` ) ;
71- shell . exit ( 1 ) ;
72- }
73- shell . echo ( `Done: ${ targetFile } ` ) ;
74- }
70+ if (
71+ shell . mv ( '-f' , path . join ( downloads , files [ fileIndex ] . path ) , targetFile )
72+ . code !== 0
73+ ) {
74+ shell . echo ( `Could not move file to target path: ${ targetFile } ` ) ;
75+ shell . exit ( 1 ) ;
76+ }
77+ if ( ! fs . existsSync ( targetFile ) ) {
78+ shell . echo ( `Could not find file: ${ targetFile } ` ) ;
79+ shell . exit ( 1 ) ;
80+ }
81+ shell . echo ( `Done: ${ targetFile } ` ) ;
82+ } ;
7583
7684/**
7785 * @param url {string} Download URL
7886 * @param targetDir {string} Directory into which to decompress the archive
7987 * @param targetFile {string} Path to the main file expected after decompressing
8088 * @param force {boolean} Whether to download even if the target file exists
8189 */
82- exports . downloadUnzipAll = async ( url , targetDir , targetFile , force , decompressOptions = undefined ) => {
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- }
90+ exports . downloadUnzipAll = async (
91+ url ,
92+ targetDir ,
93+ targetFile ,
94+ force ,
95+ decompressOptions = undefined
96+ ) => {
97+ if ( fs . existsSync ( targetFile ) && ! force ) {
98+ shell . echo ( `Skipping download because file already exists: ${ targetFile } ` ) ;
99+ return ;
100+ }
101+ if ( ! fs . existsSync ( targetDir ) ) {
102+ if ( shell . mkdir ( '-p' , targetDir ) . code !== 0 ) {
103+ shell . echo ( 'Could not create new directory.' ) ;
104+ shell . exit ( 1 ) ;
92105 }
106+ }
93107
94- shell . echo ( `>>> Downloading from '${ url } '...` ) ;
95- const data = await download ( url ) ;
96- shell . echo ( `<<< Download succeeded.` ) ;
108+ shell . echo ( `>>> Downloading from '${ url } '...` ) ;
109+ const data = await download ( url ) ;
110+ shell . echo ( `<<< Download succeeded.` ) ;
97111
98- shell . echo ( '>>> Decompressing...' ) ;
99- let options = {
100- plugins : [
101- unzip ( ) ,
102- untargz ( )
103- ]
104- } ;
105- if ( decompressOptions ) {
106- options = Object . assign ( options , decompressOptions )
107- }
108- const files = await decompress ( data , targetDir , options ) ;
109- if ( files . length === 0 ) {
110- shell . echo ( 'Error ocurred while decompressing the archive.' ) ;
111- shell . exit ( 1 ) ;
112- }
113- shell . echo ( '<<< Decompressing succeeded.' ) ;
112+ shell . echo ( '>>> Decompressing...' ) ;
113+ let options = {
114+ plugins : [ unzip ( ) , untargz ( ) , untarbz2 ( ) ] ,
115+ } ;
116+ if ( decompressOptions ) {
117+ options = Object . assign ( options , decompressOptions ) ;
118+ }
119+ const files = await decompress ( data , targetDir , options ) ;
120+ if ( files . length === 0 ) {
121+ shell . echo ( 'Error ocurred while decompressing the archive.' ) ;
122+ shell . exit ( 1 ) ;
123+ }
124+ shell . echo ( '<<< Decompressing succeeded.' ) ;
114125
115- if ( ! fs . existsSync ( targetFile ) ) {
116- shell . echo ( `Could not find file: ${ targetFile } ` ) ;
117- shell . exit ( 1 ) ;
118- }
119- shell . echo ( `Done: ${ targetFile } ` ) ;
120- }
126+ if ( ! fs . existsSync ( targetFile ) ) {
127+ shell . echo ( `Could not find file: ${ targetFile } ` ) ;
128+ shell . exit ( 1 ) ;
129+ }
130+ shell . echo ( `Done: ${ targetFile } ` ) ;
131+ } ;
0 commit comments