Merge pull request #496 from panticmilos/v-mpantic/get-latest-version-from-cache

Get latest version from cache if exists
This commit is contained in:
Marko Zivic 2022-05-30 09:13:35 +02:00 committed by GitHub
commit daff393d43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 21 deletions

View file

@ -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...');
}
);
});
});

27
dist/setup/index.js vendored
View file

@ -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);
}
/***/ }),

View file

@ -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<INodeVersionInfo | null> {
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<string> {
let osPlat: string = os.platform();
let osArch: string = translateArchToDistUrl(arch);
@ -370,14 +382,14 @@ async function queryDistForMatch(
throw new Error(`Unexpected OS '${osPlat}'`);
}
let versions: string[] = [];
let nodeVersions = await getVersionsFromDist();
if (!nodeVersions) {
core.debug('No dist manifest cached');
nodeVersions = await getVersionsFromDist();
}
if (
versionSpec === 'current' ||
versionSpec === 'latest' ||
versionSpec === 'node'
) {
let versions: string[] = [];
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);
}