11'use strict' ;
22
3- const Debug = require ( 'debug' ) ;
43const Nv = require ( '@pkgjs/nv' ) ;
54const Yaml = require ( 'js-yaml' ) ;
65
7- const Loader = require ( './loader' ) ;
6+ const TravisImports = require ( './imports' ) ;
7+ const Utils = require ( '../utils' ) ;
88
99
1010const internals = { } ;
1111
1212
13- internals . log = Debug ( 'detect-node-support' ) ;
14-
15-
1613internals . nodeAliases = {
1714 latest : 'active' ,
1815 node : 'active' ,
1916 stable : 'active'
2017} ;
2118
2219
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-
11420internals . scan = async ( travisYaml , options ) => {
11521
116- await internals . applyImports ( travisYaml , options ) ;
22+ await TravisImports . apply ( travisYaml , options ) ;
11723
11824 const rawSet = new Set ( ) ;
11925
120- for ( const v of internals . toArray ( travisYaml . node_js ) ) {
26+ for ( const v of Utils . toArray ( travisYaml . node_js ) ) {
12127 rawSet . add ( v ) ;
12228 }
12329
12430 if ( travisYaml . env ) {
12531
126- for ( const env of internals . toArray ( travisYaml . env . matrix ) ) {
32+ for ( const env of Utils . toArray ( travisYaml . env . matrix ) ) {
12733
12834 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 . / * ] + ) " ? / ) ;
12935
@@ -135,7 +41,7 @@ internals.scan = async (travisYaml, options) => {
13541
13642 if ( travisYaml . matrix ) {
13743
138- for ( const include of internals . toArray ( travisYaml . matrix . include ) ) {
44+ for ( const include of Utils . toArray ( travisYaml . matrix . include ) ) {
13945
14046 if ( include . node_js ) {
14147 rawSet . add ( include . node_js ) ;
0 commit comments