Skip to content

Commit 80f8baa

Browse files
authored
Merge pull request #31 from pkgjs/owner-repo-style
Support "owner/repo" repository style in packument
2 parents a915588 + 56b899a commit 80f8baa

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

lib/loader.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const Debug = require('debug');
34
const Fs = require('fs');
45
const GitUrlParse = require('git-url-parse');
56
const Package = require('../package.json');
@@ -9,7 +10,10 @@ const Wreck = require('@hapi/wreck');
910

1011
const Utils = require('./utils');
1112

12-
const internals = {};
13+
const internals = {
14+
log: Debug('detect-node-support:loader'),
15+
error: Debug('detect-node-support:error')
16+
};
1317

1418

1519
internals.parseRepository = (packument) => {
@@ -66,6 +70,10 @@ internals.createPackageLoader = async (packageName) => {
6670

6771
internals.createRepositoryLoader = (repository) => {
6872

73+
if (repository.split('/').length === 2) {
74+
repository = `https://github.com/${repository}`;
75+
}
76+
6977
const parsedRepository = GitUrlParse(repository);
7078

7179
return {
@@ -85,20 +93,24 @@ internals.createRepositoryLoader = (repository) => {
8593
}
8694

8795
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
96+
internals.log('Loading: %s', url);
8897

8998
try {
9099
const { payload } = await Wreck.get(url, options);
91100

101+
internals.log('Loaded: %s', url);
92102
return payload;
93103
}
94104
catch (err) {
95105

96-
if (err.output && err.output.statusCode === 404) {
106+
if (err.data && err.data.res.statusCode === 404) {
107+
internals.log('Not found: %s', url);
97108
const error = new Error(`${repository} does not contain a ${filename}`);
98109
error.code = 'ENOENT';
99110
throw error;
100111
}
101112

113+
internals.error('Failed to load: %s', url);
102114
throw err;
103115
}
104116
}

lib/package.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ internals.what = (what) => {
3131
return { path: what };
3232
}
3333

34-
if (what.includes('/') && !what.startsWith('@')) {
35-
return { repository: `https://github.com/${what}` };
34+
if (what.split('/').length === 2 && !what.startsWith('@')) {
35+
return { repository: what };
3636
}
3737

3838
return { packageName: what };

test/index.js

+33
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,39 @@ describe('detect-node-support', () => {
454454
});
455455
});
456456

457+
it('supports "owner/repo" style repository string', async () => {
458+
459+
listRemoteStub
460+
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');
461+
462+
Nock('https://raw.githubusercontent.com')
463+
.get('/pkgjs/detect-node-support/HEAD/package.json')
464+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')))
465+
.get('/pkgjs/detect-node-support/HEAD/.travis.yml')
466+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')));
467+
468+
const result = await NodeSupport.detect({ repository: 'pkgjs/detect-node-support' });
469+
470+
expect(listRemoteStub.callCount).to.equal(1);
471+
expect(listRemoteStub.args[0]).to.equal([['http://github.com/pkgjs/detect-node-support', 'HEAD']]);
472+
473+
expect(result).to.equal({
474+
name: 'detect-node-support',
475+
version: '0.0.0-development',
476+
commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23',
477+
timestamp: 1580673602000,
478+
travis: {
479+
raw: ['10', '12', '14'],
480+
resolved: {
481+
'10': '10.20.1',
482+
'12': '12.17.0',
483+
'14': '14.3.0'
484+
}
485+
},
486+
engines: '>=10'
487+
});
488+
});
489+
457490
it('leaves out `travis` when no `.travis.yml` present', async () => {
458491

459492
fixture.stubs.listRemote

0 commit comments

Comments
 (0)