Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

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

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

const internals = {};
const internals = {
log: Debug('detect-node-support:loader'),
error: Debug('detect-node-support:error')
};


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

internals.createRepositoryLoader = (repository) => {

if (repository.split('/').length === 2) {
repository = `https://github.com/${repository}`;
}

const parsedRepository = GitUrlParse(repository);

return {
Expand All @@ -85,20 +93,24 @@ internals.createRepositoryLoader = (repository) => {
}

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

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

internals.log('Loaded: %s', url);
return payload;
}
catch (err) {

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

internals.error('Failed to load: %s', url);
throw err;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ internals.what = (what) => {
return { path: what };
}

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

return { packageName: what };
Expand Down
33 changes: 33 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,39 @@ describe('detect-node-support', () => {
});
});

it('supports "owner/repo" style repository string', async () => {

listRemoteStub
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');

Nock('https://raw.githubusercontent.com')
.get('/pkgjs/detect-node-support/HEAD/package.json')
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')))
.get('/pkgjs/detect-node-support/HEAD/.travis.yml')
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')));

const result = await NodeSupport.detect({ repository: 'pkgjs/detect-node-support' });

expect(listRemoteStub.callCount).to.equal(1);
expect(listRemoteStub.args[0]).to.equal([['http://github.com/pkgjs/detect-node-support', 'HEAD']]);

expect(result).to.equal({
name: 'detect-node-support',
version: '0.0.0-development',
commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23',
timestamp: 1580673602000,
travis: {
raw: ['10', '12', '14'],
resolved: {
'10': '10.20.1',
'12': '12.17.0',
'14': '14.3.0'
}
},
engines: '>=10'
});
});

it('leaves out `travis` when no `.travis.yml` present', async () => {

listRemoteStub
Expand Down