Skip to content

Commit 09e0741

Browse files
committed
feat: load imports from another repo
1 parent 1adbddd commit 09e0741

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

lib/travis/index.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
'use strict';
22

3+
const Debug = require('debug');
34
const Nv = require('@pkgjs/nv');
45
const Yaml = require('js-yaml');
56

7+
const Loader = require('./loader');
8+
69

710
const internals = {};
811

12+
13+
internals.log = Debug('detect-node-support');
14+
15+
916
internals.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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
import:
3+
- source: pkgjs/detect-node-support:test/fixtures/travis-ymls/testing-imports/partials/indirect-node-14.yml
4+
- source: pkgjs/detect-node-support:test/fixtures/travis-ymls/testing-imports/partials/node-14.yml # cache hit

test/travis.js

+32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

3+
const Fs = require('fs');
4+
const Nock = require('nock');
5+
const Path = require('path');
6+
37
const NodeSupport = require('..');
48

59
const TestContext = require('./fixtures');
@@ -125,4 +129,32 @@ describe('.travis.yml parsing', () => {
125129
});
126130
});
127131

132+
it('resolves from another repo', async () => {
133+
134+
Nock('https://raw.githubusercontent.com')
135+
.get('/pkgjs/detect-node-support/HEAD/test/fixtures/travis-ymls/testing-imports/partials/indirect-node-14.yml')
136+
.reply(200, Fs.readFileSync(Path.join(__dirname, 'fixtures', 'travis-ymls', 'testing-imports', 'partials', 'indirect-node-14.yml')))
137+
.get('/pkgjs/detect-node-support/HEAD/test/fixtures/travis-ymls/testing-imports/partials/node-14.yml')
138+
.reply(200, Fs.readFileSync(Path.join(__dirname, 'fixtures', 'travis-ymls', 'testing-imports', 'partials', 'node-14.yml')));
139+
140+
await fixture.setupRepoFolder({
141+
partials: true,
142+
travisYml: `testing-imports/another-repo.yml`
143+
});
144+
145+
const result = await NodeSupport.detect({ path: fixture.path });
146+
147+
internals.assertCommit(result);
148+
149+
expect(result).to.equal({
150+
name: 'test-module',
151+
version: '0.0.0-development',
152+
timestamp: 1580673602000,
153+
travis: {
154+
raw: ['14'],
155+
resolved: { '14': '14.3.0' }
156+
}
157+
});
158+
});
159+
128160
});

0 commit comments

Comments
 (0)