Skip to content

Commit daff393

Browse files
Merge pull request #496 from panticmilos/v-mpantic/get-latest-version-from-cache
Get latest version from cache if exists
2 parents ea3459b + b14573d commit daff393

File tree

3 files changed

+72
-20
lines changed

3 files changed

+72
-20
lines changed

__tests__/installer.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -913,4 +913,31 @@ describe('setup-node', () => {
913913
}
914914
);
915915
});
916+
917+
describe('latest alias syntax from cache', () => {
918+
it.each(['latest', 'current', 'node'])(
919+
'download the %s version if alias is provided',
920+
async inputVersion => {
921+
// Arrange
922+
inputs['node-version'] = inputVersion;
923+
const expectedVersion = nodeTestDist[0];
924+
925+
os.platform = 'darwin';
926+
os.arch = 'x64';
927+
928+
const toolPath = path.normalize(
929+
`/cache/node/${expectedVersion.version}/x64`
930+
);
931+
findSpy.mockReturnValue(toolPath);
932+
933+
// Act
934+
await main.run();
935+
936+
// assert
937+
expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
938+
939+
expect(logSpy).toHaveBeenCalledWith('getting latest node version...');
940+
}
941+
);
942+
});
916943
});

dist/setup/index.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -62343,6 +62343,7 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) {
6234362343
return __awaiter(this, void 0, void 0, function* () {
6234462344
// Store manifest data to avoid multiple calls
6234562345
let manifest;
62346+
let nodeVersions;
6234662347
let osPlat = os.platform();
6234762348
let osArch = translateArchToDistUrl(arch);
6234862349
if (isLtsAlias(versionSpec)) {
@@ -62351,6 +62352,11 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) {
6235162352
manifest = yield getManifest(auth);
6235262353
versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest);
6235362354
}
62355+
if (isLatestSyntax(versionSpec)) {
62356+
nodeVersions = yield getVersionsFromDist();
62357+
versionSpec = yield queryDistForMatch(versionSpec, arch, nodeVersions);
62358+
core.info(`getting latest node version...`);
62359+
}
6235462360
if (checkLatest) {
6235562361
core.info('Attempt to resolve the latest version from manifest...');
6235662362
const resolvedVersion = yield resolveVersionFromManifest(versionSpec, stable, auth, osArch, manifest);
@@ -62402,7 +62408,7 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) {
6240262408
// Download from nodejs.org
6240362409
//
6240462410
if (!downloadPath) {
62405-
info = yield getInfoFromDist(versionSpec, arch);
62411+
info = yield getInfoFromDist(versionSpec, arch, nodeVersions);
6240662412
if (!info) {
6240762413
throw new Error(`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`);
6240862414
}
@@ -62502,12 +62508,11 @@ function getInfoFromManifest(versionSpec, stable, auth, osArch = translateArchTo
6250262508
return info;
6250362509
});
6250462510
}
62505-
function getInfoFromDist(versionSpec, arch = os.arch()) {
62511+
function getInfoFromDist(versionSpec, arch = os.arch(), nodeVersions) {
6250662512
return __awaiter(this, void 0, void 0, function* () {
6250762513
let osPlat = os.platform();
6250862514
let osArch = translateArchToDistUrl(arch);
62509-
let version;
62510-
version = yield queryDistForMatch(versionSpec, arch);
62515+
let version = yield queryDistForMatch(versionSpec, arch, nodeVersions);
6251162516
if (!version) {
6251262517
return null;
6251362518
}
@@ -62566,7 +62571,7 @@ function evaluateVersions(versions, versionSpec) {
6256662571
}
6256762572
return version;
6256862573
}
62569-
function queryDistForMatch(versionSpec, arch = os.arch()) {
62574+
function queryDistForMatch(versionSpec, arch = os.arch(), nodeVersions) {
6257062575
return __awaiter(this, void 0, void 0, function* () {
6257162576
let osPlat = os.platform();
6257262577
let osArch = translateArchToDistUrl(arch);
@@ -62585,11 +62590,12 @@ function queryDistForMatch(versionSpec, arch = os.arch()) {
6258562590
default:
6258662591
throw new Error(`Unexpected OS '${osPlat}'`);
6258762592
}
62593+
if (!nodeVersions) {
62594+
core.debug('No dist manifest cached');
62595+
nodeVersions = yield getVersionsFromDist();
62596+
}
6258862597
let versions = [];
62589-
let nodeVersions = yield getVersionsFromDist();
62590-
if (versionSpec === 'current' ||
62591-
versionSpec === 'latest' ||
62592-
versionSpec === 'node') {
62598+
if (isLatestSyntax(versionSpec)) {
6259362599
core.info(`getting latest node version...`);
6259462600
return nodeVersions[0].version;
6259562601
}
@@ -62688,6 +62694,9 @@ function parseNodeVersionFile(contents) {
6268862694
return nodeVersion;
6268962695
}
6269062696
exports.parseNodeVersionFile = parseNodeVersionFile;
62697+
function isLatestSyntax(versionSpec) {
62698+
return ['current', 'latest', 'node'].includes(versionSpec);
62699+
}
6269162700

6269262701

6269362702
/***/ }),

src/installer.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export async function getNode(
3737
) {
3838
// Store manifest data to avoid multiple calls
3939
let manifest: INodeRelease[] | undefined;
40+
let nodeVersions: INodeVersion[] | undefined;
4041
let osPlat: string = os.platform();
4142
let osArch: string = translateArchToDistUrl(arch);
4243

@@ -49,6 +50,12 @@ export async function getNode(
4950
versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest);
5051
}
5152

53+
if (isLatestSyntax(versionSpec)) {
54+
nodeVersions = await getVersionsFromDist();
55+
versionSpec = await queryDistForMatch(versionSpec, arch, nodeVersions);
56+
core.info(`getting latest node version...`);
57+
}
58+
5259
if (checkLatest) {
5360
core.info('Attempt to resolve the latest version from manifest...');
5461
const resolvedVersion = await resolveVersionFromManifest(
@@ -119,7 +126,7 @@ export async function getNode(
119126
// Download from nodejs.org
120127
//
121128
if (!downloadPath) {
122-
info = await getInfoFromDist(versionSpec, arch);
129+
info = await getInfoFromDist(versionSpec, arch, nodeVersions);
123130
if (!info) {
124131
throw new Error(
125132
`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
@@ -265,14 +272,18 @@ async function getInfoFromManifest(
265272

266273
async function getInfoFromDist(
267274
versionSpec: string,
268-
arch: string = os.arch()
275+
arch: string = os.arch(),
276+
nodeVersions?: INodeVersion[]
269277
): Promise<INodeVersionInfo | null> {
270278
let osPlat: string = os.platform();
271279
let osArch: string = translateArchToDistUrl(arch);
272280

273-
let version: string;
281+
let version: string = await queryDistForMatch(
282+
versionSpec,
283+
arch,
284+
nodeVersions
285+
);
274286

275-
version = await queryDistForMatch(versionSpec, arch);
276287
if (!version) {
277288
return null;
278289
}
@@ -349,7 +360,8 @@ function evaluateVersions(versions: string[], versionSpec: string): string {
349360

350361
async function queryDistForMatch(
351362
versionSpec: string,
352-
arch: string = os.arch()
363+
arch: string = os.arch(),
364+
nodeVersions?: INodeVersion[]
353365
): Promise<string> {
354366
let osPlat: string = os.platform();
355367
let osArch: string = translateArchToDistUrl(arch);
@@ -370,14 +382,14 @@ async function queryDistForMatch(
370382
throw new Error(`Unexpected OS '${osPlat}'`);
371383
}
372384

385+
if (!nodeVersions) {
386+
core.debug('No dist manifest cached');
387+
nodeVersions = await getVersionsFromDist();
388+
}
389+
373390
let versions: string[] = [];
374-
let nodeVersions = await getVersionsFromDist();
375391

376-
if (
377-
versionSpec === 'current' ||
378-
versionSpec === 'latest' ||
379-
versionSpec === 'node'
380-
) {
392+
if (isLatestSyntax(versionSpec)) {
381393
core.info(`getting latest node version...`);
382394
return nodeVersions[0].version;
383395
}
@@ -482,3 +494,7 @@ export function parseNodeVersionFile(contents: string): string {
482494
}
483495
return nodeVersion;
484496
}
497+
498+
function isLatestSyntax(versionSpec): boolean {
499+
return ['current', 'latest', 'node'].includes(versionSpec);
500+
}

0 commit comments

Comments
 (0)