Skip to content

Commit

Permalink
Add cran support
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbuck committed Oct 1, 2020
1 parent 2758891 commit 477a597
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Type must be one of:
- `java`
- `go`
- `pub`
- `cran`
- `ping`

Response:
Expand Down
15 changes: 15 additions & 0 deletions functional.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,19 @@ describe('functional', () => {
'path',
'https://github.com/dart-lang/path',
);
testBulk(
'cran',
'fracdiff',
'https://github.com/mmaechler/fracdiff',
);
testBulk(
'cran',
'usethis',
'https://github.com/r-lib/usethis',
);
testBulk(
'cran',
'boot',
'https://github.com/cran/boot',
);
});
7 changes: 7 additions & 0 deletions src/registries/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
"/project_uri"
]
},
"cran": {
"registry": "https://crandb.r-pkg.org/%s",
"fallback": "https://github.com/cran/%s",
"xpaths": [
"/URL"
]
},
"npm": {
"registry": "https://registry.npmjs.org/%s",
"fallback": "https://www.npmjs.com/package/%s",
Expand Down
10 changes: 9 additions & 1 deletion src/registries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const xpathHelper = require('./xpath-helper');
const registryConfig = require('./config.json');
const cache = require('../utils/cache');
const log = require('../utils/log');
const { prioritiesHost } = require('../utils/url');

async function resolve(type, packageName) {
const cacheKey = `${type}_${packageName}`;
Expand Down Expand Up @@ -47,7 +48,7 @@ async function resolve(type, packageName) {
return;
}

const urls = xpathHelper(json, config.xpaths);
let urls = xpathHelper(json, config.xpaths);

if (type === 'npm') {
if (json.repository && json.repository.directory) {
Expand All @@ -63,6 +64,13 @@ async function resolve(type, packageName) {
}
}

if (type === 'cran') {
// Some packages export multiple urls seperated by comma.
urls = urls.map(url => url.split(',').map(s => s.trim())).flat()

urls = prioritiesHost('https://github.com', urls);
}

const validUrls = urls.map((bestMatchUrl) => {
try {
let url = repositoryUrl(bestMatchUrl);
Expand Down
18 changes: 18 additions & 0 deletions src/utils/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

function prioritiesHost(host, urls = []) {
let insertIndex = 0;
return urls.reduce((memo, url) => {
if (url.includes(host)) {
memo.splice(insertIndex++, 0, url)
} else {
memo.push(url)
}
return memo;
}, [])
}

module.exports = {
prioritiesHost,
}


39 changes: 39 additions & 0 deletions src/utils/url.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { prioritiesHost } = require('./url.js');

const inputUrls = [
'https://foo.com',
'https://github.com/foo',
'https://bar.com',
'https://github.com/bar',
]

describe('url', () => {
describe('prioritiesHost', () => {
it('priorities github.com', function() {
expect(prioritiesHost('github.com', inputUrls)).toStrictEqual([
'https://github.com/foo',
'https://github.com/bar',
'https://foo.com',
'https://bar.com',
]);
})

it('priorities foo.com', function() {
expect(prioritiesHost('foo.com', inputUrls)).toStrictEqual([
'https://foo.com',
'https://github.com/foo',
'https://bar.com',
'https://github.com/bar',
]);
})

it('priorities bar.com', function() {
expect(prioritiesHost('bar.com', inputUrls)).toStrictEqual([
'https://bar.com',
'https://foo.com',
'https://github.com/foo',
'https://github.com/bar',
]);
})
});
});

0 comments on commit 477a597

Please sign in to comment.