Skip to content

Commit 561944d

Browse files
Removed unsupported lib, added async methods (#932)
* Removed unsupported lib, added async methods * Update 3rd party library change to async * Format style in ThirdPartyNotice
1 parent 8e26b3e commit 561944d

File tree

11 files changed

+999
-527
lines changed

11 files changed

+999
-527
lines changed

node/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ Backported from ver.`3.4.0`:
4949

5050
- Add `getBoolFeatureFlag` [#936](https://github.com/microsoft/azure-pipelines-task-lib/pull/936)
5151

52+
5253
## 4.5.0
5354

5455
- Added `execAsync` methods that return native promises. Marked `exec` methods that return promises from the Q library as deprecated [#905](https://github.com/microsoft/azure-pipelines-task-lib/pull/905)
56+
57+
## 4.6.0
58+
59+
- Replaced deprecated "sync-request" lib and Added new Async methods - [#932](https://github.com/microsoft/azure-pipelines-task-lib/pull/932)

node/ThirdPartyNotice.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ This Azure Pipelines extension (azure-pipelines-task-lib) is based on or incorpo
3636
30. semver (git+https://github.com/npm/node-semver.git)
3737
31. shelljs (git://github.com/arturadib/shelljs.git)
3838
32. string_decoder (git://github.com/rvagg/string_decoder.git)
39-
33. sync-request (git+https://github.com/ForbesLindesay/sync-request.git)
39+
33. nodejs-file-downloader (git://github.com/ibrod83/nodejs-file-downloader.git)
4040
34. then-request (git+https://github.com/then/then-request.git)
4141
35. typedarray (git://github.com/substack/typedarray.git)
4242
36. typescript (git+https://github.com/Microsoft/TypeScript.git)
@@ -889,7 +889,8 @@ IN THE SOFTWARE.
889889
=========================================
890890
END OF string_decoder NOTICES, INFORMATION, AND LICENSE
891891

892-
%% sync-request NOTICES, INFORMATION, AND LICENSE BEGIN HERE
892+
893+
%% nodejs-file-downloader NOTICES, INFORMATION, AND LICENSE BEGIN HERE
893894
=========================================
894895
Copyright (c) 2014 Forbes Lindesay
895896

@@ -911,7 +912,8 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
911912
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
912913
THE SOFTWARE.
913914
=========================================
914-
END OF sync-request NOTICES, INFORMATION, AND LICENSE
915+
END OF nodejs-file-downloader NOTICES, INFORMATION, AND LICENSE
916+
915917

916918
%% then-request NOTICES, INFORMATION, AND LICENSE BEGIN HERE
917919
=========================================

node/buildutils.js

Lines changed: 79 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ var fs = require('fs');
44
var os = require('os');
55
var path = require('path');
66
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");
810

911
var downloadPath = path.join(__dirname, '_download');
1012
var 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
}
2022
var 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(/\.tar\.gz$/)) {
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

138156
var addPath = function (directory) {
139157
var separator;

0 commit comments

Comments
 (0)