UNPKG

github-action-readme-generator

Version:

The docs generator for GitHub Actions. Auto-syncs action.yml to README.md with 8 sections: inputs, outputs, usage, badges, branding & more. Works as CLI or GitHub Action.

1 lines 10.2 kB
{"version":3,"file":"svg-editor.mjs","sourceRoot":"","sources":["../../src/svg-editor.mts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAGlC,OAAO,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,KAAK,OAAO,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,eAAe,EAAoC,MAAM,QAAQ,CAAC,CAAC,oBAAoB;AAGhG,OAAO,EACL,gCAAgC,EAChC,mBAAmB,EACnB,kBAAkB,EAClB,8BAA8B,EAC9B,6BAA6B,EAC7B,YAAY,EACZ,WAAW,GACZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,OAAO,MAAM,oBAAoB,CAAC;AAEzC;;GAEG;AAEH,MAAM,CAAC,OAAO,OAAO,SAAS;IACpB,GAAG,CAAU;IAEb,MAAM,CAAa;IAEnB,MAAM,CAAa;IAEnB,QAAQ,CAAe;IAE/B;;OAEG;IACH;QACE,IAAI,CAAC,GAAG,GAAG,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC;IACtC,CAAC;IAED;;OAEG;IACH,OAAO;QACL,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,IAAI,CAAC,MAAM,GAAG,eAAe,EAAE,CAAC;YAChC,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC;YACjC,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;YACtC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;gBACjB,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC,QAAQ,CAAC,eAAe,CAAc,CAAC;YAC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B,wDAAwD;QACxD,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QACrC,YAAY,CAAC,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QAEtF,gEAAgE;QAChE,MAAM,OAAO,GAAG,YAAY,CAAC,MAAM,EAAE,CAAC;QACtC,OAAO,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAEzC,oDAAoD;QACpD,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YAC9C,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;QAC1C,CAAC;QAED,6DAA6D;QAC7D,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;QAE9D,2BAA2B;QAC3B,OAAO,CAAC,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;QAEpC,qEAAqE;QACrE,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;QAEhC,6DAA6D;QAC7D,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAErC,8BAA8B;QAC9B,OAAO,CAAC,wDAAwD,EAAE,MAAM,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC,IAAI,CACxF,IAAI,CACL,CAAC;IACJ,CAAC;CACF","sourcesContent":["/**\n * This TypeScript code imports necessary modules and defines a class named 'SVGEditor' for generating SVG images.\n * The class has methods for initializing the SVG window, generating SVG content, and writing SVG files.\n * It utilizes various packages such as 'fs', 'path', '@svgdotjs/svg.js', 'feather-icons', and 'svgdom' for SVG manipulation and file operations.\n * The class also defines interfaces for badges and brand colors.\n */\n\nimport * as fs from 'node:fs';\nimport * as path from 'node:path';\n\nimport type { Container } from '@svgdotjs/svg.js';\nimport { registerWindow, SVG } from '@svgdotjs/svg.js';\nimport type { FeatherIconNames } from 'feather-icons';\nimport * as feather from 'feather-icons';\nimport { createSVGWindow, type SVGDocument, type SVGWindow } from 'svgdom'; /// main-module.js';\n\nimport type { BrandColors } from './constants.js';\nimport {\n brandingSquareEdgeLengthInPixels,\n DEFAULT_BRAND_COLOR,\n DEFAULT_BRAND_ICON,\n GITHUB_ACTIONS_BRANDING_COLORS,\n GITHUB_ACTIONS_BRANDING_ICONS,\n isValidColor,\n isValidIcon,\n} from './constants.js';\nimport LogTask from './logtask/index.js';\n\n/**\n * Utility class for generating SVG images.\n */\n\nexport default class SVGEditor {\n private log: LogTask;\n\n private window?: SVGWindow;\n\n private canvas?: Container;\n\n private document?: SVGDocument;\n\n /**\n * Initializes a new SVGEditor instance.\n */\n constructor() {\n this.log = new LogTask('SVGEditor');\n }\n\n /**\n * Initializes the SVG window, document, and canvas if not already set up.\n */\n initSVG(): void {\n if (!this.window) {\n this.window = createSVGWindow();\n const { document } = this.window;\n registerWindow(this.window, document);\n if (!this.canvas) {\n this.canvas = SVG(document.documentElement) as Container;\n }\n }\n }\n\n /**\n * Generates a branded SVG image.\n * @param {string | undefined} svgPath - Path to write the generated SVG file to.\n * @param {Partial<FeatherIconNames>} icon - Name of the icon to use.\n * @param {Partial<BrandColors>} bgcolor - Background color for the image.\n * @returns {Promise<void>} A promise that resolves when the image is generated.\n */\n generateSvgImage(\n svgPath: string | undefined,\n icon: Partial<FeatherIconNames> = DEFAULT_BRAND_ICON,\n bgcolor: Partial<BrandColors> = DEFAULT_BRAND_COLOR\n ): void {\n if (!svgPath || svgPath.length === 0) {\n this.log.debug('No svgPath provided');\n return;\n }\n\n if (!isValidIcon(icon)) {\n this.log.error(`Valid Branding Icon Names: ${GITHUB_ACTIONS_BRANDING_ICONS}`);\n this.log.fail(`Invalid icon name: ${icon}`);\n return;\n }\n if (!isValidColor(bgcolor)) {\n this.log.error(`Valid Branding Colors: ${GITHUB_ACTIONS_BRANDING_COLORS}`);\n this.log.fail('Invalid branding color');\n return;\n }\n this.log.info(`SVG to generate ${icon} at ${svgPath} with color ${bgcolor}.`);\n // Initialize SVG\n this.initSVG();\n // Generate SVG content\n const svgContent = this.generateSVGContent(icon, bgcolor);\n\n // Write SVG file\n this.writeSVGFile(svgPath, svgContent);\n\n this.log.debug('SVG image generated successfully');\n }\n\n /**\n * Writes the SVG xml to disk.\n * @param {string} svgPath - File path to save the SVG to.\n * @param {string} svgContent - The XML for the SVG file.\n */\n writeSVGFile(svgPath: string, svgContent: string): void {\n fs.mkdirSync(path.dirname(svgPath), { recursive: true });\n this.log.debug(`Writing SVG file to ${svgPath}`);\n fs.writeFile(svgPath, svgContent, 'utf8', () => {\n return this.log.debug(`SVG image generated: ${svgPath}`);\n });\n }\n\n /**\n * Generates the SVG content for the branding image.\n * @param {FeatherIconNames} icon - Name of the icon to use.\n * @param {BrandColors} color - Background color for the image.\n * @param {number} outerViewBox - Size of the canvas for the image.\n * @returns {string} The generated SVG content.\n */\n generateSVGContent(\n icon: FeatherIconNames,\n color: BrandColors,\n outerViewBox: number = 100\n ): string {\n const { canvas, log } = this;\n // Validate canvas\n if (!canvas) {\n log.fail('Canvas not initialized');\n return '';\n }\n\n const svgData = feather.icons[icon];\n log.debug(`SVG data to ingest: ${svgData.toSvg()}`);\n\n canvas.clear();\n\n // Create a canvas that is `outerViewBox` x `outerViewBox`\n canvas\n .size(brandingSquareEdgeLengthInPixels, brandingSquareEdgeLengthInPixels)\n .viewbox(`0 0 ${outerViewBox} ${outerViewBox}`)\n .fill('none');\n\n // Create a 'color' circle that touches the edges of the canvas\n const circleSize = outerViewBox / 2;\n canvas\n .circle('50%')\n .fill(color)\n .radius(circleSize)\n .cx(circleSize)\n .cy(circleSize)\n .stroke({ width: 0 });\n\n // Create an svg box that is half the size of the parent\n const iconsvgOuter = canvas.nested();\n iconsvgOuter.attr('overflow', 'visible').height('50%').width('50%').x('25%').y('25%');\n\n // create a nested svg and add the feather-icon paths to the svg\n const iconsvg = iconsvgOuter.nested();\n iconsvg.id('icon').svg(svgData.contents);\n\n // Append all of the attributes from the fether-icon\n for (const attr of Object.keys(svgData.attrs)) {\n iconsvg.attr(attr, svgData.attrs[attr]);\n }\n\n // invert the stroke color if it matches the background color\n iconsvg.stroke(color.startsWith('white') ? 'white' : 'black');\n\n // remove the edge clipping\n iconsvg.attr('overflow', 'visible');\n\n // Make the viewbox of the svg match the exact dimensions of the icon\n iconsvg.viewbox(iconsvg.bbox());\n\n // Make the svg icon center itself vertically and horozonally\n iconsvg.height('auto').width('auto');\n\n // return the xml file content\n return ['<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>', canvas.svg(), '\\n'].join(\n '\\n'\n );\n }\n}\n"]}