You've already forked dynamic-badges-action
This adds the posibility of saving an SVG badge generated by the same shields.io dirictly to the gist. Instead of prepering a JSON file to be sent to their service, we use their library directly, which outputs an SVG file that we can save to the user’s gist. Filenames ending in `.svg` will use this library automatically. Additionally there is a major refactoring where the older `node:http` library has been swapped out for `fetch`. Also swap from node 16 to node 20 fixes #24
82 lines
2.1 KiB
JavaScript
82 lines
2.1 KiB
JavaScript
'use strict'
|
|
/**
|
|
* @module badge-maker
|
|
*/
|
|
|
|
const _makeBadge = require('./make-badge')
|
|
|
|
class ValidationError extends Error {}
|
|
|
|
function _validate(format) {
|
|
if (format !== Object(format)) {
|
|
throw new ValidationError('makeBadge takes an argument of type object')
|
|
}
|
|
|
|
if (!('message' in format)) {
|
|
throw new ValidationError('Field `message` is required')
|
|
}
|
|
|
|
const stringFields = ['labelColor', 'color', 'message', 'label']
|
|
stringFields.forEach(function (field) {
|
|
if (field in format && typeof format[field] !== 'string') {
|
|
throw new ValidationError(`Field \`${field}\` must be of type string`)
|
|
}
|
|
})
|
|
|
|
const styleValues = [
|
|
'plastic',
|
|
'flat',
|
|
'flat-square',
|
|
'for-the-badge',
|
|
'social',
|
|
]
|
|
if ('style' in format && !styleValues.includes(format.style)) {
|
|
throw new ValidationError(
|
|
`Field \`style\` must be one of (${styleValues.toString()})`
|
|
)
|
|
}
|
|
}
|
|
|
|
function _clean(format) {
|
|
const expectedKeys = ['label', 'message', 'labelColor', 'color', 'style']
|
|
|
|
const cleaned = {}
|
|
Object.keys(format).forEach(key => {
|
|
if (format[key] != null && expectedKeys.includes(key)) {
|
|
cleaned[key] = format[key]
|
|
} else {
|
|
throw new ValidationError(
|
|
`Unexpected field '${key}'. Allowed values are (${expectedKeys.toString()})`
|
|
)
|
|
}
|
|
})
|
|
|
|
// Legacy.
|
|
cleaned.label = cleaned.label || ''
|
|
|
|
return cleaned
|
|
}
|
|
|
|
/**
|
|
* Create a badge
|
|
*
|
|
* @param {object} format Object specifying badge data
|
|
* @param {string} format.label (Optional) Badge label (e.g: 'build')
|
|
* @param {string} format.message (Required) Badge message (e.g: 'passing')
|
|
* @param {string} format.labelColor (Optional) Label color
|
|
* @param {string} format.color (Optional) Message color
|
|
* @param {string} format.style (Optional) Visual style e.g: 'flat'
|
|
* @returns {string} Badge in SVG format
|
|
* @see https://github.com/badges/shields/tree/master/badge-maker/README.md
|
|
*/
|
|
function makeBadge(format) {
|
|
_validate(format)
|
|
const cleanedFormat = _clean(format)
|
|
return _makeBadge(cleanedFormat)
|
|
}
|
|
|
|
module.exports = {
|
|
makeBadge,
|
|
ValidationError,
|
|
}
|