Skip to content

Commit 772e957

Browse files
committed
fixup! Add mirror option
1 parent 6a399da commit 772e957

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

lib/installer.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,40 @@ function queryLatestMatch(versionSpec, mirror) {
108108
nodeVersions.forEach((nodeVersion) => {
109109
// ensure this version supports your os and platform
110110
if (nodeVersion.files.indexOf(dataFileName) >= 0) {
111-
versions.push(nodeVersion.version);
111+
versions.push(nodeVersion);
112112
}
113113
});
114114
// get the latest version that matches the version spec
115115
let version = evaluateVersions(versions, versionSpec);
116116
return version;
117117
});
118118
}
119+
exports.queryLatestMatch = queryLatestMatch;
119120
// TODO - should we just export this from @actions/tool-cache? Lifted directly from there
120121
function evaluateVersions(versions, versionSpec) {
121122
let version = '';
122123
core.debug(`evaluating ${versions.length} versions`);
123124
versions = versions.sort((a, b) => {
124-
if (semver.gt(a, b)) {
125+
const versionA = semver.coerce(a.version);
126+
const versionB = semver.coerce(b.version);
127+
// If versions are equal, compare date instead
128+
if (versionA === versionB || versionA === null || versionB === null) {
129+
if (new Date(a.date) > new Date(b.date)) {
130+
return 1;
131+
}
132+
return -1;
133+
}
134+
if (semver.gt(versionA, versionB)) {
125135
return 1;
126136
}
127137
return -1;
128138
});
129139
for (let i = versions.length - 1; i >= 0; i--) {
130-
const potential = versions[i];
131-
const satisfied = semver.satisfies(potential, versionSpec);
140+
const potential = versions[i].version;
141+
const semverPotential = semver.coerce(potential);
142+
if (semverPotential === null)
143+
continue;
144+
const satisfied = semver.satisfies(semverPotential, versionSpec);
132145
if (satisfied) {
133146
version = potential;
134147
break;

src/installer.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ if (!tempDirectory) {
3232
//
3333
interface INodeVersion {
3434
version: string;
35+
date: string;
3536
files: string[];
3637
}
3738

@@ -81,7 +82,7 @@ export async function getNode(versionSpec: string, mirror: string) {
8182
core.addPath(toolPath);
8283
}
8384

84-
async function queryLatestMatch(
85+
export async function queryLatestMatch(
8586
versionSpec: string,
8687
mirror: string
8788
): Promise<string> {
@@ -101,15 +102,15 @@ async function queryLatestMatch(
101102
throw new Error(`Unexpected OS '${osPlat}'`);
102103
}
103104

104-
let versions: string[] = [];
105+
let versions: INodeVersion[] = [];
105106
let dataUrl = `${mirror}/index.json`;
106107
let rest: restm.RestClient = new restm.RestClient('setup-node');
107108
let nodeVersions: INodeVersion[] =
108109
(await rest.get<INodeVersion[]>(dataUrl)).result || [];
109110
nodeVersions.forEach((nodeVersion: INodeVersion) => {
110111
// ensure this version supports your os and platform
111112
if (nodeVersion.files.indexOf(dataFileName) >= 0) {
112-
versions.push(nodeVersion.version);
113+
versions.push(nodeVersion);
113114
}
114115
});
115116

@@ -119,18 +120,32 @@ async function queryLatestMatch(
119120
}
120121

121122
// TODO - should we just export this from @actions/tool-cache? Lifted directly from there
122-
function evaluateVersions(versions: string[], versionSpec: string): string {
123+
function evaluateVersions(
124+
versions: INodeVersion[],
125+
versionSpec: string
126+
): string {
123127
let version = '';
124128
core.debug(`evaluating ${versions.length} versions`);
125129
versions = versions.sort((a, b) => {
126-
if (semver.gt(a, b)) {
130+
const versionA: semver.SemVer | null = semver.coerce(a.version);
131+
const versionB: semver.SemVer | null = semver.coerce(b.version);
132+
// If versions are equal, compare date instead
133+
if (versionA === versionB || versionA === null || versionB === null) {
134+
if (new Date(a.date) > new Date(b.date)) {
135+
return 1;
136+
}
137+
return -1;
138+
}
139+
if (semver.gt(versionA, versionB)) {
127140
return 1;
128141
}
129142
return -1;
130143
});
131144
for (let i = versions.length - 1; i >= 0; i--) {
132-
const potential: string = versions[i];
133-
const satisfied: boolean = semver.satisfies(potential, versionSpec);
145+
const potential: string = versions[i].version;
146+
const semverPotential: semver.SemVer | null = semver.coerce(potential);
147+
if (semverPotential === null) continue;
148+
const satisfied: boolean = semver.satisfies(semverPotential, versionSpec);
134149
if (satisfied) {
135150
version = potential;
136151
break;

0 commit comments

Comments
 (0)