Files
shields/scripts/badge-cli.js
Paul Melnikow 4bd16f93e8 Sort imports and requires (#3056)
This will definitely save time, and ensure more uniformity.

It moves the `createServiceTester()` calls to a different place from where I'd like them, though I'm happy to have them checked by the linter.

Closes #2701
2019-02-21 22:14:40 -05:00

47 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')
const newPath = pathname.replace('.svg', '.json')
searchParams.set('style', '_shields_test')
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)
}
})()