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b,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACjB,OAAO,GAAG,CAAC;IACb,CAAC,EAAE,EAAE,CAAC,CAAC;IACT,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;GAKG;AACI,KAAK,UAAU,uCAAuC,CAC3D,QAAgB,EAChB,OAAgB,EAChB,EAAE,iBAAiB,KAAsC,EAAE;;IAE3D,IACE,OAAA,OAAO,CAAC,QAAQ,0CAAG,CAAC,EAAE,IAAI,MAAK,WAAW;QAC1C,OAAA,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,0CAAE,MAAM,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAChD,iBAAiB,CAAC,CAAC,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,EAC7C,MAAM,MAAK,CAAC,EACd;QACA,MAAM,+BAAkB,CAAC,QAAQ,CAAC,CAAC;KACpC;SAAM;QACL,MAAM,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;KACvC;AACH,CAAC;AAfD,0FAeC","sourcesContent":["import { Attributes, Element, js2xml, xml2js } from 'xml-js';\n\nimport {\n createDirAndWriteFile,\n readFileWithFallback,\n removeFileIfExists,\n} from '../utils/file-utils';\n\ntype ExplicitNewValue<T> = { newValue: T };\ntype WithExplicitNewValue<T> = T | ExplicitNewValue<T>;\n\ntype ExpectedElementAttributes = Record<string, WithExplicitNewValue<string | number | undefined>>;\ntype WithExplicitIndex<T> = T & { idx?: number };\ntype WithDeletionFlag<T> = T & { deletionFlag?: boolean };\n\ntype ExpectedElements = WithExplicitNewValue<\n WithExplicitIndex<WithDeletionFlag<ExpectedElement>>[]\n>;\n\nexport type ExpectedElementType = {\n name: string;\n attributes?: ExpectedElementAttributes;\n elements?: ExpectedElements;\n};\n\nexport type ExpectedElementsType = {\n elements: ExpectedElements;\n};\n\nexport type ExpectedCommentType = {\n comment: string;\n};\n\nexport type ExpectedTextType = {\n text: string | number | boolean;\n};\n\nexport type ExpectedElement =\n | ExpectedElementType\n | ExpectedElementsType\n | ExpectedCommentType\n | ExpectedTextType;\n\nfunction isElementType(el: ExpectedElement): el is ExpectedElementType {\n return (el as ExpectedElementType).name !== undefined;\n}\n\nfunction isElementsType(el: ExpectedElement): el is ExpectedElementsType {\n return !(el as ExpectedElementType).name && Boolean((el as ExpectedElementsType).elements);\n}\n\nfunction isCommentType(el: ExpectedElement): el is ExpectedCommentType {\n return (el as ExpectedCommentType).comment !== undefined;\n}\n\nfunction isTextType(el: ExpectedElement): el is ExpectedTextType {\n return (el as ExpectedTextType).text !== undefined;\n}\n\nfunction isExplicitNewValue<T>(el: WithExplicitNewValue<T>): el is ExplicitNewValue<T> {\n // @ts-ignore\n return typeof el === 'object' && el.hasOwnProperty('newValue');\n}\n\nfunction unboxExplicitNewValue<T>(el: WithExplicitNewValue<T>): T {\n return isExplicitNewValue(el) ? el.newValue : el;\n}\n\nfunction compareElements(element: Element, expectedElement: ExpectedElement): boolean {\n if (isTextType(expectedElement)) {\n return element.type === 'text';\n }\n\n if (isCommentType(expectedElement)) {\n return element.type === 'comment' && element.comment?.trim() === expectedElement.comment.trim();\n }\n\n if (isElementType(expectedElement) && element.type === 'element') {\n if (expectedElement.name !== element.name) {\n return false;\n }\n\n if (!element.attributes) {\n return true;\n }\n\n for (const [key, value] of Object.entries(\n (expectedElement as ExpectedElementType).attributes || {}\n )) {\n if (isExplicitNewValue(value)) {\n // this attribute has to be overridden\n continue;\n }\n if (element.attributes[key] !== value) {\n return false;\n }\n }\n return true;\n }\n\n return false;\n}\n\nfunction sortWithExplicitIndex<T>(elements?: WithExplicitIndex<T>[]): T[] | undefined {\n if (!elements) {\n return;\n }\n const result: T[] = new Array(elements.length);\n const elementsWithExplicitIndices = elements.filter(({ idx }) => idx !== undefined);\n const elementsWithoutExplicitIndices = elements.filter(({ idx }) => idx === undefined);\n elementsWithoutExplicitIndices.forEach((el, idx) => (result[idx] = el));\n elementsWithExplicitIndices.forEach(({ idx, ...el }, i) => {\n // @ts-ignore\n result.splice(idx ?? i, 0, el);\n });\n return result;\n}\n\nfunction mergeXmlElementsLists(\n current?: Element[],\n expected?: ExpectedElements\n): Element[] | undefined {\n if (isExplicitNewValue(expected) || !current) {\n const sortedExpected = sortWithExplicitIndex(unboxExplicitNewValue(expected));\n return sortedExpected?.map(convertToElement);\n }\n if (!expected) {\n return current;\n }\n\n const result: WithExplicitIndex<WithDeletionFlag<Element>>[] = [];\n\n for (const currentElement of current) {\n const idxInExpected = expected.findIndex(el => compareElements(currentElement, el));\n if (idxInExpected !== -1) {\n const { idx, ...element } = expected.splice(idxInExpected, 1)[0];\n if (!element.deletionFlag) {\n result.push({ idx, ...mergeXmlElements(currentElement, element) });\n }\n } else {\n result.push(currentElement);\n }\n }\n result.push(\n ...expected\n .filter(({ deletionFlag }) => !deletionFlag)\n .map(({ idx, ...el }) => ({ idx, ...convertToElement(el) }))\n );\n return sortWithExplicitIndex(result);\n}\n\nfunction convertToElement({\n idx,\n ...expectedElement\n}: WithExplicitIndex<ExpectedElement>): Element {\n // @ts-ignore\n if (expectedElement.deletionFlag) {\n throw new Error('Cannot convert ExpectedElement to Element when deletionFlag is set');\n }\n\n if (isCommentType(expectedElement)) {\n return {\n ...expectedElement,\n type: 'comment',\n };\n }\n if (isTextType(expectedElement)) {\n return {\n ...expectedElement,\n type: 'text',\n };\n }\n if (isElementsType(expectedElement)) {\n return {\n elements: unboxExplicitNewValue(expectedElement.elements)\n .filter(({ deletionFlag }) => !deletionFlag)\n .map(convertToElement),\n type: 'element',\n };\n }\n const { elements, attributes, ...expectedRest } = expectedElement;\n const result: Element = {\n ...expectedRest,\n type: 'element',\n };\n if (attributes) {\n result.attributes = convertExpectedAttributes(attributes);\n }\n if (elements) {\n result.elements = unboxExplicitNewValue(elements)\n .filter(({ deletionFlag }) => !deletionFlag)\n .map(convertToElement);\n }\n return result;\n}\n\nfunction convertExpectedAttributes(\n expectedAttributes?: ExpectedElementAttributes\n): Attributes | undefined {\n if (expectedAttributes) {\n return Object.entries(expectedAttributes).reduce(\n (acc, [key, value]) => ({\n ...acc,\n [key]: unboxExplicitNewValue(value),\n }),\n {}\n );\n }\n return undefined;\n}\n\nfunction mergeAndConvertToElement(\n { attributes: currentAttributes, ...currentRest }: Omit<Element, 'elements'>,\n { attributes: expectedAttributes, ...expectedRest }: Omit<ExpectedElementType, 'elements'>\n): Element {\n const result: Element = {\n ...currentRest,\n ...expectedRest,\n };\n const attributes = (currentAttributes || expectedAttributes) && {\n ...currentAttributes,\n ...convertExpectedAttributes(expectedAttributes),\n };\n if (attributes) {\n result.attributes = attributes;\n }\n return result;\n}\n\nfunction deepEqual(e1: any, e2: any) {\n if ((!e1 && e2) || (e1 && !e2)) {\n return false;\n } else if (!e1 && !e2) {\n return true;\n }\n\n if (Object.keys(e1).length !== Object.keys(e2).length) {\n return false;\n }\n\n for (const [key, val1] of Object.entries(e1)) {\n const val2 = e2[key];\n const areObjects = isObject(val1) && isObject(val2);\n if ((areObjects && !deepEqual(val1, val2)) || (!areObjects && val1 !== val2)) {\n return false;\n }\n }\n\n return true;\n}\n\nfunction isObject(element: any) {\n return element && typeof element === 'object';\n}\n\n/**\n * Assumption is that elements are `equal` semantically\n */\nexport function mergeXmlElements(current: Element, expected: ExpectedElement): Element {\n if (isCommentType(expected)) {\n return {\n ...current,\n ...expected,\n type: 'comment',\n };\n }\n\n if (isTextType(expected)) {\n return {\n ...current,\n ...expected,\n type: 'text',\n };\n }\n\n if (isElementsType(expected)) {\n const result = {\n ...current,\n type: 'element',\n };\n const elements = mergeXmlElementsLists(current.elements, expected.elements);\n if (elements) {\n result.elements = elements;\n }\n return result;\n }\n\n const { elements: currentElements, ...currentRest } = current;\n const { elements: expectedElements, ...expectedRest } = expected;\n\n const elements = mergeXmlElementsLists(current.elements, expected.elements);\n const result = {\n ...mergeAndConvertToElement(currentRest, expectedRest),\n type: 'element',\n };\n if (elements) {\n result.elements = elements;\n }\n return result;\n}\n\n/**\n * @param filePath\n * @param fallbackContent\n */\nexport async function readXmlFile(\n filePath: string,\n fallbackContent: Element | string = `<?xml version=\"1.0\" encoding=\"utf-8\"?>`\n): Promise<Element> {\n const fileContent = await readFileWithFallback(\n filePath,\n typeof fallbackContent === 'string' ? fallbackContent : 'fallbackToElement'\n );\n if (fileContent === 'fallbackToElement' && typeof fallbackContent === 'object') {\n return fallbackContent;\n }\n const fileXml = xml2js(fileContent);\n return fileXml as Element;\n}\n\nexport async function writeXmlFile(filePath: string, xml: Element) {\n const fileXml = js2xml(xml, { indentAttributes: true, spaces: 2 });\n const correctedFile = fileXml.replace(\n /(?<openTag><[^\\s]+)\\n *(?<firstAttribute> [^\\s]+=\".+?\")\\n *((?<secondAttribute> [^\\s]+=\".+?\")\\n *)?(?<closeTag>[/?]?>)/g,\n '$1$2$4$5'\n );\n await createDirAndWriteFile(filePath, `${correctedFile}\\n`);\n}\n\n/**\n * Checks whether two xmlElements are equal in terms of their structure\n */\nexport function xmlElementsEqual(\n a: Element,\n b: Element,\n { disregardComments = true }: { disregardComments?: boolean } = {}\n): boolean {\n const filteredA = !disregardComments ? a : removeComments(a);\n const filteredB = !disregardComments ? b : removeComments(b);\n return deepEqual(filteredA, filteredB);\n}\n\nfunction removeComments(e: Element): Element | undefined {\n if (e.type === 'comment') {\n return;\n }\n const result = Object.entries(e)\n .map(([key, value]): [string, any] => {\n if (key === 'elements' && Array.isArray(value)) {\n const filteredValue = value\n .map(removeComments)\n .filter((el): el is Element => el !== undefined);\n return [key, filteredValue.length > 0 ? filteredValue : undefined];\n }\n return [key, value];\n })\n .filter(([_, value]) => value !== undefined)\n .reduce((acc, [key, value]) => {\n // @ts-ignore\n acc[key] = value;\n return acc;\n }, {});\n return result;\n}\n\n/**\n * Check if given `element` has some meaningful data:\n * - if so: write it to the file\n * - if no: remove file completely\n * Function assumes that the structure of the input `element` is correct (`element.elements[name = resources]`).\n */\nexport async function writeXmlFileOrRemoveFileUponNoResources(\n filePath: string,\n element: Element,\n { disregardComments }: { disregardComments?: boolean } = {}\n) {\n if (\n element.elements?.[0].name === 'resources' &&\n element.elements[0].elements?.filter(({ type }) =>\n disregardComments ? type !== 'comment' : true\n ).length === 0\n ) {\n await removeFileIfExists(filePath);\n } else {\n await writeXmlFile(filePath, element);\n }\n}\n"]} |