Skip to content

Commit

Permalink
fix: allow file-path to be accepted by disassemble
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Mar 11, 2024
1 parent e3fc7fa commit 31ecd17
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ npm install xml-disassembler

## Disassembling Files

Disassemble 1 or multiple XML files in the immediate directory (`xmlPath`), without recursion. Each XML file will be disassembled into their own sub-directories using their base name (everything before the first `.` in the file-name).
Disassemble 1 XML file or multiple XML files in the immediate directory, without recursion. Each XML file will be disassembled into their own sub-directories using their base name (everything before the first `.` in the file-name).

Example:

Expand Down Expand Up @@ -100,7 +100,7 @@ Import the `DisassembleXMLFileHandler` class from the package.
```typescript
/*
FLAGS
- xmlPath: Directory containing the XML files to disassemble (must be directory). This will only disassemble files in the immediate directory.
- 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.
- 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.
Expand Down
44 changes: 27 additions & 17 deletions src/service/disassembleXMLFileHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,33 @@ export class DisassembleXMLFileHandler {
} = xmlAttributes;
const fileStat = await fs.stat(xmlPath);

if (!fileStat.isDirectory()) {
logger.error(
`The provided xmlPath ${xmlPath} to disassemble is not a directory.`,
);
return;
}
const files = await fs.readdir(xmlPath);
for (const file of files) {
const filePath = path.join(xmlPath, file);
if (filePath.endsWith(".xml")) {
await this.processFile({
xmlPath,
filePath,
uniqueIdElements,
prePurge,
postPurge,
});
if (fileStat.isFile()) {
const filePath = path.resolve(xmlPath);
if (!filePath.endsWith(".xml")) {
logger.error(`The file path ${filePath} is not an XML file.`);
return;
}
const basePath = path.dirname(filePath);
await this.processFile({
xmlPath: basePath,
filePath,
uniqueIdElements,
prePurge,
postPurge,
});
} else if (fileStat.isDirectory()) {
const files = await fs.readdir(xmlPath);
for (const file of files) {
const filePath = path.join(xmlPath, file);
if (filePath.endsWith(".xml")) {
await this.processFile({
xmlPath,
filePath,
uniqueIdElements,
prePurge,
postPurge,
});
}
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe("main function", () => {
});
it("should disassemble a XML file with no namespace.", async () => {
await disassembleHandler.disassemble({
xmlPath: "mock/no-namespace",
xmlPath: "mock/no-namespace/HR_Admin.permissionset-meta.xml",
uniqueIdElements:
"application,apexClass,name,externalDataSource,flow,object,apexPage,recordType,tab,field",
});
Expand All @@ -167,11 +167,15 @@ describe("main function", () => {

expect(logger.error).not.toHaveBeenCalled();
});
it("should test disassemble error condition (file path provided).", async () => {
it("should test disassemble error condition (XML file path not provided).", async () => {
let fakeFile = "mock/not-an-xml.txt";
fakeFile = path.resolve(fakeFile);
const fakeFileContents = "Testing error condition.";
fs.writeFileSync(fakeFile, fakeFileContents);
await disassembleHandler.disassemble({
xmlPath: "mock/no-namespace/HR_Admin.permissionset-meta.xml",
xmlPath: fakeFile,
});

fs.unlinkSync(fakeFile);
expect(logger.error).toHaveBeenCalled();
});
it("should test reassemble error condition (file path provided).", async () => {
Expand Down

0 comments on commit 31ecd17

Please sign in to comment.