Skip to content

Commit 79bc298

Browse files
author
Maxim Zaytsev
authored
Remove deasync (#1038)
1 parent efde24b commit 79bc298

11 files changed

+87
-162
lines changed

node/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 4.x
44

5+
## 4.12.1
6+
7+
- Remove deasync from task-lib - [#1038](https://github.com/microsoft/azure-pipelines-task-lib/pull/1038)
8+
59
### 4.12.0
610

711
- Added audit action for task.issue [#1033](https://github.com/microsoft/azure-pipelines-task-lib/pull/1033)

node/buildutils.js

+11-24
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@ var os = require('os');
55
var path = require('path');
66
var process = require('process');
77
var admZip = require('adm-zip');
8-
var deasync = require('deasync')
98
const Downloader = require("nodejs-file-downloader");
109

1110
var downloadPath = path.join(__dirname, '_download');
1211
var testPath = path.join(__dirname, '_test');
1312

14-
exports.run = function (cl) {
13+
var run = function (cl) {
1514
console.log('> ' + cl);
1615
var rc = exec(cl).code;
1716
if (rc !== 0) {
1817
echo('Exec failed with rc ' + rc);
1918
exit(rc);
2019
}
2120
}
22-
var run = exports.run;
23-
24-
21+
exports.run = run;
2522

2623
const getExternalsAsync = async () => {
2724
if (process.env['TF_BUILD']) {
@@ -50,8 +47,11 @@ const getExternalsAsync = async () => {
5047
addPath(path.join(nodeArchivePath, 'node-' + nodeVersion + '-linux-x64', 'bin'));
5148
break;
5249
case 'win32':
53-
var nodeExePath = await downloadFileAsync(nodeUrl + '/' + nodeVersion + '/win-x64/node.exe');
54-
var nodeLibPath = await downloadFileAsync(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+
5555
var nodeDirectory = path.join(testPath, 'node');
5656
mkdir('-p', nodeDirectory);
5757
cp(nodeExePath, path.join(nodeDirectory, 'node.exe'));
@@ -64,18 +64,6 @@ const getExternalsAsync = async () => {
6464
exports.getExternalsAsync = getExternalsAsync
6565

6666

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-
7967
var downloadFileAsync = async function (url, fileName) {
8068
// validate parameters
8169
if (!url) {
@@ -84,7 +72,7 @@ var downloadFileAsync = async function (url, fileName) {
8472

8573
// skip if already downloaded
8674
var scrubbedUrl = url.replace(/[/\:?]/g, '_');
87-
if (fileName == undefined) {
75+
if (fileName === undefined) {
8876
fileName = scrubbedUrl;
8977
}
9078
var targetPath = path.join(downloadPath, 'file', fileName);
@@ -109,10 +97,9 @@ var downloadFileAsync = async function (url, fileName) {
10997
fileName: fileName
11098
});
11199

112-
const { fileName: downloadedFileName } = await downloader.download(); // Downloader.download() resolves with some useful properties.
100+
const { filePath } = await downloader.download(); // Downloader.download() resolves with some useful properties.
113101
fs.writeFileSync(marker, '');
114-
return downloadedFileName;
115-
102+
return filePath;
116103
};
117104

118105

@@ -123,7 +110,7 @@ var downloadArchiveAsync = async function (url, fileName) {
123110

124111
// skip if already downloaded and extracted
125112
var scrubbedUrl = url.replace(/[\/\\:?]/g, '_');
126-
if (fileName != undefined) {
113+
if (fileName !== undefined) {
127114
scrubbedUrl = fileName;
128115
}
129116
var targetPath = path.join(downloadPath, 'archive', scrubbedUrl);

node/make.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ target.build = function() {
3838
rm(path.join(buildPath, 'index.*'));
3939
}
4040

41-
target.test = function() {
41+
target.test = async function() {
4242
target.build();
4343

44-
buildutils.getExternals();
44+
await buildutils.getExternalsAsync();
4545
run('tsc -p ./test');
4646
cp('-Rf', rp('test/scripts'), testPath);
4747
cp('-Rf', rp('test/fakeTasks'), testPath);
@@ -65,3 +65,13 @@ target.loc = function() {
6565
var enContents = JSON.stringify(strings, null, 2);
6666
fs.writeFileSync(path.join(strPath, 'resources.resjson'), enContents)
6767
}
68+
69+
process.on('uncaughtException', err => {
70+
console.error(`Uncaught exception: ${err.message}`);
71+
console.debug(err.stack);
72+
});
73+
74+
process.on('unhandledRejection', err => {
75+
console.error(`Unhandled rejection: ${err.message}`);
76+
console.debug(err.stack);
77+
});

node/mock-test.ts

+14-23
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import cp = require('child_process');
22
import fs = require('fs');
3-
import ncp = require('child_process');
43
import os = require('os');
54
import path = require('path');
65
import cmdm = require('./taskcommand');
76
import shelljs = require('shelljs');
8-
import deasync = require('deasync')
97
const Downloader = require("nodejs-file-downloader");
108

119
const COMMAND_TAG = '[command]';
@@ -20,7 +18,6 @@ export class MockTestRunner {
2018

2119
this._taskJsonPath = taskJsonPath || '';
2220
this._testPath = testPath;
23-
this.nodePath = this.getNodePathSync();
2421
}
2522

2623
private _testPath = '';
@@ -34,14 +31,20 @@ export class MockTestRunner {
3431
public errorIssues: string[] = [];
3532
public warningIssues: string[] = [];
3633

37-
public async LoadAsync(testPath: string, taskJsonPath?: string): Promise<MockTestRunner> {
34+
public async LoadAsync(testPath?: string, taskJsonPath?: string): Promise<MockTestRunner> {
3835
return new Promise(async (resolve, reject) => {
3936
if (this.nodePath != '') {
4037
resolve(this);
4138
return;
4239
}
43-
this._taskJsonPath = taskJsonPath || '';
44-
this._testPath = testPath;
40+
if (testPath) {
41+
this._testPath = testPath;
42+
}
43+
44+
if (taskJsonPath) {
45+
this._taskJsonPath = taskJsonPath;
46+
}
47+
4548
this.nodePath = await this.getNodePath();
4649
resolve(this)
4750
})
@@ -82,7 +85,11 @@ export class MockTestRunner {
8285
let nodePath = this.nodePath;
8386
if (nodeVersion) {
8487
nodePath = await this.getNodePath(nodeVersion);
88+
} else if (!nodePath) {
89+
nodePath = await this.getNodePath();
90+
this.nodePath = nodePath;
8591
}
92+
8693
let spawn = cp.spawnSync(nodePath, [this._testPath]);
8794

8895
// Clean environment
@@ -155,15 +162,6 @@ export class MockTestRunner {
155162
}
156163
}
157164

158-
/**
159-
* @deprecated This method uses library which is not prefered to use on production
160-
*/
161-
public run(nodeVersion?: number): void {
162-
let completeExecution = false;
163-
this.runAsync(nodeVersion).then(t => completeExecution = true)
164-
deasync.loopWhile(function () { return !completeExecution })
165-
}
166-
167165
// Returns a path to node.exe with the correct version for this task (based on if its node10 or node)
168166
private async getNodePath(nodeVersion?: number): Promise<string> {
169167
const version: number = nodeVersion || this.getNodeVersion();
@@ -195,13 +193,6 @@ export class MockTestRunner {
195193
}
196194
}
197195

198-
private getNodePathSync(nodeVersion?: number): string {
199-
let nodePath = ''
200-
this.getNodePath(nodeVersion).then(t => nodePath = t);
201-
deasync.loopWhile(function () { return nodePath == ''; });
202-
return nodePath
203-
}
204-
205196
// Determines the correct version of node to use based on the contents of the task's task.json. Defaults to Node 16.
206197
private getNodeVersion(): number {
207198
const taskJsonPath: string = this.getTaskJsonPath();
@@ -321,7 +312,7 @@ export class MockTestRunner {
321312
const originalCwd: string = process.cwd();
322313
process.chdir(downloadDestination);
323314
try {
324-
ncp.execSync(`tar -xzf "${path.join(downloadDestination, tarGzName)}"`);
315+
cp.execSync(`tar -xzf "${path.join(downloadDestination, tarGzName)}"`);
325316
}
326317
catch {
327318
throw new Error('Failed to unzip node tar.gz from ' + url);

node/package-lock.json

+4-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-pipelines-task-lib",
3-
"version": "4.12.0",
3+
"version": "4.12.1",
44
"description": "Azure Pipelines Task SDK",
55
"main": "./task.js",
66
"typings": "./task.d.ts",
@@ -28,7 +28,6 @@
2828
"homepage": "https://github.com/Microsoft/azure-pipelines-task-lib",
2929
"dependencies": {
3030
"adm-zip": "^0.5.10",
31-
"deasync": "^0.1.28",
3231
"minimatch": "3.0.5",
3332
"nodejs-file-downloader": "^4.11.1",
3433
"q": "^1.5.1",

node/test/mocktests.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -298,34 +298,44 @@ describe('Mock Tests', function () {
298298
assert.equal(numStdErrCalls, 1);
299299
})
300300

301-
it('MockTest handles node 6 tasks correctly', function (done) {
301+
it('MockTest handles node 6 tasks correctly', async function () {
302302
this.timeout(30000);
303303
const runner = new mtm.MockTestRunner(path.join(__dirname, 'fakeTasks', 'node6task', 'entry.js'));
304+
await runner.LoadAsync();
304305
const nodePath = runner.nodePath;
305306
assert(nodePath, 'node path should have been correctly set');
306307
const version = ncp.execSync(nodePath + ' -v').toString().trim();
307308
assert(semver.satisfies(version, '6.x'), 'Downloaded node version should be Node 6 instead of ' + version);
308-
done();
309309
})
310310

311-
it('MockTest handles node 10 tasks correctly', function (done) {
311+
it('MockTest handles node 10 tasks correctly', async function () {
312312
this.timeout(30000);
313-
const runner = new mtm.MockTestRunner(path.join(__dirname, 'fakeTasks', 'node10task', 'entry.js'));
313+
const runner = new mtm.MockTestRunner();
314+
await runner.LoadAsync(path.join(__dirname, 'fakeTasks', 'node10task', 'entry.js'));
314315
const nodePath = runner.nodePath;
315316
assert(nodePath, 'node path should have been correctly set');
316317
const version = ncp.execSync(nodePath + ' -v').toString().trim();
317318
assert(semver.satisfies(version, '10.x'), 'Downloaded node version should be Node 10 instead of ' + version);
318-
done();
319319
})
320320

321-
it('MockTest handles node 16 tasks correctly', function (done) {
321+
it('MockTest handles node 16 tasks correctly', async function () {
322322
this.timeout(30000);
323323
const runner = new mtm.MockTestRunner(path.join(__dirname, 'fakeTasks', 'node16task', 'entry.js'));
324+
await runner.LoadAsync();
324325
const nodePath = runner.nodePath;
325326
assert(nodePath, 'node path should have been correctly set');
326327
const version = ncp.execSync(nodePath + ' -v').toString().trim();
327328
assert(semver.satisfies(version, '16.x'), 'Downloaded node version should be Node 16 instead of ' + version);
328-
done();
329+
})
330+
331+
it('MockTest handles node tasks correctly by async call', async function() {
332+
this.timeout(30000);
333+
const runner = await (new mtm.MockTestRunner).LoadAsync(path.join(__dirname, 'fakeTasks', 'node16task', 'entry.js'));
334+
const nodePath = runner.nodePath;
335+
assert(nodePath, 'node path should have been correctly set');
336+
const version = ncp.execSync(nodePath + ' -v').toString().trim();
337+
assert(semver.satisfies(version, '16.x'), 'Downloaded node version should be Node 16 instead of ' + version);
338+
await Promise.resolve()
329339
})
330340

331341
it('MockTest handles node tasks correctly by async call', async () => {

0 commit comments

Comments
 (0)