Files
shields/scripts/badge-cli.js
chris48s 3a19a2f4e5 fix badge CLI debug script (#4865)
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2020-04-04 18:57:27 +00:00

49 lines
1.2 KiB
JavaScript

'use strict'
const { URL } = require('url')
const config = require('config').util.toObject()
const got = require('got')
const emojic = require('emojic')
const Server = require('../core/server/server')
const trace = require('../core/base-service/trace')
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)
await server.start()
const { body } = await got(
`${server.baseUrl.replace(/\/$/, '')}${badgeUrl}`,
{ json: true }
)
trace.logTrace('outbound', emojic.shield, 'Rendered badge', body)
await server.stop()
}
async function main() {
if (process.argv.length < 3) {
console.error(`Usage: node ${__filename} 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)
}
})()