From 2b1d1bed147148bc996f03476caddfe70e129f6a Mon Sep 17 00:00:00 2001 From: jameswilddev Date: Wed, 7 Jul 2021 23:36:15 +0100 Subject: [PATCH] Fix breakage due to internal SVGO changes. --- .../convert-svg-document-to-def-step/index.ts | 42 ++++++------------- 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/@shanzhai/convert-svg-document-to-def-step/index.ts b/@shanzhai/convert-svg-document-to-def-step/index.ts index 57d5c2c8..f7ed9f9d 100644 --- a/@shanzhai/convert-svg-document-to-def-step/index.ts +++ b/@shanzhai/convert-svg-document-to-def-step/index.ts @@ -6,24 +6,13 @@ const JS2SVG = require("svgo/lib/svgo/js2svg"); import { Input, Output, ActionStep } from "@shanzhai/interfaces"; -type AnySvgElement = SvgElementWithTextContent | SvgElementWithArrayContent; - -type SvgElement = { - content: TContent; - +type ParsedSvg = { + children: ReadonlyArray; readonly attrs: { - id: string; + [key: string]: string; }; }; -type SvgElementWithTextContent = SvgElement; - -type SvgElementWithArrayContent = SvgElement; - -type SvgElementWithArrayOfArraysContent = SvgElement< - SvgElementWithArrayContent[] ->; - export class ConvertSvgDocumentToDefStep extends ActionStep { constructor( public readonly svgDocument: Input, @@ -35,35 +24,28 @@ export class ConvertSvgDocumentToDefStep extends ActionStep { async execute(): Promise { const svgDocument = await this.svgDocument.get(); - const root = await new Promise((resolve) => - svg2js(svgDocument, resolve) - ); + const root = svg2js(svgDocument) as ParsedSvg; - const children = (root as SvgElementWithArrayOfArraysContent).content[0] - .content; + const children = root.children[0].children; - if (children === undefined) { + if (children.length === 0) { throw new Error(`Empty SVG documents cannot be converted into defs.`); } if (children.length === 1) { // Remove the wrapping (there's already a single root). - root.content = children; + root.children = children; } else { // Replace the wrapping with a . - const groupSource = await new Promise( - (resolve) => svg2js(``, resolve) - ); - root.content = groupSource.content[0].content; - groupSource.content[0].content[0].content = children; + const groupSource = svg2js(``) as ParsedSvg; + root.children = groupSource.children[0].children; + groupSource.children[0].children[0].children = children; } // Inject a blank ID. This should be safely replaceable later down // the line, as we've already filtered out IDs using SVGO. - const idSource = await new Promise((resolve) => - svg2js(``, resolve) - ); - root.content[0].attrs.id = idSource.content[0].attrs.id; + const idSource = svg2js(``) as ParsedSvg; + root.children[0].attrs.id = idSource.children[0].attrs.id; const generated = new JS2SVG(root).data; await this.svgDef.set(generated);