@@ -4,22 +4,23 @@ var fs = require('fs');
4
4
var os = require ( 'os' ) ;
5
5
var path = require ( 'path' ) ;
6
6
var process = require ( 'process' ) ;
7
- var syncRequest = require ( 'sync-request' ) ;
7
+ var admZip = require ( 'adm-zip' ) ;
8
+ const Downloader = require ( "nodejs-file-downloader" ) ;
8
9
9
10
var downloadPath = path . join ( __dirname , '_download' ) ;
10
11
var testPath = path . join ( __dirname , '_test' ) ;
11
12
12
- exports . run = function ( cl ) {
13
+ var run = function ( cl ) {
13
14
console . log ( '> ' + cl ) ;
14
15
var rc = exec ( cl ) . code ;
15
16
if ( rc !== 0 ) {
16
17
echo ( 'Exec failed with rc ' + rc ) ;
17
18
exit ( rc ) ;
18
19
}
19
20
}
20
- var run = exports . run ;
21
+ exports . run = run ;
21
22
22
- exports . getExternals = function ( ) {
23
+ const getExternalsAsync = async ( ) => {
23
24
if ( process . env [ 'TF_BUILD' ] ) {
24
25
// skip adding node 5.10.1 to the PATH. the CI definition tests against node 5 and 6.
25
26
return ;
@@ -38,16 +39,19 @@ exports.getExternals = function () {
38
39
var nodeVersion = 'v16.13.0' ;
39
40
switch ( platform ) {
40
41
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' ) ;
42
43
addPath ( path . join ( nodeArchivePath , 'node-' + nodeVersion + '-darwin-x64' , 'bin' ) ) ;
43
44
break ;
44
45
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' ) ;
46
47
addPath ( path . join ( nodeArchivePath , 'node-' + nodeVersion + '-linux-x64' , 'bin' ) ) ;
47
48
break ;
48
49
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
+
51
55
var nodeDirectory = path . join ( testPath , 'node' ) ;
52
56
mkdir ( '-p' , nodeDirectory ) ;
53
57
cp ( nodeExePath , path . join ( nodeDirectory , 'node.exe' ) ) ;
@@ -57,83 +61,84 @@ exports.getExternals = function () {
57
61
}
58
62
}
59
63
60
- var downloadFile = function ( url ) {
64
+ exports . getExternalsAsync = getExternalsAsync
65
+
66
+
67
+ var downloadFileAsync = async function ( url , fileName ) {
61
68
// validate parameters
62
69
if ( ! url ) {
63
70
throw new Error ( 'Parameter "url" must be set.' ) ;
64
71
}
65
72
66
- // short-circuit if already downloaded
73
+ // skip if already downloaded
67
74
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 ) ;
69
79
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
+ }
72
84
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
+ }
77
90
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' ) ) ;
82
93
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
+ } ) ;
86
99
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
+ } ;
89
104
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
- }
96
105
97
- // validate parameters
106
+ var downloadArchiveAsync = async function ( url , fileName ) {
98
107
if ( ! url ) {
99
108
throw new Error ( 'Parameter "url" must be set.' ) ;
100
109
}
101
110
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 ;
104
115
}
105
-
106
- // short-circuit if already downloaded and extracted
107
- var scrubbedUrl = url . replace ( / [ / \: ? ] / g, '_' ) ;
108
116
var targetPath = path . join ( downloadPath , 'archive' , scrubbedUrl ) ;
109
117
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 ) ;
133
129
}
134
130
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
+
135
139
return targetPath ;
136
- }
140
+ } ;
141
+
137
142
138
143
var addPath = function ( directory ) {
139
144
var separator ;
0 commit comments