forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbadge-cli.js
More file actions
45 lines (41 loc) · 1.19 KB
/
Copy pathbadge-cli.js
File metadata and controls
45 lines (41 loc) · 1.19 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
43
44
45
import { fileURLToPath, URL } from 'url'
import config from 'config'
import got from 'got'
import emojic from 'emojic'
import Server from '../core/server/server.js'
import trace from '../core/base-service/trace.js'
function normalizeBadgeUrl(url) {
// Provide a base URL in order to accept fragments.
const { pathname, searchParams } = new URL(url, 'http://example.com')
let newPath = pathname.replace('.svg', '')
if (!newPath.endsWith('.json')) {
newPath = `${newPath}.json`
}
return `${newPath}?${searchParams.toString()}`
}
async function traceBadge(badgeUrl) {
const server = new Server(config.util.toObject())
await server.start()
const body = await got(
`${server.baseUrl.replace(/\/$/, '')}${badgeUrl}`,
).json()
trace.logTrace('outbound', emojic.shield, 'Rendered badge', body)
await server.stop()
}
async function main() {
if (process.argv.length < 3) {
console.error(`Usage: node ${fileURLToPath(import.meta.url)} badge-url`)
process.exit(1)
}
const [, , url] = process.argv
const normalized = normalizeBadgeUrl(url)
await traceBadge(normalized)
}
;(async () => {
try {
await main()
} catch (e) {
console.error(e)
process.exit(1)
}
})()