diff --git a/__snapshots__/make-badge.spec.mjs.js b/__snapshots__/make-badge.spec.js similarity index 100% rename from __snapshots__/make-badge.spec.mjs.js rename to __snapshots__/make-badge.spec.js diff --git a/badge-maker/README.md b/badge-maker/README.md index 1f0d712c51a7f..b9bf6993b7b60 100644 --- a/badge-maker/README.md +++ b/badge-maker/README.md @@ -21,14 +21,6 @@ badge build passed :brightgreen > mybadge.svg ### As a library -With CommonJS in JavaScript, - -```js -const { makeBadge, ValidationError } = require('badge-maker') -``` - -With ESM or TypeScript, - ```ts import { makeBadge, ValidationError } from 'badge-maker' ``` diff --git a/badge-maker/lib/badge-cli.js b/badge-maker/lib/badge-cli.js index a930a01e4c867..ecfef4e4369bb 100755 --- a/badge-maker/lib/badge-cli.js +++ b/badge-maker/lib/badge-cli.js @@ -1,9 +1,7 @@ #!/usr/bin/env node -'use strict' - -const { namedColors } = require('./color') -const { makeBadge } = require('./index') +import { namedColors } from './color.js' +import { makeBadge } from './index.js' if (process.argv.length < 4) { console.log('Usage: badge label message [:color] [@style]') diff --git a/badge-maker/lib/badge-cli.spec.mjs b/badge-maker/lib/badge-cli.spec.js similarity index 100% rename from badge-maker/lib/badge-cli.spec.mjs rename to badge-maker/lib/badge-cli.spec.js diff --git a/badge-maker/lib/badge-renderers.js b/badge-maker/lib/badge-renderers.js index 60d9f08890d47..79ce329cac7ea 100644 --- a/badge-maker/lib/badge-renderers.js +++ b/badge-maker/lib/badge-renderers.js @@ -1,8 +1,8 @@ 'use strict' -const anafanafo = require('anafanafo') -const { brightness } = require('./color') -const { XmlElement, ElementList } = require('./xml') +import anafanafo from 'anafanafo' +import { brightness } from './color.js' +import { XmlElement, ElementList } from './xml.js' // https://github.com/badges/shields/pull/1132 const FONT_SCALE_UP_FACTOR = 10 @@ -984,7 +984,7 @@ function forTheBadge({ ) } -module.exports = { +export default { plastic: params => Plastic.render(params), flat: params => Flat.render(params), 'flat-square': params => FlatSquare.render(params), diff --git a/badge-maker/lib/color.js b/badge-maker/lib/color.js index 3ea1454b7c251..4ae55aebbd10c 100644 --- a/badge-maker/lib/color.js +++ b/badge-maker/lib/color.js @@ -1,9 +1,9 @@ 'use strict' -const { fromString } = require('css-color-converter') +import { fromString } from 'css-color-converter' // When updating these, be sure also to update the list in `badge-maker/README.md`. -const namedColors = { +export const namedColors = { brightgreen: '#4c1', green: '#97ca00', yellow: '#dfb317', @@ -33,7 +33,7 @@ Object.entries(aliases).forEach(([alias, original]) => { // This function returns false for `#ccc`. However `isCSSColor('#ccc')` is // true. const hexColorRegex = /^([\da-f]{3}){1,2}$/i -function isHexColor(s = '') { +export function isHexColor(s = '') { return hexColorRegex.test(s) } @@ -41,7 +41,7 @@ function isCSSColor(color) { return typeof color === 'string' && fromString(color.trim()) } -function normalizeColor(color) { +export function normalizeColor(color) { if (color === undefined) { return undefined } else if (color in namedColors) { @@ -57,7 +57,7 @@ function normalizeColor(color) { } } -function toSvgColor(color) { +export function toSvgColor(color) { const normalized = normalizeColor(color) if (normalized in namedColors) { return namedColors[normalized] @@ -68,7 +68,7 @@ function toSvgColor(color) { } } -function brightness(color) { +export function brightness(color) { if (color) { const cssColor = fromString(color) if (cssColor) { @@ -78,11 +78,3 @@ function brightness(color) { } return 0 } - -module.exports = { - namedColors, - isHexColor, - normalizeColor, - toSvgColor, - brightness, -} diff --git a/badge-maker/lib/color.spec.js b/badge-maker/lib/color.spec.js index 502765b4755dc..10570fe2a6325 100644 --- a/badge-maker/lib/color.spec.js +++ b/badge-maker/lib/color.spec.js @@ -1,12 +1,7 @@ 'use strict' -const { test, given, forCases } = require('sazerac') -const { - isHexColor, - normalizeColor, - toSvgColor, - brightness, -} = require('./color') +import { test, given, forCases } from 'sazerac' +import { isHexColor, normalizeColor, toSvgColor, brightness } from './color.js' test(isHexColor, () => { forCases([given('f00bae'), given('4c1'), given('ABC123')]).expect(true) diff --git a/badge-maker/lib/constants.js b/badge-maker/lib/constants.js index 82f8b1d301ab5..6119380f6b946 100644 --- a/badge-maker/lib/constants.js +++ b/badge-maker/lib/constants.js @@ -1,5 +1 @@ -const DEFAULT_LOGO_HEIGHT = 14 - -module.exports = { - DEFAULT_LOGO_HEIGHT, -} +export const DEFAULT_LOGO_HEIGHT = 14 diff --git a/badge-maker/lib/index.js b/badge-maker/lib/index.js index 881188f411051..53ad208f56168 100644 --- a/badge-maker/lib/index.js +++ b/badge-maker/lib/index.js @@ -3,9 +3,9 @@ * @module badge-maker */ -const _makeBadge = require('./make-badge') +import _makeBadge from './make-badge.js' -class ValidationError extends Error {} +export class ValidationError extends Error {} function _validate(format) { if (format !== Object(format)) { @@ -105,13 +105,8 @@ function _clean(format) { * @returns {string} Badge in SVG format * @see https://github.com/badges/shields/tree/master/badge-maker/README.md */ -function makeBadge(format) { +export function makeBadge(format) { _validate(format) const cleanedFormat = _clean(format) return _makeBadge(cleanedFormat) } - -module.exports = { - makeBadge, - ValidationError, -} diff --git a/badge-maker/lib/index.spec.mjs b/badge-maker/lib/index.spec.js similarity index 100% rename from badge-maker/lib/index.spec.mjs rename to badge-maker/lib/index.spec.js diff --git a/badge-maker/lib/make-badge.js b/badge-maker/lib/make-badge.js index 7119670d47ce0..a02400c2ea068 100644 --- a/badge-maker/lib/make-badge.js +++ b/badge-maker/lib/make-badge.js @@ -1,15 +1,13 @@ -'use strict' - -const { normalizeColor, toSvgColor } = require('./color') -const badgeRenderers = require('./badge-renderers') -const { stripXmlWhitespace } = require('./xml') -const { DEFAULT_LOGO_HEIGHT } = require('./constants') +import { normalizeColor, toSvgColor } from './color.js' +import badgeRenderers from './badge-renderers.js' +import { stripXmlWhitespace } from './xml.js' +import { DEFAULT_LOGO_HEIGHT } from './constants.js' /* note: makeBadge() is fairly thinly wrapped so if we are making changes here it is likely this will impact on the package's public interface in index.js */ -module.exports = function makeBadge({ +export default function makeBadge({ format, style = 'flat', label, diff --git a/badge-maker/lib/make-badge.spec.mjs b/badge-maker/lib/make-badge.spec.js similarity index 100% rename from badge-maker/lib/make-badge.spec.mjs rename to badge-maker/lib/make-badge.spec.js diff --git a/badge-maker/lib/xml.js b/badge-maker/lib/xml.js index 10b8a8994415c..2bc0f5462bd85 100644 --- a/badge-maker/lib/xml.js +++ b/badge-maker/lib/xml.js @@ -2,13 +2,11 @@ * @module */ -'use strict' - -function stripXmlWhitespace(xml) { +export function stripXmlWhitespace(xml) { return xml.replace(/>\s+/g, '>').replace(/<\s+/g, '<').trim() } -function escapeXml(s) { +export function escapeXml(s) { if (typeof s === 'number') { return s } else if (s === undefined || typeof s !== 'string') { @@ -26,7 +24,7 @@ function escapeXml(s) { /** * Representation of an XML element */ -class XmlElement { +export class XmlElement { /** * Xml Element Constructor * @@ -77,7 +75,7 @@ class XmlElement { * Convenience class. Sometimes it is useful to return an object that behaves * like an XmlElement but renders multiple XML tags (not wrapped in a ). */ -class ElementList { +export class ElementList { constructor({ content = [] }) { this.content = content } @@ -92,5 +90,3 @@ class ElementList { ) } } - -module.exports = { escapeXml, stripXmlWhitespace, XmlElement, ElementList } diff --git a/badge-maker/lib/xml.spec.js b/badge-maker/lib/xml.spec.js index 78ea84bada11c..a54e6a6d10cab 100644 --- a/badge-maker/lib/xml.spec.js +++ b/badge-maker/lib/xml.spec.js @@ -1,7 +1,5 @@ -'use strict' - -const { test, given } = require('sazerac') -const { XmlElement } = require('./xml') +import { test, given } from 'sazerac' +import { XmlElement } from './xml.js' function testRender(params) { return new XmlElement(params).render() diff --git a/badge-maker/package.json b/badge-maker/package.json index 1401d836ff443..b138c0af07c69 100644 --- a/badge-maker/package.json +++ b/badge-maker/package.json @@ -1,6 +1,8 @@ { "name": "badge-maker", "version": "4.1.0", + "type": "module", + "exports": "./lib/index.js", "description": "Shields.io badge library", "keywords": [ "GitHub", @@ -10,7 +12,6 @@ "shields.io" ], "types": "index.d.ts", - "main": "lib/index.js", "repository": { "type": "git", "url": "git+https://github.com/badges/shields.git",