@@ -16,10 +16,18 @@ import path = require('path');
1616import fs = require( 'fs-extra' ) ;
1717import configData = require( '../src/tools.json' ) ;
1818
19- async function isDownloadRequired ( filePath , sha256 ) : Promise < boolean > {
19+ interface PlatformData {
20+ url : string ;
21+ sha256sum :string ;
22+ dlFileName :string ;
23+ cmdFileName : string ;
24+ filePrefix : string ;
25+ }
26+
27+ async function isDownloadRequired ( filePath : string , sha256 : string ) : Promise < boolean > {
2028 let result = true ;
2129 if ( fs . existsSync ( filePath ) ) {
22- const fileSha256 = await hasha . fromFile ( filePath , { algorithm : 'sha256' } ) ;
30+ const fileSha256 = await hasha . fromFile ( filePath , { algorithm : 'sha256' } ) ;
2331 result = fileSha256 !== sha256 ;
2432 }
2533 return result ;
@@ -28,51 +36,47 @@ async function isDownloadRequired(filePath, sha256): Promise<boolean> {
2836async function downloadFileAndCreateSha256 (
2937 toolsCacheFolder : string ,
3038 toolsFolder : string ,
31- dlFileName : string ,
32- url : string ,
33- sha256sum : string ,
34- cmdFileName : string ,
35- filePrefix ?: string ,
39+ platform : PlatformData ,
3640) : Promise < void > {
3741 mkdirp . sync ( toolsCacheFolder ) ;
3842 mkdirp . sync ( toolsFolder ) ;
39- const currentFile = path . join ( toolsCacheFolder , dlFileName ) ;
40- if ( await isDownloadRequired ( currentFile , sha256sum ) ) {
41- console . log ( `Downloading ${ url } to ${ currentFile } ` ) ;
42- await DownloadUtil . downloadFile ( url , currentFile , ( current ) => console . log ( `${ current } %` ) ) ;
43+ const currentFile = path . join ( toolsCacheFolder , platform . dlFileName ) ;
44+ if ( await isDownloadRequired ( currentFile , platform . sha256sum ) ) {
45+ console . log ( `Downloading ${ platform . url } to ${ currentFile } ` ) ;
46+ await DownloadUtil . downloadFile ( platform . url , currentFile , ( current ) =>
47+ console . log ( `${ current } %` ) ,
48+ ) ;
4349 const currentSHA256 = await hasha . fromFile ( currentFile , { algorithm : 'sha256' } ) ;
44- if ( currentSHA256 === sha256sum ) {
50+ if ( currentSHA256 === platform . sha256sum ) {
4551 console . log ( `Download of ${ currentFile } has finished and SHA256 is correct` ) ;
4652 } else {
4753 throw Error ( `${ currentFile } is downloaded and SHA256 is not correct` ) ;
4854 }
4955 } else {
50- console . log ( 'Previously downloaded archive SHA256 is correct' )
56+ console . log ( 'Previously downloaded archive SHA256 is correct' ) ;
5157 }
52- console . log ( `Extracting ${ currentFile } to ${ path . join ( toolsFolder , cmdFileName ) } ` ) ;
53- await Archive . extract ( currentFile , toolsFolder , cmdFileName , filePrefix ) ;
58+ console . log ( `Extracting ${ currentFile } to ${ path . join ( toolsFolder , platform . cmdFileName ) } ` ) ;
59+ await Archive . extract ( currentFile , toolsFolder , platform . cmdFileName , platform . filePrefix ) ;
5460}
5561
5662async function bundleTools ( ) : Promise < void > {
5763 const outFolder = path . resolve ( '.' , 'out' ) ;
5864 const toolsCacheFolder = path . join ( outFolder , 'tools-cache' ) ;
59- const currentPlatform = process . argv . find ( ( arg ) => arg === '--platform' ) ? process . platform : 'all' ;
65+ const currentPlatform = process . argv . find ( ( arg ) => arg === '--platform' )
66+ ? process . platform
67+ : 'all' ;
6068 console . log ( currentPlatform ) ;
6169 console . info ( `Download tools to '${ toolsCacheFolder } '` ) ;
6270 for ( const key in configData ) {
6371 const tool = configData [ key ] ;
64- for ( const OS in configData [ key ] . platform ) {
72+ for ( const OS in tool . platform ) {
6573 if ( currentPlatform === 'all' || OS === process . platform ) {
6674 console . log ( `Bundle '${ tool . description } ' for ${ OS } ` ) ;
6775 // eslint-disable-next-line no-await-in-loop
6876 await downloadFileAndCreateSha256 (
6977 toolsCacheFolder ,
7078 path . join ( outFolder , 'tools' , OS ) ,
71- configData [ key ] . platform [ OS ] . dlFileName ,
72- configData [ key ] . platform [ OS ] . url ,
73- configData [ key ] . platform [ OS ] . sha256sum ,
74- configData [ key ] . platform [ OS ] . cmdFileName ,
75- configData [ key ] . platform [ OS ] . filePrefix ,
79+ tool . platform [ OS ] ,
7680 ) ;
7781 }
7882 }
0 commit comments