'use strict'; var log4js = require('log4js'); var node_fs = require('node:fs'); var promises = require('node:fs/promises'); var node_path = require('node:path'); var ignore = require('ignore'); var xmlDisassembler = require('xml-disassembler'); var fastXmlParser = require('fast-xml-parser'); /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; function disassembleHandler(filePath, uniqueIdElements, prePurge, postPurge, ignorePath) { return __awaiter(this, void 0, void 0, function* () { const handler = new xmlDisassembler.DisassembleXMLFileHandler(); yield handler.disassemble({ filePath, uniqueIdElements, prePurge, postPurge, ignorePath, }); }); } const INDENT = " "; const XML_PARSER_OPTION = { commentPropName: "!---", ignoreAttributes: false, ignoreNameSpace: false, parseTagValue: false, parseNodeValue: false, parseAttributeValue: false, trimValues: true, processEntities: false, cdataPropName: "![CDATA[", }; const JSON_PARSER_OPTION = Object.assign(Object.assign({}, XML_PARSER_OPTION), { format: true, indentBy: INDENT, suppressBooleanAttributes: false, suppressEmptyNode: false }); function transform2JSON(xmlPath) { return __awaiter(this, void 0, void 0, function* () { const subFiles = yield promises.readdir(xmlPath); for (const subFile of subFiles) { const subFilePath = node_path.join(xmlPath, subFile); if ((yield promises.stat(subFilePath)).isDirectory()) { yield transform2JSON(subFilePath); } else if ((yield promises.stat(subFilePath)).isFile() && subFilePath.endsWith(".xml")) { yield writeJSON(subFilePath); yield promises.rm(subFilePath); } } }); } function writeJSON(xmlPath) { return __awaiter(this, void 0, void 0, function* () { const xmlParser = new fastXmlParser.XMLParser(XML_PARSER_OPTION); const xmlContent = yield promises.readFile(xmlPath, "utf-8"); const xmlParsed = xmlParser.parse(xmlContent, true); const jsonString = JSON.stringify(xmlParsed, null, 2); const jsonPath = xmlPath.replace(/\.xml$/, ".json"); yield promises.writeFile(jsonPath, jsonString); logger.debug(`${xmlPath} has been transformed into ${jsonPath}`); }); } class XmlToJsonDisassembler { constructor() { this.ign = ignore(); } disassemble(xmlAttributes) { return __awaiter(this, void 0, void 0, function* () { const { filePath, uniqueIdElements = "", prePurge = false, postPurge = false, ignorePath = ".xmldisassemblerignore", } = xmlAttributes; const resolvedIgnorePath = node_path.resolve(ignorePath); if (node_fs.existsSync(resolvedIgnorePath)) { const content = yield promises.readFile(resolvedIgnorePath); this.ign.add(content.toString()); } const fileStat = yield promises.stat(filePath); if (fileStat.isFile()) { const resolvedPath = node_path.resolve(filePath); if (!resolvedPath.endsWith(".xml")) { logger.error(`The file path is not an XML file: ${resolvedPath}`); return; } if (this.ign.ignores(filePath)) { logger.warn(`File ignored by ${ignorePath}: ${resolvedPath}`); return; } yield this.processFile({ filePath: resolvedPath, uniqueIdElements, prePurge, postPurge, ignorePath, }); } else if (fileStat.isDirectory()) { const subFiles = yield promises.readdir(filePath); for (const subFile of subFiles) { const subFilePath = node_path.join(filePath, subFile); const relativeSubFilePath = node_path.relative(process.cwd(), subFilePath); if (subFilePath.endsWith(".xml") && !this.ign.ignores(relativeSubFilePath)) { yield this.processFile({ filePath: subFilePath, uniqueIdElements, prePurge, postPurge, ignorePath, }); } else if (this.ign.ignores(relativeSubFilePath)) { logger.warn(`File ignored by ${ignorePath}: ${subFilePath}`); } } } }); } processFile(xmlAttributes) { return __awaiter(this, void 0, void 0, function* () { const { filePath, uniqueIdElements, prePurge, postPurge, ignorePath } = xmlAttributes; yield disassembleHandler(filePath, uniqueIdElements, prePurge, postPurge, ignorePath); const fullName = node_path.basename(filePath, node_path.extname(filePath)); const basePath = node_path.dirname(filePath); const baseName = fullName.split(".")[0]; yield transform2JSON(node_path.join(basePath, baseName)); }); } } function reassembleHandler(filePath, fileExtension, postpurge) { return __awaiter(this, void 0, void 0, function* () { const handler = new xmlDisassembler.ReassembleXMLFileHandler(); yield handler.reassemble({ filePath, fileExtension, postPurge: postpurge, }); }); } function transform2XML(jsonPath, indentLevel = 0) { return __awaiter(this, void 0, void 0, function* () { const jsonString = yield promises.readFile(jsonPath, "utf-8"); const jsonObject = JSON.parse(jsonString); const xmlBuilder = new fastXmlParser.XMLBuilder(JSON_PARSER_OPTION); const xmlString = xmlBuilder.build(jsonObject); const formattedXml = xmlString .split("\n") .map((line) => `${" ".repeat(indentLevel * INDENT.length)}${line}`) .join("\n") .trimEnd(); const xmlPath = jsonPath.replace(/\.json$/, ".xml"); yield promises.writeFile(xmlPath, formattedXml); logger.debug(`${jsonPath} has been transformed into ${xmlPath}`); }); } function deleteReassembledXML(disassembledPath) { return __awaiter(this, void 0, void 0, function* () { const files = yield promises.readdir(disassembledPath); for (const file of files) { const filePath = node_path.join(disassembledPath, file); const fileStat = yield promises.stat(filePath); if (fileStat.isFile() && filePath.endsWith(".xml")) { yield promises.rm(filePath); } else if (fileStat.isDirectory()) { yield deleteReassembledXML(filePath); } } }); } class JsonToXmlReassembler { reassemble(xmlAttributes) { return __awaiter(this, void 0, void 0, function* () { const { filePath, fileExtension = "xml", postPurge = false, } = xmlAttributes; const fileStat = yield promises.stat(filePath); if (fileStat.isFile()) { logger.error(`The path ${filePath} is not a directory.`); return; } else if (fileStat.isDirectory()) { yield this.processFile(filePath); } yield reassembleHandler(filePath, fileExtension, postPurge); if (!postPurge) yield deleteReassembledXML(filePath); }); } processFile(filePath) { return __awaiter(this, void 0, void 0, function* () { const files = yield promises.readdir(filePath); for (const file of files) { const subFilePath = node_path.join(filePath, file); const subFileStat = yield promises.stat(subFilePath); if (subFileStat.isFile() && subFilePath.endsWith(".json")) { yield transform2XML(subFilePath); } else if (subFileStat.isDirectory()) { yield this.processFile(subFilePath); } } }); } } const logger = log4js.getLogger(); function setLogLevel(level) { log4js.getLogger().level = level; } log4js.configure({ appenders: { disassemble: { type: "file", filename: "disassemble.log" } }, categories: { default: { appenders: ["disassemble"], level: "error" } }, }); exports.JsonToXmlReassembler = JsonToXmlReassembler; exports.XmlToJsonDisassembler = XmlToJsonDisassembler; exports.logger = logger; exports.setLogLevel = setLogLevel; //# sourceMappingURL=index.cjs.map