Skip to content

Commit 45b9782

Browse files
authored
Introduce tl.getNodeMajorVersion (#979)
* Introduce getNodeMajorVersion * Update tests * Update version and changelog * Handle undefined Node version
1 parent cea7412 commit 45b9782

File tree

8 files changed

+38
-8
lines changed

8 files changed

+38
-8
lines changed

node/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,8 @@ Backported from ver.`3.4.0`:
5656

5757
## 4.6.0
5858

59-
- Replaced deprecated "sync-request" lib and Added new Async methods - [#932](https://github.com/microsoft/azure-pipelines-task-lib/pull/932)
59+
- Replaced deprecated "sync-request" lib and Added new Async methods - [#932](https://github.com/microsoft/azure-pipelines-task-lib/pull/932)
60+
61+
## 4.6.1
62+
63+
- Added `getNodeMajorVersion` [#979](https://github.com/microsoft/azure-pipelines-task-lib/pull/979)

node/Strings/resources.resjson/en-US/resources.resjson

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
"loc.messages.LIB_UseFirstGlobMatch": "Multiple workspace matches. using first.",
3232
"loc.messages.LIB_MergeTestResultNotSupported": "Merging test results from multiple files to one test run is not supported on this version of build agent for OSX/Linux, each test result file will be published as a separate test run in VSO/TFS.",
3333
"loc.messages.LIB_PlatformNotSupported": "Platform not supported: %s",
34-
"loc.messages.LIB_CopyFileFailed": "Error while copying the file. Attempts left: %s"
34+
"loc.messages.LIB_CopyFileFailed": "Error while copying the file. Attempts left: %s",
35+
"loc.messages.LIB_UndefinedNodeVersion": "Node version is undefined."
3536
}

node/lib.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"LIB_UseFirstGlobMatch": "Multiple workspace matches. using first.",
3333
"LIB_MergeTestResultNotSupported": "Merging test results from multiple files to one test run is not supported on this version of build agent for OSX/Linux, each test result file will be published as a separate test run in VSO/TFS.",
3434
"LIB_PlatformNotSupported": "Platform not supported: %s",
35-
"LIB_CopyFileFailed": "Error while copying the file. Attempts left: %s"
35+
"LIB_CopyFileFailed": "Error while copying the file. Attempts left: %s",
36+
"LIB_UndefinedNodeVersion": "Node version is undefined."
3637
}
3738
}

node/mock-answer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface TaskLibAnswers {
1414
find?: { [key: string]: string[] },
1515
findMatch?: { [key: string]: string[] },
1616
getPlatform?: { [key: string]: task.Platform },
17+
getNodeMajorVersion?: { [key: string]: Number },
1718
getAgentMode?: { [key: string]: task.AgentHostedMode },
1819
legacyFindFiles?: { [key: string]: string[] },
1920
ls?: { [key: string]: string },

node/mock-task.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ export function getPlatform(): task.Platform {
222222
return mock.getResponse('getPlatform', 'getPlatform', module.exports.debug);
223223
}
224224

225+
export function getNodeMajorVersion(): Number {
226+
return mock.getResponse('getNodeMajorVersion', 'getNodeMajorVersion', module.exports.debug);
227+
}
228+
225229
export function getAgentMode(): task.AgentHostedMode {
226230
return mock.getResponse('getAgentMode', 'getAgentMode', module.exports.debug);
227231
}

node/package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-pipelines-task-lib",
3-
"version": "4.6.0",
3+
"version": "4.6.1",
44
"description": "Azure Pipelines Task SDK",
55
"main": "./task.js",
66
"typings": "./task.d.ts",

node/task.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,24 @@ export function getPlatform(): Platform {
702702
}
703703
}
704704

705+
/**
706+
* Resolves major version of Node.js engine used by the agent.
707+
* @returns {Number} Node's major version.
708+
*/
709+
export function getNodeMajorVersion(): Number {
710+
const version = process?.versions?.node;
711+
if (!version) {
712+
throw new Error(loc('LIB_UndefinedNodeVersion'));
713+
}
714+
715+
const parts = version.split('.').map(Number);
716+
if (parts.length < 1) {
717+
return NaN;
718+
}
719+
720+
return parts[0];
721+
}
722+
705723
/**
706724
* Return hosted type of Agent
707725
* @returns {AgentHostedMode}

0 commit comments

Comments
 (0)