diff --git a/README.md b/README.md index 678fd82..feb9345 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ npm install xml2json-disassembler ```typescript /* FLAGS -- xmlPath: Path to 1 XML file or a directory of XML files to disassemble, then transform into JSON files. If the path provided is a directory, only the files in the immediate directory will be disassembled and transformed. +- filePath: Path to 1 XML file or a directory of XML files to disassemble, then transform into JSON files. If the path provided is a directory, only the files in the immediate directory will be disassembled and transformed. - uniqueIdElements: (Optional) Comma-separated list of unique and required ID elements used to name disassembled files for nested elements. Defaults to SHA-256 hash if unique ID elements are undefined or not found. - prePurge: (Optional) Boolean value. If set to true, purge pre-existing transformed directories prior to disassembling and transformed the file. @@ -41,7 +41,7 @@ import { XmlToJsonDisassembler } from "xml2json-disassembler"; const handler = new XmlToJsonDisassembler(); await handler.disassemble({ - xmlPath: "test/baselines/general", + filePath: "test/baselines/general", uniqueIdElements: "application,apexClass,name,externalDataSource,flow,object,apexPage,recordType,tab,field", prePurge: true, @@ -49,7 +49,7 @@ await handler.disassemble({ }); ``` -Disassemble then transform 1 or multiple XML files into JSON files. If the `xmlPath` is a directory, only the XMLs in the immediate directory will be processed. Each XML wiill be transformed into JSON files in new sub-directories using the XML's base name (everything before the first period in the file-name). +Disassemble then transform 1 or multiple XML files into JSON files. If the `filePath` is a directory, only the XMLs in the immediate directory will be processed. Each XML wiill be transformed into JSON files in new sub-directories using the XML's base name (everything before the first period in the file-name). Example: @@ -120,7 +120,7 @@ will be disassembled into a sub-directory named `HR_Admin` as such: ```typescript /* FLAGS -- jsonPath: Path to the directory containing the JSON files to reassemble into 1 XML file (must be a directory). +- filePath: Path to the directory containing the JSON files to reassemble into 1 XML file (must be a directory). - fileExtension: (Optional) Desired file extension for the final XML (default: `.xml`). - postPurge: (Optional) Boolean value. If set to true, purge the disassembled directory containing JSON files after the XML is reassembled. Defaults to false. @@ -129,13 +129,13 @@ import { JsonToXmlReassembler } from "xml2json-disassembler"; const handler = new JsonToXmlReassembler(); await handler.reassemble({ - jsonPath: "test/baselines/HR_Admin", + filePath: "test/baselines/HR_Admin", fileExtension: "permissionset-meta.xml", postPurge: true, }); ``` -Reassemble all of the JSON files in a directory into 1 XML file. **Note:** You should only be reassembling JSON files created by the `XmlToJsonDisassembler` class for intended results. The reassembled XML file will be created in the parent directory of `jsonPath` and will overwrite the original file used to create the original disassembled directories, if it still exists and the `fileExtension` flag matches the original file extension. +Reassemble all of the JSON files in a directory into 1 XML file. **Note:** You should only be reassembling JSON files created by the `XmlToJsonDisassembler` class for intended results. The reassembled XML file will be created in the parent directory of `filePath` and will overwrite the original file used to create the original disassembled directories, if it still exists and the `fileExtension` flag matches the original file extension. ## Logging @@ -175,7 +175,7 @@ setLogLevel("debug"); const disassembleHandler = new XmlToJsonDisassembler(); await disassembleHandler.disassemble({ - xmlPath: "test/baselines/general", + filePath: "test/baselines/general", uniqueIdElements: "application,apexClass,name,externalDataSource,flow,object,apexPage,recordType,tab,field", prePurge: true, @@ -184,7 +184,7 @@ await disassembleHandler.disassemble({ const reassembleHandler = new JsonToXmlReassembler(); await reassembleHandler.reassemble({ - jsonPath: "test/baselines/HR_Admin", + filePath: "test/baselines/HR_Admin", fileExtension: "permissionset-meta.xml", postPurge: true, }); diff --git a/package.json b/package.json index b396ce5..9d27878 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "fast-xml-parser": "^4.3.4", "log4js": "^6.9.1", "tslib": "^2.6.2", - "xml-disassembler": "^1.2.9" + "xml-disassembler": "^1.2.10" }, "repository": { "type": "git", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 83c931e..19b0b89 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -15,8 +15,8 @@ dependencies: specifier: ^2.6.2 version: 2.6.2 xml-disassembler: - specifier: ^1.2.9 - version: 1.2.9 + specifier: ^1.2.10 + version: 1.2.10 devDependencies: "@rollup/plugin-terser": @@ -7500,10 +7500,10 @@ packages: signal-exit: 3.0.7 dev: true - /xml-disassembler@1.2.9: + /xml-disassembler@1.2.10: resolution: { - integrity: sha512-6D6LFs1w3stBaxhJhE3msqqTV57+gDVJj0UT2WEiy31pLXs2v+IIhXCjspEjV6Ug/1Rn/Ki1xt9mc0TO54Sf/w==, + integrity: sha512-mcHh0CQRxjMC9NIZVZ/YCyREC2vX3HQWa5SS4/QlfWtl8rHZSAXWb38a4cvOJG44q55VvJkuhW8EOKh123xNgw==, } engines: { node: ">=18", pnpm: ">=8" } dependencies: diff --git a/src/service/disassembleHandler.ts b/src/service/disassembleHandler.ts index a2c6141..ff6fdb2 100644 --- a/src/service/disassembleHandler.ts +++ b/src/service/disassembleHandler.ts @@ -3,14 +3,14 @@ import { DisassembleXMLFileHandler } from "xml-disassembler"; export async function disassembleHandler( - xmlPath: string, + filePath: string, uniqueIdElements: string, prePurge: boolean, postPurge: boolean, ): Promise { const handler = new DisassembleXMLFileHandler(); await handler.disassemble({ - xmlPath, + filePath, uniqueIdElements, prePurge, postPurge, diff --git a/src/service/json2xmlReassembler.ts b/src/service/json2xmlReassembler.ts index cdd4295..3a2897e 100644 --- a/src/service/json2xmlReassembler.ts +++ b/src/service/json2xmlReassembler.ts @@ -10,38 +10,38 @@ import { deleteReassembledXML } from "@src/service/deleteReassembledXML"; export class JsonToXmlReassembler { async reassemble(xmlAttributes: { - jsonPath: string; + filePath: string; fileExtension?: string; postPurge?: boolean; }): Promise { const { - jsonPath, + filePath, fileExtension = "xml", postPurge = false, } = xmlAttributes; - const fileStat = await stat(jsonPath); + const fileStat = await stat(filePath); if (fileStat.isFile()) { - logger.error(`The path ${jsonPath} is not a directory.`); + logger.error(`The path ${filePath} is not a directory.`); return; } else if (fileStat.isDirectory()) { - await this.processFile(jsonPath); + await this.processFile(filePath); } - await reassembleHandler(jsonPath, fileExtension, postPurge); + await reassembleHandler(filePath, fileExtension, postPurge); // delete XML files created during reassembly - this is needed if postPurge is false - if (!postPurge) await deleteReassembledXML(jsonPath); + if (!postPurge) await deleteReassembledXML(filePath); } - async processFile(jsonPath: string): Promise { - const files = await readdir(jsonPath); + async processFile(filePath: string): Promise { + const files = await readdir(filePath); for (const file of files) { - const filePath = join(jsonPath, file); - const fileStat = await stat(filePath); - if (fileStat.isFile() && filePath.endsWith(".json")) { - await transform2XML(filePath); - } else if (fileStat.isDirectory()) { - await this.processFile(filePath); + const subFilePath = join(filePath, file); + const subFileStat = await stat(subFilePath); + if (subFileStat.isFile() && subFilePath.endsWith(".json")) { + await transform2XML(subFilePath); + } else if (subFileStat.isDirectory()) { + await this.processFile(subFilePath); } } } diff --git a/src/service/reassembleHandler.ts b/src/service/reassembleHandler.ts index 75f2ae6..7b0ab36 100644 --- a/src/service/reassembleHandler.ts +++ b/src/service/reassembleHandler.ts @@ -3,13 +3,13 @@ import { ReassembleXMLFileHandler } from "xml-disassembler"; export async function reassembleHandler( - xmlPath: string, + filePath: string, fileExtension: string, postpurge: boolean, ): Promise { const handler = new ReassembleXMLFileHandler(); await handler.reassemble({ - xmlPath, + filePath, fileExtension, postPurge: postpurge, }); diff --git a/src/service/xml2jsonDisassembler.ts b/src/service/xml2jsonDisassembler.ts index f07db21..9721b73 100644 --- a/src/service/xml2jsonDisassembler.ts +++ b/src/service/xml2jsonDisassembler.ts @@ -9,38 +9,38 @@ import { transform2JSON } from "@src/service/transform2JSON"; export class XmlToJsonDisassembler { async disassemble(xmlAttributes: { - xmlPath: string; + filePath: string; uniqueIdElements?: string; prePurge?: boolean; postPurge?: boolean; }): Promise { const { - xmlPath, + filePath, uniqueIdElements = "", prePurge = false, postPurge = false, } = xmlAttributes; - const fileStat = await stat(xmlPath); + const fileStat = await stat(filePath); if (fileStat.isFile()) { - const filePath = resolve(xmlPath); - if (!filePath.endsWith(".xml")) { - logger.error(`The file path ${filePath} is not an XML file.`); + const resolvedPath = resolve(filePath); + if (!resolvedPath.endsWith(".xml")) { + logger.error(`The file path is not an XML file: ${resolvedPath}`); return; } await this.processFile({ - filePath, + filePath: resolvedPath, uniqueIdElements, prePurge, postPurge, }); } else if (fileStat.isDirectory()) { - const files = await readdir(xmlPath); - for (const file of files) { - const filePath = join(xmlPath, file); - if (filePath.endsWith(".xml")) { + const subFiles = await readdir(filePath); + for (const subFile of subFiles) { + const subFilePath = join(filePath, subFile); + if (subFilePath.endsWith(".xml")) { await this.processFile({ - filePath, + filePath: subFilePath, uniqueIdElements, prePurge, postPurge, diff --git a/test/main.spec.ts b/test/main.spec.ts index 1a729d7..1c47e27 100644 --- a/test/main.spec.ts +++ b/test/main.spec.ts @@ -37,7 +37,7 @@ describe("main function", () => { it("should disassemble & transform 1 XML file into JSON files.", async () => { await xml2jsonDisassemblerHandler.disassemble({ - xmlPath: "mock/HR_Admin.permissionset-meta.xml", + filePath: "mock/HR_Admin.permissionset-meta.xml", uniqueIdElements: "application,apexClass,name,externalDataSource,flow,object,apexPage,recordType,tab,field", }); @@ -46,7 +46,7 @@ describe("main function", () => { }); it("should disassemble & transform a directory of XML files into JSON files.", async () => { await xml2jsonDisassemblerHandler.disassemble({ - xmlPath: "mock", + filePath: "mock", uniqueIdElements: "application,apexClass,name,externalDataSource,flow,object,apexPage,recordType,tab,field", prePurge: true, @@ -57,7 +57,7 @@ describe("main function", () => { }); it("should reassemble the XML file.", async () => { await json2xmlReassemblerHandler.reassemble({ - jsonPath: "mock/HR_Admin", + filePath: "mock/HR_Admin", fileExtension: "permissionset-meta.xml", }); @@ -65,7 +65,7 @@ describe("main function", () => { }); it("should reassemble the XML file with comments.", async () => { await json2xmlReassemblerHandler.reassemble({ - jsonPath: "mock/Numbers-fr", + filePath: "mock/Numbers-fr", fileExtension: "globalValueSetTranslation-meta.xml", }); @@ -73,7 +73,7 @@ describe("main function", () => { }); it("should reassemble the CDATA XML file.", async () => { await json2xmlReassemblerHandler.reassemble({ - jsonPath: "mock/VidLand_US", + filePath: "mock/VidLand_US", fileExtension: "marketingappextension-meta.xml", }); @@ -81,7 +81,7 @@ describe("main function", () => { }); it("should reassemble the XML file with an array of leafs.", async () => { await json2xmlReassemblerHandler.reassemble({ - jsonPath: "mock/Dreamhouse", + filePath: "mock/Dreamhouse", fileExtension: "app-meta.xml", }); @@ -89,7 +89,7 @@ describe("main function", () => { }); it("should reassemble the XML file with attributes.", async () => { await json2xmlReassemblerHandler.reassemble({ - jsonPath: "mock/attributes", + filePath: "mock/attributes", }); expect(logger.error).not.toHaveBeenCalled(); @@ -100,14 +100,14 @@ describe("main function", () => { const fakeFileContents = "Testing error condition."; await writeFile(fakeFile, fakeFileContents); await xml2jsonDisassemblerHandler.disassemble({ - xmlPath: fakeFile, + filePath: fakeFile, }); expect(logger.error).toHaveBeenCalled(); }); it("should test reassemble error condition (file path provided).", async () => { const fakeFile = "mock/not-an-xml.txt"; await json2xmlReassemblerHandler.reassemble({ - jsonPath: fakeFile, + filePath: fakeFile, }); await rm(fakeFile); expect(logger.error).toHaveBeenCalled();