File tree Expand file tree Collapse file tree 4 files changed +65
-13
lines changed Expand file tree Collapse file tree 4 files changed +65
-13
lines changed Original file line number Diff line number Diff line change 1+ {
2+   "engines" : {
3+     "node" : " >=14.0.0" 
4+   }
5+ }
Original file line number Diff line number Diff line change @@ -591,6 +591,33 @@ describe('setup-node', () => {
591591      ) ; 
592592    } ) ; 
593593
594+     it ( 'reads package.json as node-version-file if provided' ,  async  ( )  =>  { 
595+       // Arrange 
596+       const  versionSpec  =  fs . readFileSync ( 
597+         path . join ( __dirname ,  'data/package.json' ) , 
598+         'utf-8' 
599+       ) ; 
600+       const  versionFile  =  'package.json' ; 
601+       const  expectedVersionSpec  =  '14' ; 
602+       process . env [ 'GITHUB_WORKSPACE' ]  =  path . join ( __dirname ,  'data' ) ; 
603+       inputs [ 'node-version-file' ]  =  versionFile ; 
604+ 
605+       parseNodeVersionSpy . mockImplementation ( ( )  =>  expectedVersionSpec ) ; 
606+       existsSpy . mockImplementationOnce ( 
607+         input  =>  input  ===  path . join ( __dirname ,  'data' ,  versionFile ) 
608+       ) ; 
609+       // Act 
610+       await  main . run ( ) ; 
611+ 
612+       // Assert 
613+       expect ( existsSpy ) . toHaveBeenCalledTimes ( 1 ) ; 
614+       expect ( existsSpy ) . toHaveReturnedWith ( true ) ; 
615+       expect ( parseNodeVersionSpy ) . toHaveBeenCalledWith ( versionSpec ) ; 
616+       expect ( logSpy ) . toHaveBeenCalledWith ( 
617+         `Resolved ${ versionFile }   as ${ expectedVersionSpec }  ` 
618+       ) ; 
619+     } ) ; 
620+ 
594621    it ( 'both node-version-file and node-version are provided' ,  async  ( )  =>  { 
595622      inputs [ 'node-version' ]  =  '12' ; 
596623      const  versionSpec  =  'v14' ; 
Original file line number Diff line number Diff line change @@ -71768,15 +71768,25 @@ function translateArchToDistUrl(arch) {
7176871768    }
7176971769}
7177071770function parseNodeVersionFile(contents) {
71771-     var _a;
71771+     var _a, _b;
71772+     let nodeVersion;
7177271773    const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
71773-     const nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version;
71774-     if (nodeVersion) {
71775-         return nodeVersion;
71774+     nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version;
71775+     if (!nodeVersion) {
71776+         try {
71777+             // Try parsing the file as an NPM `package.json`
71778+             // file.
71779+             nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node;
71780+             if (!nodeVersion)
71781+                 throw new Error();
71782+         }
71783+         catch (err) {
71784+             // In the case of an unknown format,
71785+             // return as is and evaluate the version separately.
71786+             nodeVersion = contents.trim();
71787+         }
7177671788    }
71777-     // In the case of an unknown format,
71778-     // return as is and evaluate the version separately.
71779-     return contents.trim();
71789+     return nodeVersion;
7178071790}
7178171791exports.parseNodeVersionFile = parseNodeVersionFile;
7178271792function isLatestSyntax(versionSpec) {
Original file line number Diff line number Diff line change @@ -495,16 +495,26 @@ function translateArchToDistUrl(arch: string): string {
495495} 
496496
497497export  function  parseNodeVersionFile ( contents : string ) : string  { 
498+   let  nodeVersion : string  |  undefined ; 
499+ 
498500  const  found  =  contents . match ( / ^ (?: n o d e j s \s + ) ? v ? (?< version > [ ^ \s ] + ) $ / m) ; 
499-   const  nodeVersion  =  found ?. groups ?. version ; 
501+   nodeVersion  =  found ?. groups ?. version ; 
502+ 
503+   if  ( ! nodeVersion )  { 
504+     try  { 
505+       // Try parsing the file as an NPM `package.json` 
506+       // file. 
507+       nodeVersion  =  JSON . parse ( contents ) . engines ?. node ; 
500508
501-   if  ( nodeVersion )  { 
502-     return  nodeVersion ; 
509+       if  ( ! nodeVersion )  throw  new  Error ( ) ; 
510+     }  catch  ( err )  { 
511+       // In the case of an unknown format, 
512+       // return as is and evaluate the version separately. 
513+       nodeVersion  =  contents . trim ( ) ; 
514+     } 
503515  } 
504516
505-   // In the case of an unknown format, 
506-   // return as is and evaluate the version separately. 
507-   return  contents . trim ( ) ; 
517+   return  nodeVersion  as  string ; 
508518} 
509519
510520function  isLatestSyntax ( versionSpec ) : boolean  { 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments