1
1
'use strict' ;
2
2
3
- const Debug = require ( 'debug' ) ;
4
3
const Nv = require ( '@pkgjs/nv' ) ;
5
4
const Yaml = require ( 'js-yaml' ) ;
6
5
7
- const Loader = require ( './loader' ) ;
6
+ const TravisImports = require ( './imports' ) ;
7
+ const Utils = require ( '../utils' ) ;
8
8
9
9
10
10
const internals = { } ;
11
11
12
12
13
- internals . log = Debug ( 'detect-node-support' ) ;
14
-
15
-
16
13
internals . nodeAliases = {
17
14
latest : 'active' ,
18
15
node : 'active' ,
19
16
stable : 'active'
20
17
} ;
21
18
22
19
23
- internals . toArray = ( v ) => {
24
-
25
- if ( v === undefined ) {
26
- return [ ] ;
27
- }
28
-
29
- return Array . isArray ( v ) ? v : [ v ] ;
30
- } ;
31
-
32
-
33
- internals . normalizeImports = ( travisYaml , { relativeTo } ) => {
34
-
35
- return internals . toArray ( travisYaml . import )
36
- . map ( ( entry ) => {
37
-
38
- if ( typeof entry === 'string' ) {
39
- entry = { source : entry } ;
40
- }
41
-
42
- if ( entry . source . startsWith ( './' ) ) {
43
- entry . source = entry . source . substring ( 2 ) ;
44
-
45
- if ( relativeTo ) {
46
- const relativeParts = relativeTo . source . split ( '/' ) ;
47
- relativeParts . pop ( ) ;
48
- relativeParts . push ( entry . source ) ;
49
- entry . source = relativeParts . join ( '/' ) ;
50
- }
51
- }
52
-
53
- return entry ;
54
- } )
55
- . filter ( ( entry ) => ! entry . if ) ; // @todo : log a warning
56
- } ;
57
-
58
-
59
- internals . loadSource = async ( source , { loadFile, cache } ) => {
60
-
61
- if ( cache [ source ] ) {
62
- internals . log ( 'Returning cached %s' , source ) ;
63
- return cache [ source ] ;
64
- }
65
-
66
- let path = source ;
67
-
68
- if ( source . includes ( ':' ) ) {
69
- const [ repository , fileName ] = source . split ( ':' ) ;
70
- const loader = await Loader . create ( { repository : `https://github.com/${ repository } ` } ) ;
71
-
72
- path = fileName ;
73
- loadFile = loader . loadFile ;
74
- }
75
-
76
- internals . log ( 'Loading %s' , source ) ;
77
- const result = await loadFile ( path ) ;
78
- cache [ source ] = result ;
79
- return result ;
80
- } ;
81
-
82
-
83
- internals . applyImports = async ( yaml , { loadFile, relativeTo, cache = new Map ( ) } ) => {
84
-
85
- if ( ! yaml . import ) {
86
- return ;
87
- }
88
-
89
- const imports = internals . normalizeImports ( yaml , { relativeTo } ) ;
90
-
91
- for ( const entry of imports ) {
92
-
93
- const buffer = await internals . loadSource ( entry . source , { loadFile, cache } ) ;
94
-
95
- const imported = Yaml . safeLoad ( buffer , {
96
- schema : Yaml . FAILSAFE_SCHEMA ,
97
- json : true
98
- } ) ;
99
-
100
- await internals . applyImports ( imported , { loadFile, relativeTo : entry , cache } ) ;
101
-
102
- for ( const key in imported ) {
103
-
104
- if ( key === 'import' ) {
105
- continue ;
106
- }
107
-
108
- yaml [ key ] = imported [ key ] ;
109
- }
110
- }
111
- } ;
112
-
113
-
114
20
internals . scan = async ( travisYaml , options ) => {
115
21
116
- await internals . applyImports ( travisYaml , options ) ;
22
+ await TravisImports . apply ( travisYaml , options ) ;
117
23
118
24
const rawSet = new Set ( ) ;
119
25
120
- for ( const v of internals . toArray ( travisYaml . node_js ) ) {
26
+ for ( const v of Utils . toArray ( travisYaml . node_js ) ) {
121
27
rawSet . add ( v ) ;
122
28
}
123
29
124
30
if ( travisYaml . env ) {
125
31
126
- for ( const env of internals . toArray ( travisYaml . env . matrix ) ) {
32
+ for ( const env of Utils . toArray ( travisYaml . env . matrix ) ) {
127
33
128
34
const matches = env . match ( / (?: N O D E J S _ V E R | T R A V I S _ N O D E _ V E R S I O N | N O D E _ V E R ) = " ? ( n o d e \/ ) ? (?< version > [ \w . / * ] + ) " ? / ) ;
129
35
@@ -135,7 +41,7 @@ internals.scan = async (travisYaml, options) => {
135
41
136
42
if ( travisYaml . matrix ) {
137
43
138
- for ( const include of internals . toArray ( travisYaml . matrix . include ) ) {
44
+ for ( const include of Utils . toArray ( travisYaml . matrix . include ) ) {
139
45
140
46
if ( include . node_js ) {
141
47
rawSet . add ( include . node_js ) ;
0 commit comments