Skip to content

Commit

Permalink
fix: upgrade xml-disassembler, normalize jsonPath/xmlPath variabl…
Browse files Browse the repository at this point in the history
…es to `filePath`
  • Loading branch information
mcarvin8 committed Apr 24, 2024
1 parent 0aaac47 commit 18a37b9
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 53 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -41,15 +41,15 @@ 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,
postPurge: true,
});
```

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:

Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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,
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/service/disassembleHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import { DisassembleXMLFileHandler } from "xml-disassembler";

export async function disassembleHandler(
xmlPath: string,
filePath: string,
uniqueIdElements: string,
prePurge: boolean,
postPurge: boolean,
): Promise<void> {
const handler = new DisassembleXMLFileHandler();
await handler.disassemble({
xmlPath,
filePath,
uniqueIdElements,
prePurge,
postPurge,
Expand Down
30 changes: 15 additions & 15 deletions src/service/json2xmlReassembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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<void> {
const files = await readdir(jsonPath);
async processFile(filePath: string): Promise<void> {
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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/service/reassembleHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import { ReassembleXMLFileHandler } from "xml-disassembler";

export async function reassembleHandler(
xmlPath: string,
filePath: string,
fileExtension: string,
postpurge: boolean,
): Promise<void> {
const handler = new ReassembleXMLFileHandler();
await handler.reassemble({
xmlPath,
filePath,
fileExtension,
postPurge: postpurge,
});
Expand Down
24 changes: 12 additions & 12 deletions src/service/xml2jsonDisassembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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,
Expand Down
18 changes: 9 additions & 9 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
Expand All @@ -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,
Expand All @@ -57,39 +57,39 @@ 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",
});

expect(logger.error).not.toHaveBeenCalled();
});
it("should reassemble the XML file with comments.", async () => {
await json2xmlReassemblerHandler.reassemble({
jsonPath: "mock/Numbers-fr",
filePath: "mock/Numbers-fr",
fileExtension: "globalValueSetTranslation-meta.xml",
});

expect(logger.error).not.toHaveBeenCalled();
});
it("should reassemble the CDATA XML file.", async () => {
await json2xmlReassemblerHandler.reassemble({
jsonPath: "mock/VidLand_US",
filePath: "mock/VidLand_US",
fileExtension: "marketingappextension-meta.xml",
});

expect(logger.error).not.toHaveBeenCalled();
});
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",
});

expect(logger.error).not.toHaveBeenCalled();
});
it("should reassemble the XML file with attributes.", async () => {
await json2xmlReassemblerHandler.reassemble({
jsonPath: "mock/attributes",
filePath: "mock/attributes",
});

expect(logger.error).not.toHaveBeenCalled();
Expand All @@ -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();
Expand Down

0 comments on commit 18a37b9

Please sign in to comment.