Skip to content

Commit

Permalink
fix: remove extra space in leaf file, normalize variable names, chang…
Browse files Browse the repository at this point in the history
…e `xmlPath` to `filePath` in classes
  • Loading branch information
mcarvin8 committed Apr 24, 2024
1 parent e165c23 commit 61a204c
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 131 deletions.
48 changes: 0 additions & 48 deletions CONTRIBUTING.md

This file was deleted.

22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ 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 disassembled.
- filePath: 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.
Expand All @@ -112,7 +112,7 @@ import { DisassembleXMLFileHandler } from "xml-disassembler";

const handler = new DisassembleXMLFileHandler();
await handler.disassemble({
xmlPath: "test/baselines/general",
filePath: "test/baselines/general",
uniqueIdElements:
"application,apexClass,name,externalDataSource,flow,object,apexPage,recordType,tab,field",
prePurge: true,
Expand All @@ -122,25 +122,25 @@ await handler.disassemble({

## Reassembling Files

Reassemble 1 XML directory (`xmlPath`) containing disassembled files back into 1 XML file.
Reassemble 1 XML directory (`filePath`) containing disassembled files back into 1 XML file.

**NOTE**: You should be reassembling files created by this package's `DisassembleXMLFileHandler` class for intended results. This class will assume all disassembled files in `xmlPath` have the same XML Root Element. The reassembled XML file will be created in the parent directory of `xmlPath` 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.
**NOTE**: You should be reassembling files created by this package's `DisassembleXMLFileHandler` class for intended results. This class will assume all disassembled files in `filePath` have the same XML Root Element. 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.

Import the `ReassembleXMLFileHandler` class from the package.

```typescript
/*
FLAGS
- xmlPath: Path to the disassembled XML files to reassemble (must be a directory)
- filePath: 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.
- postPurge: (Optional) Boolean value. If set to true, purge the disassembled file directory (filePath) after reassembly.
Defaults to false.
*/
import { ReassembleXMLFileHandler } from "xml-disassembler";

const handler = new ReassembleXMLFileHandler();
await handler.reassemble({
xmlPath: "test/baselines/general/HR_Admin",
filePath: "test/baselines/general/HR_Admin",
fileExtension: "permissionset-meta.xml",
postPurge: true,
});
Expand Down Expand Up @@ -204,7 +204,7 @@ if (debug) {

const disassembleHandler = new DisassembleXMLFileHandler();
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 @@ -213,7 +213,7 @@ await disassembleHandler.disassemble({

const reassembleHandler = new ReassembleXMLFileHandler();
await reassembleHandler.reassemble({
xmlPath: "test/baselines/general/HR_Admin",
filePath: "test/baselines/general/HR_Admin",
fileExtension: "permissionset-meta.xml",
});
```
Expand All @@ -223,7 +223,3 @@ await reassembleHandler.reassemble({
This project was created from a template provided by [Allan Oricil](https://github.com/AllanOricil). Thank you Allan!

His original [license](https://github.com/AllanOricil/js-template/blob/main/LICENSE) remains in this project.

## Contributing

Any contributions you would like to make are appreciated. Please see [CONTRIBUTING](https://github.com/mcarvin8/xml-disassembler/blob/main/CONTRIBUTING.md).
2 changes: 1 addition & 1 deletion src/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface XmlElement {

export interface ProcessElementParams {
element: XmlElement;
metadataPath: string;
disassembledPath: string;
uniqueIdElements: string | undefined;
rootElementName: string;
rootElementHeader: string;
Expand Down
16 changes: 8 additions & 8 deletions src/service/buildDisassembledFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import { buildLeafFile } from "@src/service/buildLeafFile";
import { parseXML } from "@src/service/parseXML";

export async function buildDisassembledFiles(
xmlPath: string,
metadataPath: string,
filePath: string,
disassembledPath: string,
uniqueIdElements: string | undefined,
baseName: string,
indent: string,
postPurge: boolean,
): Promise<void> {
const parsedXml = await parseXML(xmlPath);
const parsedXml = await parseXML(filePath);
if (parsedXml === undefined) return;
const rootElementName = Object.keys(parsedXml)[1];

Expand All @@ -39,7 +39,7 @@ export async function buildDisassembledFiles(
const [updatedLeafContent, updatedLeafCount, updatedHasNestedElements] =
await processElement({
element,
metadataPath,
disassembledPath,
uniqueIdElements,
rootElementName,
rootElementHeader,
Expand All @@ -57,7 +57,7 @@ export async function buildDisassembledFiles(
const [updatedLeafContent, updatedLeafCount, updatedHasNestedElements] =
await processElement({
element: rootElement[key] as XmlElement,
metadataPath,
disassembledPath,
uniqueIdElements,
rootElementName,
rootElementHeader,
Expand All @@ -75,21 +75,21 @@ export async function buildDisassembledFiles(

if (!hasNestedElements) {
logger.error(
`The XML file ${xmlPath} only has leaf elements. This file will not be disassembled.`,
`The XML file ${filePath} only has leaf elements. This file will not be disassembled.`,
);
return;
}

if (leafCount > 0) {
await buildLeafFile(
leafContent,
metadataPath,
disassembledPath,
baseName,
rootElementName,
rootElementHeader,
);
}
if (postPurge) {
unlink(xmlPath);
unlink(filePath);
}
}
6 changes: 3 additions & 3 deletions src/service/buildLeafFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { XML_HEADER } from "@src/helpers/constants";

export async function buildLeafFile(
leafContent: string,
metadataPath: string,
disassembledPath: string,
baseName: string,
rootElementName: string,
rootElementHeader: string,
Expand All @@ -17,8 +17,8 @@ export async function buildLeafFile(
leafFile += `${rootElementHeader}\n`;

leafFile += leafContent;
leafFile += `\n</${rootElementName}>`;
const leafOutputPath = join(metadataPath, `${baseName}.xml`);
leafFile += `</${rootElementName}>`;
const leafOutputPath = join(disassembledPath, `${baseName}.xml`);
await writeFile(leafOutputPath, leafFile);

logger.debug(`Created disassembled file: ${leafOutputPath}`);
Expand Down
4 changes: 2 additions & 2 deletions src/service/buildNestedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { buildRootElementHeader } from "@src/service/buildRootElementHeader";

export async function buildNestedFile(
element: XmlElement,
metadataPath: string,
disassembledPath: string,
uniqueIdElements: string | undefined,
rootElementName: string,
rootElementHeader: string,
Expand All @@ -23,7 +23,7 @@ export async function buildNestedFile(

const fieldName = findUniqueIdElement(element, uniqueIdElements);

const outputDirectory = join(metadataPath, parentKey);
const outputDirectory = join(disassembledPath, parentKey);
const outputFileName: string = `${fieldName}.${parentKey}-meta.xml`;
const outputPath = join(outputDirectory, outputFileName);

Expand Down
6 changes: 3 additions & 3 deletions src/service/buildReassembledFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { XML_HEADER, INDENT } from "@src/helpers/constants";

export async function buildReassembledFile(
combinedXmlContents: string[],
filePath: string,
reassembledPath: string,
xmlElement: string,
xmlRootElementHeader: string | undefined,
): Promise<void> {
Expand Down Expand Up @@ -51,8 +51,8 @@ export async function buildReassembledFile(
const closeTag = `</${xmlElement}>`;

await writeFile(
filePath,
reassembledPath,
`${XML_HEADER}\n${xmlRootElementHeader}${finalXmlContent}${closeTag}`,
);
logger.debug(`Created reassembled file: ${filePath}`);
logger.debug(`Created reassembled file: ${reassembledPath}`);
}
38 changes: 20 additions & 18 deletions src/service/disassembleXMLFileHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,43 @@ import { buildDisassembledFiles } from "@src/service/buildDisassembledFiles";

export class DisassembleXMLFileHandler {
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 provided is not an XML file: ${resolvedPath}`,
);
return;
}
const basePath = dirname(filePath);
const dirPath = dirname(resolvedPath);
await this.processFile({
xmlPath: basePath,
filePath,
dirPath,
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({
xmlPath,
filePath,
dirPath: filePath,
filePath: subFilePath,
uniqueIdElements,
prePurge,
postPurge,
Expand All @@ -55,21 +57,21 @@ export class DisassembleXMLFileHandler {
}

async processFile(xmlAttributes: {
xmlPath: string;
dirPath: string;
filePath: string;
uniqueIdElements?: string;
prePurge: boolean;
postPurge: boolean;
}): Promise<void> {
const { xmlPath, filePath, uniqueIdElements, prePurge, postPurge } =
const { dirPath, filePath, uniqueIdElements, prePurge, postPurge } =
xmlAttributes;

logger.debug(`Parsing file to disassemble: ${filePath}`);
const fullName = basename(filePath, extname(filePath));
const baseName = fullName.split(".")[0];

let outputPath;
outputPath = join(xmlPath, baseName);
outputPath = join(dirPath, baseName);

if (prePurge && existsSync(outputPath))
await rm(outputPath, { recursive: true });
Expand Down
4 changes: 2 additions & 2 deletions src/service/processElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function processElement(
): Promise<[string, number, boolean]> {
const {
element,
metadataPath,
disassembledPath,
uniqueIdElements,
rootElementName,
rootElementHeader,
Expand All @@ -22,7 +22,7 @@ export async function processElement(
if (typeof element === "object") {
await buildNestedFile(
element,
metadataPath,
disassembledPath,
uniqueIdElements,
rootElementName,
rootElementHeader,
Expand Down
Loading

0 comments on commit 61a204c

Please sign in to comment.