Files
shields/scripts/badge-cli.js
Pierre-Yves B 25f8541e5b JSON format modernisation and _shields_test removal (#3272)
* Modernised JSON format and removed _shields_test style

* Added logoWidth and labelColor fields to JSON response

* Reinstated and updated comment

* Extended expectBadge to accept Joi schemas for all fields
2019-04-07 18:57:55 +01:00

46 lines
1.1 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')
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)
}
})()