@@ -32,6 +32,7 @@ if (!tempDirectory) {
3232//
3333interface 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