-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (40 loc) · 1.14 KB
/
index.js
File metadata and controls
42 lines (40 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import FileParser from './FileParser.js';
import Gist from './Gist.js';
import GithubRaw from './GithubRaw.js';
import GithubRelease from './GithubRelease.js';
export default function createParser({
downloadURL,
updateURL,
}) {
if (!downloadURL && !updateURL) throw new Error('URL not provided');
const uri = updateURL || downloadURL;
if (typeof uri !== 'string') throw new Error('URL must be a string');
const Parser = getParser(uri);
if (Parser === GithubRelease) {
if (uri.split('/').length === 2) {
return new Parser(uri, downloadURL);
}
const [, owner, repo] = new URL(uri).pathname.split('/');
return new Parser(`${owner}/${repo}`, downloadURL);
}
return new Parser(uri, downloadURL);
}
function getParser(uri = '') {
if (uri.split('/').length === 2) {
return GithubRelease;
}
const url = new URL(uri);
if (url.hostname === 'gist.github.com') {
return Gist;
}
if (url.hostname === 'github.com') {
if (uri.includes('/releases/')) {
return GithubRelease;
}
return GithubRaw;
}
if (url.hostname === 'raw.githubusercontent.com') {
return GithubRaw;
}
return FileParser;
}