@@ -4,12 +4,14 @@ var fs = require('fs');
44var os = require ( 'os' ) ;
55var path = require ( 'path' ) ;
66var process = require ( 'process' ) ;
7- var syncRequest = require ( 'sync-request' ) ;
7+ var admZip = require ( 'adm-zip' ) ;
8+ var deasync = require ( 'deasync' )
9+ const Downloader = require ( "nodejs-file-downloader" ) ;
810
911var downloadPath = path . join ( __dirname , '_download' ) ;
1012var testPath = path . join ( __dirname , '_test' ) ;
1113
12- exports . run = function ( cl ) {
14+ exports . run = function ( cl ) {
1315 console . log ( '> ' + cl ) ;
1416 var rc = exec ( cl ) . code ;
1517 if ( rc !== 0 ) {
@@ -19,7 +21,9 @@ exports.run = function(cl) {
1921}
2022var run = exports . run ;
2123
22- exports . getExternals = function ( ) {
24+
25+
26+ const getExternalsAsync = async ( ) => {
2327 if ( process . env [ 'TF_BUILD' ] ) {
2428 // skip adding node 5.10.1 to the PATH. the CI definition tests against node 5 and 6.
2529 return ;
@@ -38,16 +42,16 @@ exports.getExternals = function () {
3842 var nodeVersion = 'v16.13.0' ;
3943 switch ( platform ) {
4044 case 'darwin' :
41- var nodeArchivePath = downloadArchive ( nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-darwin-x64.tar.gz' ) ;
45+ var nodeArchivePath = await downloadArchiveAsync ( nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-darwin-x64.tar.gz' ) ;
4246 addPath ( path . join ( nodeArchivePath , 'node-' + nodeVersion + '-darwin-x64' , 'bin' ) ) ;
4347 break ;
4448 case 'linux' :
45- var nodeArchivePath = downloadArchive ( nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-linux-x64.tar.gz' ) ;
49+ var nodeArchivePath = await downloadArchiveAsync ( nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-linux-x64.tar.gz' ) ;
4650 addPath ( path . join ( nodeArchivePath , 'node-' + nodeVersion + '-linux-x64' , 'bin' ) ) ;
4751 break ;
4852 case 'win32' :
49- var nodeExePath = downloadFile ( nodeUrl + '/' + nodeVersion + '/win-x64/node.exe' ) ;
50- var nodeLibPath = downloadFile ( nodeUrl + '/' + nodeVersion + '/win-x64/node.lib' ) ;
53+ var nodeExePath = await downloadFileAsync ( nodeUrl + '/' + nodeVersion + '/win-x64/node.exe' ) ;
54+ var nodeLibPath = await downloadFileAsync ( nodeUrl + '/' + nodeVersion + '/win-x64/node.lib' ) ;
5155 var nodeDirectory = path . join ( testPath , 'node' ) ;
5256 mkdir ( '-p' , nodeDirectory ) ;
5357 cp ( nodeExePath , path . join ( nodeDirectory , 'node.exe' ) ) ;
@@ -57,83 +61,97 @@ exports.getExternals = function () {
5761 }
5862}
5963
60- var downloadFile = function ( url ) {
64+ exports . getExternalsAsync = getExternalsAsync
65+
66+
67+
68+ /**
69+ * @deprecated This method uses library which is not prefered to use on production
70+ */
71+ exports . getExternals = function ( ) {
72+ var result = false ;
73+ getExternalsAsync ( ) . then ( t => result = true ) ;
74+ deasync . loopWhile ( function ( ) { return ! result } ) ;
75+ return result ;
76+ }
77+
78+
79+ var downloadFileAsync = async function ( url , fileName ) {
6180 // validate parameters
6281 if ( ! url ) {
6382 throw new Error ( 'Parameter "url" must be set.' ) ;
6483 }
6584
66- // short-circuit if already downloaded
85+ // skip if already downloaded
6786 var scrubbedUrl = url . replace ( / [ / \: ? ] / g, '_' ) ;
68- var targetPath = path . join ( downloadPath , 'file' , scrubbedUrl ) ;
87+ if ( fileName == undefined ) {
88+ fileName = scrubbedUrl ;
89+ }
90+ var targetPath = path . join ( downloadPath , 'file' , fileName ) ;
6991 var marker = targetPath + '.completed' ;
70- if ( ! test ( '-f' , marker ) ) {
71- console . log ( 'Downloading file: ' + url ) ;
92+ if ( test ( '-f' , marker ) ) {
93+ console . log ( 'File already exists: ' + targetPath ) ;
94+ return targetPath ;
95+ }
7296
73- // delete any previous partial attempt
74- if ( test ( '-f' , targetPath ) ) {
75- rm ( '-f' , targetPath ) ;
76- }
97+ console . log ( 'Downloading file: ' + url ) ;
98+ // delete any previous partial attempt
99+ if ( test ( '-f' , targetPath ) ) {
100+ rm ( '-f' , targetPath ) ;
101+ }
77102
78- // download the file
79- mkdir ( '-p' , path . join ( downloadPath , 'file' ) ) ;
80- var result = syncRequest ( 'GET' , url ) ;
81- fs . writeFileSync ( targetPath , result . getBody ( ) ) ;
103+ // download the file
104+ mkdir ( '-p' , path . join ( downloadPath , 'file' ) ) ;
82105
83- // write the completed marker
84- fs . writeFileSync ( marker , '' ) ;
85- }
106+ const downloader = new Downloader ( {
107+ url : url ,
108+ directory : path . join ( downloadPath , 'file' ) ,
109+ fileName : fileName
110+ } ) ;
86111
87- return targetPath ;
88- }
112+ const { fileName : downloadedFileName } = await downloader . download ( ) ; // Downloader.download() resolves with some useful properties.
113+ fs . writeFileSync ( marker , '' ) ;
114+ return downloadedFileName ;
89115
90- var downloadArchive = function ( url ) {
91- // validate platform
92- var platform = os . platform ( ) ;
93- if ( platform != 'darwin' && platform != 'linux' ) {
94- throw new Error ( 'Unexpected platform: ' + platform ) ;
95- }
116+ } ;
96117
97- // validate parameters
118+
119+ var downloadArchiveAsync = async function ( url , fileName ) {
98120 if ( ! url ) {
99121 throw new Error ( 'Parameter "url" must be set.' ) ;
100122 }
101123
102- if ( ! url . match ( / \. t a r \. g z $ / ) ) {
103- throw new Error ( 'Expected .tar.gz' ) ;
124+ // skip if already downloaded and extracted
125+ var scrubbedUrl = url . replace ( / [ \/ \\ : ? ] / g, '_' ) ;
126+ if ( fileName != undefined ) {
127+ scrubbedUrl = fileName ;
104128 }
105-
106- // short-circuit if already downloaded and extracted
107- var scrubbedUrl = url . replace ( / [ / \: ? ] / g, '_' ) ;
108129 var targetPath = path . join ( downloadPath , 'archive' , scrubbedUrl ) ;
109130 var marker = targetPath + '.completed' ;
110- if ( ! test ( '-f' , marker ) ) {
111- // download the archive
112- var archivePath = downloadFile ( url ) ;
113- console . log ( 'Extracting archive: ' + url ) ;
114-
115- // delete any previously attempted extraction directory
116- if ( test ( '-d' , targetPath ) ) {
117- rm ( '-rf' , targetPath ) ;
118- }
119-
120- // extract
121- mkdir ( '-p' , targetPath ) ;
122- var cwd = process . cwd ( ) ;
123- process . chdir ( targetPath ) ;
124- try {
125- run ( 'tar -xzf "' + archivePath + '"' ) ;
126- }
127- finally {
128- process . chdir ( cwd ) ;
129- }
130-
131- // write the completed marker
132- fs . writeFileSync ( marker , '' ) ;
131+ if ( test ( '-f' , marker ) ) {
132+ return targetPath ;
133133 }
134+
135+ // download the archive
136+ var archivePath = await downloadFileAsync ( url , scrubbedUrl ) ;
137+ console . log ( 'Extracting archive: ' + url ) ;
138+
139+ // delete any previously attempted extraction directory
140+ if ( test ( '-d' , targetPath ) ) {
141+ rm ( '-rf' , targetPath ) ;
142+ }
143+
144+ // extract
145+ mkdir ( '-p' , targetPath ) ;
146+ var zip = new admZip ( archivePath ) ;
147+ zip . extractAllTo ( targetPath ) ;
148+
149+ // write the completed marker
150+ fs . writeFileSync ( marker , '' ) ;
134151
135152 return targetPath ;
136- }
153+ } ;
154+
137155
138156var addPath = function ( directory ) {
139157 var separator ;
0 commit comments