-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (32 loc) · 1.05 KB
/
index.js
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
const badgen = require('badgen')
const {send} = require('micro')
module.exports = async (req, res) => {
try {
if (req.url === '/favicon.ico' || req.url === '/') {
send(res, 404, '404')
return
}
const url = require('url').parse(req.url, true)
const packageName = req.url.replace(/\?.*/, '').substr(1)
const pkgResponse = await (
require('package-json')(packageName, {fullMetadata: true})
.catch(() => undefined)
)
const error = !pkgResponse
const hasPostinstaller = !error && Boolean(pkgResponse.postinstaller)
const status = error ? 'error' : hasPostinstaller ? 'ready' : 'missing'
res.setHeader('Content-type', 'image/svg+xml')
res.setHeader('X-Status', status)
send(res, error ? 404 : 200, badgen({
status,
color : error ? 'red' : hasPostinstaller ? 'green' : 'orange',
style : url.query.style,
subject: 'postinstaller'
}))
res.end()
} catch (error) {
console.log(error)
send(res, error.statusCode || 500, error.message || 'Error')
res.end()
}
}