File tree Expand file tree Collapse file tree 3 files changed +33
-4
lines changed Expand file tree Collapse file tree 3 files changed +33
-4
lines changed Original file line number Diff line number Diff line change @@ -101,6 +101,7 @@ describe('main tests', () => {
101
101
${ ' 14.1.0 ' } | ${ '14.1.0' }
102
102
${ '{"volta": {"node": ">=14.0.0 <=17.0.0"}}' } | ${ '>=14.0.0 <=17.0.0' }
103
103
${ '{"engines": {"node": "17.0.0"}}' } | ${ '17.0.0' }
104
+ ${ '{}' } | ${ null }
104
105
` . it ( 'parses "$contents"' , ( { contents, expected} ) => {
105
106
expect ( util . parseNodeVersionFile ( contents ) ) . toBe ( expected ) ;
106
107
} ) ;
Original file line number Diff line number Diff line change @@ -105,7 +105,17 @@ function resolveVersionInput(): string {
105
105
) ;
106
106
}
107
107
108
- version = parseNodeVersionFile ( fs . readFileSync ( versionFilePath , 'utf8' ) ) ;
108
+ const parsedVersion = parseNodeVersionFile (
109
+ fs . readFileSync ( versionFilePath , 'utf8' )
110
+ ) ;
111
+
112
+ if ( parsedVersion ) {
113
+ version = parsedVersion ;
114
+ } else {
115
+ core . warning (
116
+ `Could not determine node version from ${ versionFilePath } . Falling back`
117
+ ) ;
118
+ }
109
119
110
120
core . info ( `Resolved ${ versionFileInput } as ${ version } ` ) ;
111
121
}
Original file line number Diff line number Diff line change 1
1
import * as core from '@actions/core' ;
2
2
import * as exec from '@actions/exec' ;
3
3
4
- export function parseNodeVersionFile ( contents : string ) : string {
4
+ export function parseNodeVersionFile ( contents : string ) : string | null {
5
5
let nodeVersion : string | undefined ;
6
6
7
7
// Try parsing the file as an NPM `package.json` file.
8
8
try {
9
- nodeVersion = JSON . parse ( contents ) . volta ?. node ;
10
- if ( ! nodeVersion ) nodeVersion = JSON . parse ( contents ) . engines ?. node ;
9
+ const manifest = JSON . parse ( contents ) ;
10
+
11
+ // JSON can parse numbers, but that's handled later
12
+ if ( typeof manifest === 'object' ) {
13
+ nodeVersion = manifest . volta ?. node ;
14
+ if ( ! nodeVersion ) nodeVersion = manifest . engines ?. node ;
15
+
16
+ // if contents are an object, we parsed JSON
17
+ // this can happen if node-version-file is a package.json
18
+ // yet contains no volta.node or engines.node
19
+ //
20
+ // if node-version file is _not_ json, control flow
21
+ // will not have reached these lines.
22
+ //
23
+ // And because we've reached here, we know the contents
24
+ // *are* JSON, so no further string parsing makes sense.
25
+ if ( ! nodeVersion ) {
26
+ return null ;
27
+ }
28
+ }
11
29
} catch {
12
30
core . info ( 'Node version file is not JSON file' ) ;
13
31
}
You can’t perform that action at this time.
0 commit comments