diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 004362d0d..f3eb8db9e 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -913,4 +913,31 @@ describe('setup-node', () => { } ); }); + + describe('latest alias syntax from cache', () => { + it.each(['latest', 'current', 'node'])( + 'download the %s version if alias is provided', + async inputVersion => { + // Arrange + inputs['node-version'] = inputVersion; + const expectedVersion = nodeTestDist[0]; + + os.platform = 'darwin'; + os.arch = 'x64'; + + const toolPath = path.normalize( + `/cache/node/${expectedVersion.version}/x64` + ); + findSpy.mockReturnValue(toolPath); + + // Act + await main.run(); + + // assert + expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`); + + expect(logSpy).toHaveBeenCalledWith('getting latest node version...'); + } + ); + }); }); diff --git a/dist/setup/index.js b/dist/setup/index.js index ecf999142..8a4d83b59 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -62343,6 +62343,7 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) { return __awaiter(this, void 0, void 0, function* () { // Store manifest data to avoid multiple calls let manifest; + let nodeVersions; let osPlat = os.platform(); let osArch = translateArchToDistUrl(arch); if (isLtsAlias(versionSpec)) { @@ -62351,6 +62352,11 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) { manifest = yield getManifest(auth); versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest); } + if (isLatestSyntax(versionSpec)) { + nodeVersions = yield getVersionsFromDist(); + versionSpec = yield queryDistForMatch(versionSpec, arch, nodeVersions); + core.info(`getting latest node version...`); + } if (checkLatest) { core.info('Attempt to resolve the latest version from manifest...'); const resolvedVersion = yield resolveVersionFromManifest(versionSpec, stable, auth, osArch, manifest); @@ -62402,7 +62408,7 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os.arch()) { // Download from nodejs.org // if (!downloadPath) { - info = yield getInfoFromDist(versionSpec, arch); + info = yield getInfoFromDist(versionSpec, arch, nodeVersions); if (!info) { throw new Error(`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`); } @@ -62502,12 +62508,11 @@ function getInfoFromManifest(versionSpec, stable, auth, osArch = translateArchTo return info; }); } -function getInfoFromDist(versionSpec, arch = os.arch()) { +function getInfoFromDist(versionSpec, arch = os.arch(), nodeVersions) { return __awaiter(this, void 0, void 0, function* () { let osPlat = os.platform(); let osArch = translateArchToDistUrl(arch); - let version; - version = yield queryDistForMatch(versionSpec, arch); + let version = yield queryDistForMatch(versionSpec, arch, nodeVersions); if (!version) { return null; } @@ -62566,7 +62571,7 @@ function evaluateVersions(versions, versionSpec) { } return version; } -function queryDistForMatch(versionSpec, arch = os.arch()) { +function queryDistForMatch(versionSpec, arch = os.arch(), nodeVersions) { return __awaiter(this, void 0, void 0, function* () { let osPlat = os.platform(); let osArch = translateArchToDistUrl(arch); @@ -62585,11 +62590,12 @@ function queryDistForMatch(versionSpec, arch = os.arch()) { default: throw new Error(`Unexpected OS '${osPlat}'`); } + if (!nodeVersions) { + core.debug('No dist manifest cached'); + nodeVersions = yield getVersionsFromDist(); + } let versions = []; - let nodeVersions = yield getVersionsFromDist(); - if (versionSpec === 'current' || - versionSpec === 'latest' || - versionSpec === 'node') { + if (isLatestSyntax(versionSpec)) { core.info(`getting latest node version...`); return nodeVersions[0].version; } @@ -62688,6 +62694,9 @@ function parseNodeVersionFile(contents) { return nodeVersion; } exports.parseNodeVersionFile = parseNodeVersionFile; +function isLatestSyntax(versionSpec) { + return ['current', 'latest', 'node'].includes(versionSpec); +} /***/ }), diff --git a/src/installer.ts b/src/installer.ts index 5b87d29a0..cd4619ea0 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -37,6 +37,7 @@ export async function getNode( ) { // Store manifest data to avoid multiple calls let manifest: INodeRelease[] | undefined; + let nodeVersions: INodeVersion[] | undefined; let osPlat: string = os.platform(); let osArch: string = translateArchToDistUrl(arch); @@ -49,6 +50,12 @@ export async function getNode( versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest); } + if (isLatestSyntax(versionSpec)) { + nodeVersions = await getVersionsFromDist(); + versionSpec = await queryDistForMatch(versionSpec, arch, nodeVersions); + core.info(`getting latest node version...`); + } + if (checkLatest) { core.info('Attempt to resolve the latest version from manifest...'); const resolvedVersion = await resolveVersionFromManifest( @@ -119,7 +126,7 @@ export async function getNode( // Download from nodejs.org // if (!downloadPath) { - info = await getInfoFromDist(versionSpec, arch); + info = await getInfoFromDist(versionSpec, arch, nodeVersions); if (!info) { throw new Error( `Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.` @@ -265,14 +272,18 @@ async function getInfoFromManifest( async function getInfoFromDist( versionSpec: string, - arch: string = os.arch() + arch: string = os.arch(), + nodeVersions?: INodeVersion[] ): Promise { let osPlat: string = os.platform(); let osArch: string = translateArchToDistUrl(arch); - let version: string; + let version: string = await queryDistForMatch( + versionSpec, + arch, + nodeVersions + ); - version = await queryDistForMatch(versionSpec, arch); if (!version) { return null; } @@ -349,7 +360,8 @@ function evaluateVersions(versions: string[], versionSpec: string): string { async function queryDistForMatch( versionSpec: string, - arch: string = os.arch() + arch: string = os.arch(), + nodeVersions?: INodeVersion[] ): Promise { let osPlat: string = os.platform(); let osArch: string = translateArchToDistUrl(arch); @@ -370,14 +382,14 @@ async function queryDistForMatch( throw new Error(`Unexpected OS '${osPlat}'`); } + if (!nodeVersions) { + core.debug('No dist manifest cached'); + nodeVersions = await getVersionsFromDist(); + } + let versions: string[] = []; - let nodeVersions = await getVersionsFromDist(); - if ( - versionSpec === 'current' || - versionSpec === 'latest' || - versionSpec === 'node' - ) { + if (isLatestSyntax(versionSpec)) { core.info(`getting latest node version...`); return nodeVersions[0].version; } @@ -482,3 +494,7 @@ export function parseNodeVersionFile(contents: string): string { } return nodeVersion; } + +function isLatestSyntax(versionSpec): boolean { + return ['current', 'latest', 'node'].includes(versionSpec); +}