22
33const Nv = require ( '@pkgjs/nv' ) ;
44
5+
6+ const internals = { } ;
7+
8+
9+ internals . parseActionsSetupNode = function * ( workflow ) {
10+
11+ for ( const job of Object . values ( workflow . jobs ) ) {
12+
13+ const nodeSteps = job . steps . filter ( ( { uses } ) => uses && uses . startsWith ( 'actions/setup-node' ) ) ;
14+ for ( const step of nodeSteps ) {
15+ const nodeVersion = step . with && step . with [ 'node-version' ] ;
16+
17+ if ( ! nodeVersion ) {
18+ // Docs say: "The node-version input is optional. If not supplied, the node version that is PATH will be used."
19+ // Therefore we cannot reliably detect a specific version, but we do want to let the user know
20+ yield 'not-set' ;
21+ continue ;
22+ }
23+
24+ const matrixMatch = nodeVersion . match ( / ^ \$ { { \s + m a t r i x .(?< matrixVarName > .* ) \s + } } $ / ) ;
25+ if ( matrixMatch ) {
26+ const matrix = job . strategy . matrix [ matrixMatch . groups . matrixVarName ] ;
27+
28+ yield * matrix ;
29+ continue ;
30+ }
31+
32+ const envMatch = nodeVersion . match ( / ^ \$ { { \s + e n v .(?< envVarName > .* ) \s + } } $ / ) ;
33+ if ( envMatch ) {
34+ const envValue = workflow . env [ envMatch . groups . envVarName ] ;
35+
36+ yield envValue ;
37+ continue ;
38+ }
39+
40+ yield nodeVersion ;
41+ }
42+ }
43+ } ;
44+
45+
546exports . detect = async ( meta ) => {
647
748 const files = await meta . loadFolder ( '.github/workflows' ) ;
849 const rawSet = new Set ( ) ;
950
1051 if ( ! files . length ) {
52+ // explicitly return no `githubActions` - this is different to finding actions and detecting no Node.js versions
1153 return ;
1254 }
1355
@@ -19,37 +61,8 @@ exports.detect = async (meta) => {
1961
2062 const workflow = await meta . loadFile ( `.github/workflows/${ file } ` , { yaml : true } ) ;
2163
22- for ( const job of Object . values ( workflow . jobs ) ) {
23-
24- const nodeSteps = job . steps . filter ( ( { uses } ) => uses && uses . startsWith ( 'actions/setup-node' ) ) ;
25- for ( const step of nodeSteps ) {
26- const nodeVersion = step . with && step . with [ 'node-version' ] ;
27-
28- if ( ! nodeVersion ) {
29- // @todo - no node version defined - use default? what is the default?
30- continue ;
31- }
32-
33- const matrixMatch = nodeVersion . match ( / ^ \$ { { \s + m a t r i x .(?< matrixVarName > .* ) \s + } } $ / ) ;
34- if ( matrixMatch ) {
35- const matrix = job . strategy . matrix [ matrixMatch . groups . matrixVarName ] ;
36-
37- for ( const version of matrix ) {
38- rawSet . add ( version ) ;
39- }
40-
41- continue ;
42- }
43-
44- const envMatch = nodeVersion . match ( / ^ \$ { { \s + e n v .(?< envVarName > .* ) \s + } } $ / ) ;
45- if ( envMatch ) {
46- rawSet . add ( workflow . env [ envMatch . groups . envVarName ] ) ;
47-
48- continue ;
49- }
50-
51- rawSet . add ( nodeVersion ) ;
52- }
64+ for ( const version of internals . parseActionsSetupNode ( workflow ) ) {
65+ rawSet . add ( version ) ;
5366 }
5467 }
5568
0 commit comments