11'use strict' ;
22
3+ const Debug = require ( 'debug' ) ;
34const Nv = require ( '@pkgjs/nv' ) ;
45const Yaml = require ( 'js-yaml' ) ;
56
7+ const Loader = require ( './loader' ) ;
8+
69
710const internals = { } ;
811
12+
13+ internals . log = Debug ( 'detect-node-support' ) ;
14+
15+
916internals . nodeAliases = {
1017 latest : 'active' ,
1118 node : 'active' ,
@@ -49,7 +56,31 @@ internals.normalizeImports = (travisYaml, { relativeTo }) => {
4956} ;
5057
5158
52- internals . applyImports = async ( yaml , { loadFile, relativeTo } ) => {
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 ( ) } ) => {
5384
5485 if ( ! yaml . import ) {
5586 return ;
@@ -59,14 +90,14 @@ internals.applyImports = async (yaml, { loadFile, relativeTo }) => {
5990
6091 for ( const entry of imports ) {
6192
62- const buffer = await loadFile ( entry . source ) ;
93+ const buffer = await internals . loadSource ( entry . source , { loadFile , cache } ) ;
6394
6495 const imported = Yaml . safeLoad ( buffer , {
6596 schema : Yaml . FAILSAFE_SCHEMA ,
6697 json : true
6798 } ) ;
6899
69- await internals . applyImports ( imported , { loadFile, relativeTo : entry } ) ;
100+ await internals . applyImports ( imported , { loadFile, relativeTo : entry , cache } ) ;
70101
71102 for ( const key in imported ) {
72103
0 commit comments