@@ -4,12 +4,14 @@ 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
+ var deasync = require ( 'deasync' )
9
+ const Downloader = require ( "nodejs-file-downloader" ) ;
8
10
9
11
var downloadPath = path . join ( __dirname , '_download' ) ;
10
12
var testPath = path . join ( __dirname , '_test' ) ;
11
13
12
- exports . run = function ( cl ) {
14
+ exports . run = function ( cl ) {
13
15
console . log ( '> ' + cl ) ;
14
16
var rc = exec ( cl ) . code ;
15
17
if ( rc !== 0 ) {
@@ -19,7 +21,9 @@ exports.run = function(cl) {
19
21
}
20
22
var run = exports . run ;
21
23
22
- exports . getExternals = function ( ) {
24
+
25
+
26
+ const getExternalsAsync = async ( ) => {
23
27
if ( process . env [ 'TF_BUILD' ] ) {
24
28
// skip adding node 5.10.1 to the PATH. the CI definition tests against node 5 and 6.
25
29
return ;
@@ -38,16 +42,16 @@ exports.getExternals = function () {
38
42
var nodeVersion = 'v16.13.0' ;
39
43
switch ( platform ) {
40
44
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' ) ;
42
46
addPath ( path . join ( nodeArchivePath , 'node-' + nodeVersion + '-darwin-x64' , 'bin' ) ) ;
43
47
break ;
44
48
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' ) ;
46
50
addPath ( path . join ( nodeArchivePath , 'node-' + nodeVersion + '-linux-x64' , 'bin' ) ) ;
47
51
break ;
48
52
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' ) ;
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,97 @@ exports.getExternals = function () {
57
61
}
58
62
}
59
63
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 ) {
61
80
// validate parameters
62
81
if ( ! url ) {
63
82
throw new Error ( 'Parameter "url" must be set.' ) ;
64
83
}
65
84
66
- // short-circuit if already downloaded
85
+ // skip if already downloaded
67
86
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 ) ;
69
91
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
+ }
72
96
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
+ }
77
102
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' ) ) ;
82
105
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
+ } ) ;
86
111
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 ;
89
115
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
+ } ;
96
117
97
- // validate parameters
118
+
119
+ var downloadArchiveAsync = async function ( url , fileName ) {
98
120
if ( ! url ) {
99
121
throw new Error ( 'Parameter "url" must be set.' ) ;
100
122
}
101
123
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 ;
104
128
}
105
-
106
- // short-circuit if already downloaded and extracted
107
- var scrubbedUrl = url . replace ( / [ / \: ? ] / g, '_' ) ;
108
129
var targetPath = path . join ( downloadPath , 'archive' , scrubbedUrl ) ;
109
130
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 ;
133
133
}
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 , '' ) ;
134
151
135
152
return targetPath ;
136
- }
153
+ } ;
154
+
137
155
138
156
var addPath = function ( directory ) {
139
157
var separator ;
0 commit comments