-
Notifications
You must be signed in to change notification settings - Fork 59
/
sample.ts
209 lines (184 loc) · 6.52 KB
/
sample.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import * as toolLib from './tool';
import * as taskLib from 'azure-pipelines-task-lib/task';
import * as restm from 'typed-rest-client/RestClient';
import * as os from 'os';
import * as path from 'path';
async function run() {
try {
//
// Get an explicit version
//
await getNode('v5.10.1', false);
//
// Query latest with a wildcard
//
await getNode('6.x', true);
//
// Complex versionSpecs are supported.
//
// For example:
// await getNode('9.x || >=4.7.0', true);
//
}
catch (error) {
taskLib.setResult(taskLib.TaskResult.Failed, error.message);
}
}
//
// Node versions interface
// see https://nodejs.org/dist/index.json
//
interface INodeVersion {
version: string,
files: string[]
}
let osPlat: string = os.platform();
let osArch: string = os.arch();
//
// Basic pattern:
// if !checkLatest
// toolPath = check cache
// if !toolPath
// if version is a range
// match = query nodejs.org
// if !match
// fail
// toolPath = check cache
// if !toolPath
// download, extract, and cache
// toolPath = cacheDir
// PATH = cacheDir + PATH
//
async function getNode(versionSpec: string, checkLatest: boolean) {
console.log('');
console.log('--------------------------');
console.log('SAMPLE: ' + versionSpec);
console.log('--------------------------');
if (toolLib.isExplicitVersion(versionSpec)) {
checkLatest = false; // check latest doesn't make sense when explicit version
}
let toolPath: string;
if (!checkLatest) {
//
// Let's try and resolve the version spec locally first
//
toolPath = toolLib.findLocalTool('node', versionSpec);
}
if (!toolPath) {
let version: string;
if (toolLib.isExplicitVersion(versionSpec)) {
//
// Explicit version was specified. No need to query for list of versions.
//
version = versionSpec;
}
else {
//
// Let's query and resolve the latest version for the versionSpec.
// If the version is an explicit version (1.1.1 or v1.1.1) then no need to query.
// If your tool doesn't offer a mechanism to query,
// then it can only support exact version inputs.
//
version = await queryLatestMatch(versionSpec);
if (!version) {
throw new Error(`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`);
}
//
// Check the cache for the resolved version.
//
toolPath = toolLib.findLocalTool('node', version)
}
if (!toolPath) {
//
// Download, extract, cache
//
toolPath = await acquireNode(version);
}
}
//
// A tool installer initimately knows details about the layout of that tool
// for example, node binary is in the bin folder after the extract on Mac/Linux.
// layouts could change by version, by platform etc... but that's the tool installers job
//
if (osPlat != 'win32') {
toolPath = path.join(toolPath, 'bin');
}
//
// Prepend the tools path. This prepends the PATH for the current process and
// instructs the agent to prepend for each task that follows.
//
toolLib.prependPath(toolPath);
}
async function queryLatestMatch(versionSpec: string): Promise<string> {
//
// Hopefully your tool supports an easy way to get a version list.
// Node offers a json list of versions.
//
let dataFileName: string;
switch (osPlat) {
case "linux": dataFileName = "linux-" + osArch; break;
case "darwin": dataFileName = "osx-" + osArch + '-tar'; break;
case "win32": dataFileName = "win-" + osArch + '-exe'; break;
default: throw new Error(`Unexpected OS '${osPlat}'`);
}
let versions: string[] = [];
let dataUrl = "https://nodejs.org/dist/index.json";
let rest: restm.RestClient = new restm.RestClient('vsts-node-tool');
let nodeVersions: INodeVersion[] = (await rest.get<INodeVersion[]>(dataUrl)).result;
nodeVersions.forEach((nodeVersion:INodeVersion) => {
//
// Ensure this version supports your os and platform.
//
if (nodeVersion.files.indexOf(dataFileName) >= 0) {
versions.push(nodeVersion.version);
}
});
//
// If there is no data driven way to get versions supported,
// a last option is to tool.scrape() with a regex.
//
// For example:
// let scrapeUrl = 'https://nodejs.org/dist/';
// let re: RegExp = /v(\d+\.)(\d+\.)(\d+)/g;
// versions = await toolLib.scrape(scrapeUrl, re);
//
//
// Get the latest version that matches the version spec.
//
let version: string = toolLib.evaluateVersions(versions, versionSpec);
return version;
}
async function acquireNode(version: string): Promise<string> {
//
// Download - a tool installer intimately knows how to get the tool (and construct urls)
//
version = toolLib.cleanVersion(version);
let fileName: string = osPlat == 'win32'? 'node-v' + version + '-win-' + os.arch() :
'node-v' + version + '-' + osPlat + '-' + os.arch();
let urlFileName: string = osPlat == 'win32'? fileName + '.7z':
fileName + '.tar.gz';
let downloadUrl = 'https://nodejs.org/dist/v' + version + '/' + urlFileName;
let downloadPath: string = await toolLib.downloadTool(downloadUrl);
//
// Extract
//
let extPath: string;
if (osPlat == 'win32') {
taskLib.assertAgent('2.115.0');
extPath = taskLib.getVariable('Agent.TempDirectory');
if (!extPath) {
throw new Error('Expected Agent.TempDirectory to be set');
}
extPath = path.join(extPath, 'n'); // use as short a path as possible due to nested node_modules folders
extPath = await toolLib.extract7z(downloadPath, extPath);
}
else {
extPath = await toolLib.extractTar(downloadPath);
}
//
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
//
let toolRoot = path.join(extPath, fileName);
return await toolLib.cacheDir(toolRoot, 'node', version);
}
run();