@@ -4,22 +4,23 @@ 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+ const Downloader = require ( "nodejs-file-downloader" ) ;
89
910var downloadPath = path . join ( __dirname , '_download' ) ;
1011var testPath = path . join ( __dirname , '_test' ) ;
1112
12- exports . run = function ( cl ) {
13+ var run = function ( cl ) {
1314 console . log ( '> ' + cl ) ;
1415 var rc = exec ( cl ) . code ;
1516 if ( rc !== 0 ) {
1617 echo ( 'Exec failed with rc ' + rc ) ;
1718 exit ( rc ) ;
1819 }
1920}
20- var run = exports . run ;
21+ exports . run = run ;
2122
22- exports . getExternals = function ( ) {
23+ const getExternalsAsync = async ( ) => {
2324 if ( process . env [ 'TF_BUILD' ] ) {
2425 // skip adding node 5.10.1 to the PATH. the CI definition tests against node 5 and 6.
2526 return ;
@@ -38,16 +39,19 @@ exports.getExternals = function () {
3839 var nodeVersion = 'v16.13.0' ;
3940 switch ( platform ) {
4041 case 'darwin' :
41- var nodeArchivePath = downloadArchive ( nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-darwin-x64.tar.gz' ) ;
42+ var nodeArchivePath = await downloadArchiveAsync ( nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-darwin-x64.tar.gz' ) ;
4243 addPath ( path . join ( nodeArchivePath , 'node-' + nodeVersion + '-darwin-x64' , 'bin' ) ) ;
4344 break ;
4445 case 'linux' :
45- var nodeArchivePath = downloadArchive ( nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-linux-x64.tar.gz' ) ;
46+ var nodeArchivePath = await downloadArchiveAsync ( nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-linux-x64.tar.gz' ) ;
4647 addPath ( path . join ( nodeArchivePath , 'node-' + nodeVersion + '-linux-x64' , 'bin' ) ) ;
4748 break ;
4849 case 'win32' :
49- var nodeExePath = downloadFile ( nodeUrl + '/' + nodeVersion + '/win-x64/node.exe' ) ;
50- var nodeLibPath = downloadFile ( nodeUrl + '/' + nodeVersion + '/win-x64/node.lib' ) ;
50+ var [ nodeExePath , nodeLibPath ] = await Promise . all ( [
51+ downloadFileAsync ( nodeUrl + '/' + nodeVersion + '/win-x64/node.exe' ) ,
52+ downloadFileAsync ( nodeUrl + '/' + nodeVersion + '/win-x64/node.lib' )
53+ ] ) ;
54+
5155 var nodeDirectory = path . join ( testPath , 'node' ) ;
5256 mkdir ( '-p' , nodeDirectory ) ;
5357 cp ( nodeExePath , path . join ( nodeDirectory , 'node.exe' ) ) ;
@@ -57,83 +61,84 @@ exports.getExternals = function () {
5761 }
5862}
5963
60- var downloadFile = function ( url ) {
64+ exports . getExternalsAsync = getExternalsAsync
65+
66+
67+ var downloadFileAsync = async function ( url , fileName ) {
6168 // validate parameters
6269 if ( ! url ) {
6370 throw new Error ( 'Parameter "url" must be set.' ) ;
6471 }
6572
66- // short-circuit if already downloaded
73+ // skip if already downloaded
6774 var scrubbedUrl = url . replace ( / [ / \: ? ] / g, '_' ) ;
68- var targetPath = path . join ( downloadPath , 'file' , scrubbedUrl ) ;
75+ if ( fileName === undefined ) {
76+ fileName = scrubbedUrl ;
77+ }
78+ var targetPath = path . join ( downloadPath , 'file' , fileName ) ;
6979 var marker = targetPath + '.completed' ;
70- if ( ! test ( '-f' , marker ) ) {
71- console . log ( 'Downloading file: ' + url ) ;
80+ if ( test ( '-f' , marker ) ) {
81+ console . log ( 'File already exists: ' + targetPath ) ;
82+ return targetPath ;
83+ }
7284
73- // delete any previous partial attempt
74- if ( test ( '-f' , targetPath ) ) {
75- rm ( '-f' , targetPath ) ;
76- }
85+ console . log ( 'Downloading file: ' + url ) ;
86+ // delete any previous partial attempt
87+ if ( test ( '-f' , targetPath ) ) {
88+ rm ( '-f' , targetPath ) ;
89+ }
7790
78- // download the file
79- mkdir ( '-p' , path . join ( downloadPath , 'file' ) ) ;
80- var result = syncRequest ( 'GET' , url ) ;
81- fs . writeFileSync ( targetPath , result . getBody ( ) ) ;
91+ // download the file
92+ mkdir ( '-p' , path . join ( downloadPath , 'file' ) ) ;
8293
83- // write the completed marker
84- fs . writeFileSync ( marker , '' ) ;
85- }
94+ const downloader = new Downloader ( {
95+ url : url ,
96+ directory : path . join ( downloadPath , 'file' ) ,
97+ fileName : fileName
98+ } ) ;
8699
87- return targetPath ;
88- }
100+ const { filePath } = await downloader . download ( ) ; // Downloader.download() resolves with some useful properties.
101+ fs . writeFileSync ( marker , '' ) ;
102+ return filePath ;
103+ } ;
89104
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- }
96105
97- // validate parameters
106+ var downloadArchiveAsync = async function ( url , fileName ) {
98107 if ( ! url ) {
99108 throw new Error ( 'Parameter "url" must be set.' ) ;
100109 }
101110
102- if ( ! url . match ( / \. t a r \. g z $ / ) ) {
103- throw new Error ( 'Expected .tar.gz' ) ;
111+ // skip if already downloaded and extracted
112+ var scrubbedUrl = url . replace ( / [ \/ \\ : ? ] / g, '_' ) ;
113+ if ( fileName !== undefined ) {
114+ scrubbedUrl = fileName ;
104115 }
105-
106- // short-circuit if already downloaded and extracted
107- var scrubbedUrl = url . replace ( / [ / \: ? ] / g, '_' ) ;
108116 var targetPath = path . join ( downloadPath , 'archive' , scrubbedUrl ) ;
109117 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 , '' ) ;
118+ if ( test ( '-f' , marker ) ) {
119+ return targetPath ;
120+ }
121+
122+ // download the archive
123+ var archivePath = await downloadFileAsync ( url , scrubbedUrl ) ;
124+ console . log ( 'Extracting archive: ' + url ) ;
125+
126+ // delete any previously attempted extraction directory
127+ if ( test ( '-d' , targetPath ) ) {
128+ rm ( '-rf' , targetPath ) ;
133129 }
134130
131+ // extract
132+ mkdir ( '-p' , targetPath ) ;
133+ var zip = new admZip ( archivePath ) ;
134+ zip . extractAllTo ( targetPath ) ;
135+
136+ // write the completed marker
137+ fs . writeFileSync ( marker , '' ) ;
138+
135139 return targetPath ;
136- }
140+ } ;
141+
137142
138143var addPath = function ( directory ) {
139144 var separator ;
0 commit comments