-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm-to-gh.html
65 lines (62 loc) · 1.44 KB
/
npm-to-gh.html
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<style>
body{
margin: 0;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
font-size: 3em;
font-family: sans-serif;
color: rgba(20,20,20,.9);
}
@keyframes loader {
0% { content: "" }
25% { content: "." }
50% { content: ".." }
75% { content: "..." }
100% { content: "..." }
}
#loader:empty::after {
content: "";
animation: loader 2s linear infinite;
display: inline-block;
}
em {
margin-right: 1rem;
}
nav {
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
}
nav a {
margin: 0 8px;
font-size: .8em;
}
nav:empty::after {
content: "No results";
}
</style>
<div id="loader"></div>
<script>
const url = new URL(location.href);
const q = url.searchParams.get('q');
if (!q) {
loader.innerHTML = `<em>URL format:</em><code>?q=npm-package-name</code>`;
throw 'missing q param';
}
fetch('https://api.npms.io/v2/package/' + q)
.then(async r => {
if (!r.ok) {
const d = await fetch('https://api.npms.io/v2/search?q=' + q).then(r => r.json());
loader.innerHTML = `<nav>${d.results.map(({package: p}) => `<a href="${p.links.repository}">${p.name}</a>`).join('')}</nav>`;
return;
}
const d = await r.json();
if (!d.collected.metadata.links.repository) {
loader.innerHTML = `No GH found <a href="${d.collected.metadata.links.npm}">${d.collected.metadata.name}</a>`;
return;
}
location.href = d.collected.metadata.links.repository;
})
</script>