Skip to content

Commit

Permalink
feat: allow users to delete disassembled files are reassembly
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Mar 28, 2024
1 parent 3c8693b commit 662e78d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ Import the `DisassembleXMLFileHandler` class from the package.
```typescript
/*
FLAGS
- xmlPath: Path to 1 XML file or a directory of XML files to disassemble. If the path provided is a directory, only the files in the immediate directory will be disasssembled.
- xmlPath: Path to 1 XML file or a directory of XML files to disassemble. If the path provided is a directory, only the files in the immediate directory will be disassembled.
- 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 disassembled directories prior to disassembling the file.
Defaults to false.
- postPurge: (Optional) Boolean value. If set to true, purge the original XML file after disassembling it.
Defaults to false.
Defaults to false.
*/
import { DisassembleXMLFileHandler } from "xml-disassembler";

Expand All @@ -133,13 +133,16 @@ Import the `ReassembleXMLFileHandler` class from the package.
FLAGS
- xmlPath: Path to the disassembled XML files to reassemble (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 file directory (xmlPath) after reassembly.
Defaults to false.
*/
import { ReassembleXMLFileHandler } from "xml-disassembler";

const handler = new ReassembleXMLFileHandler();
await handler.reassemble({
xmlPath: "test/baselines/general/HR_Admin",
fileExtension: "permissionset-meta.xml",
postPurge: true,
});
```

Expand Down
4 changes: 3 additions & 1 deletion src/service/reassembleXMLFileHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ export class ReassembleXMLFileHandler {
async reassemble(xmlAttributes: {
xmlPath: string;
fileExtension?: string;
postPurge?: boolean;
}): Promise<void> {
const { xmlPath, fileExtension } = xmlAttributes;
const { xmlPath, fileExtension, postPurge = false } = xmlAttributes;
const combinedXmlContents: string[] = [];
const fileStat = await fs.stat(xmlPath);

Expand Down Expand Up @@ -131,6 +132,7 @@ export class ReassembleXMLFileHandler {
rootElementName,
rootElementNamespace,
);
if (postPurge) await fs.rm(xmlPath, { recursive: true });
} else {
logger.error(
`A Root Element Name was not found in any files under ${xmlPath}`,
Expand Down
5 changes: 3 additions & 2 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("main function", () => {
});

afterAll(async () => {
await fsExtra.remove(mockDir);
// await fsExtra.remove(mockDir);
});

it('should disassemble a general XML file (nested and leaf elements) with unique ID elements."', async () => {
Expand Down Expand Up @@ -138,10 +138,11 @@ describe("main function", () => {

expect(logger.error).not.toHaveBeenCalled();
});
it("should reassemble the files from the previous test (prePurge).", async () => {
it("should reassemble the files from the previous test (prePurge) and delete the disassemble files afterwards.", async () => {
await reassembleHandler.reassemble({
xmlPath: "mock/array-of-leafs/Dreamhouse",
fileExtension: "app-meta.xml",
postPurge: true,
});

expect(logger.error).not.toHaveBeenCalled();
Expand Down

0 comments on commit 662e78d

Please sign in to comment.