Skip to content

Commit

Permalink
Merge pull request #496 from panticmilos/v-mpantic/get-latest-version…
Browse files Browse the repository at this point in the history
…-from-cache

Get latest version from cache if exists
  • Loading branch information
marko-zivic-93 authored May 30, 2022
2 parents ea3459b + b14573d commit daff393
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 20 deletions.
27 changes: 27 additions & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 18 additions & 9 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
Expand Down Expand Up @@ -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}.`);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -62688,6 +62694,9 @@ function parseNodeVersionFile(contents) {
return nodeVersion;
}
exports.parseNodeVersionFile = parseNodeVersionFile;
function isLatestSyntax(versionSpec) {
return ['current', 'latest', 'node'].includes(versionSpec);
}


/***/ }),
Expand Down
38 changes: 27 additions & 11 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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(
Expand Down Expand Up @@ -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}.`
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -482,3 +494,7 @@ export function parseNodeVersionFile(contents: string): string {
}
return nodeVersion;
}

function isLatestSyntax(versionSpec): boolean {
return ['current', 'latest', 'node'].includes(versionSpec);
}

0 comments on commit daff393

Please sign in to comment.