Skip to content

Commit 7ab9d3e

Browse files
committed
feat: load from repository
1 parent 168ffd6 commit 7ab9d3e

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ exports.detect = async ({ path, repository, packageName }) => {
1212
result.name = packageInfo.name;
1313
result.version = packageInfo.version;
1414

15-
const travis = await Travis.detect({ path });
15+
const travis = await Travis.detect({ path, repository });
1616

1717
if (travis) {
1818
result.travis = travis;

lib/package.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
'use strict';
22

3+
const GitUrlParse = require('git-url-parse');
34
const Path = require('path');
5+
const Wreck = require('@hapi/wreck');
46

5-
exports.detect = ({ path, repository, packageName }) => {
67

7-
const packageJson = require(Path.join(path, 'package.json'));
8+
exports.detect = async ({ path, repository, packageName }) => {
9+
10+
let packageJson;
11+
12+
if (repository) {
13+
const parsedRepository = GitUrlParse(repository);
14+
15+
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/package.json`;
16+
17+
const { payload } = await Wreck.get(url);
18+
19+
packageJson = JSON.parse(payload.toString());
20+
}
21+
22+
if (path) {
23+
packageJson = require(Path.join(path, 'package.json'));
24+
}
825

926
const { name, version } = packageJson;
1027

lib/travis.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

33
const Fs = require('fs');
4+
const GitUrlParse = require('git-url-parse');
45
const Path = require('path');
6+
const Wreck = require('@hapi/wreck');
57
const Yaml = require('js-yaml');
68

79

@@ -56,15 +58,32 @@ internals.scan = (travisYaml) => {
5658
};
5759

5860

59-
exports.detect = ({ path }) => {
61+
exports.detect = async ({ path, repository }) => {
6062

61-
const travisYamlPath = Path.join(path, '.travis.yml');
63+
let buffer;
6264

63-
if (!Fs.existsSync(travisYamlPath)) {
64-
return;
65+
if (repository) {
66+
const parsedRepository = GitUrlParse(repository);
67+
68+
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/.travis.yml`;
69+
70+
const result = await Wreck.get(url);
71+
72+
buffer = result.payload;
73+
}
74+
75+
if (path) {
76+
77+
const travisYamlPath = Path.join(path, '.travis.yml');
78+
79+
if (!Fs.existsSync(travisYamlPath)) {
80+
return;
81+
}
82+
83+
buffer = Fs.readFileSync(travisYamlPath);
6584
}
6685

67-
const travisYaml = Yaml.safeLoad(Fs.readFileSync(travisYamlPath), { schema: Yaml.FAILSAFE_SCHEMA });
86+
const travisYaml = Yaml.safeLoad(buffer, { schema: Yaml.FAILSAFE_SCHEMA });
6887

6988
return internals.scan(travisYaml);
7089
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"tmp": "^0.1.0"
2525
},
2626
"dependencies": {
27+
"@hapi/wreck": "^16.0.1",
28+
"git-url-parse": "^11.1.2",
2729
"js-yaml": "^3.13.1"
2830
}
2931
}

test/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,22 @@ describe('node-support', () => {
242242
});
243243
});
244244
});
245+
246+
247+
describe('repository', () => {
248+
249+
it('returns node versions from `.travis.yml` in the repository', async () => {
250+
251+
const result = await NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/node-support.git' });
252+
253+
expect(result).to.equal({
254+
name: 'node-support',
255+
version: '0.0.0-development',
256+
travis: {
257+
raw: ['10', '12', '13']
258+
}
259+
});
260+
});
261+
});
245262
});
246263
});

0 commit comments

Comments
 (0)