diff --git a/lib/loader.js b/lib/loader.js index 09110a2..a1755d5 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -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'); @@ -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) => { @@ -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 { @@ -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; } } diff --git a/lib/package.js b/lib/package.js index 1e11d1f..9c88080 100644 --- a/lib/package.js +++ b/lib/package.js @@ -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 }; diff --git a/test/index.js b/test/index.js index 3e21a8b..79a3f47 100644 --- a/test/index.js +++ b/test/index.js @@ -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