Skip to content

Commit

Permalink
fix: revert to 1.3.8 to remove parallel processing
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Jan 21, 2025
1 parent 80f4636 commit 876ff0c
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 222 deletions.
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export { DisassembleXMLFileHandler } from "./service/disassembleXMLFileHandler";
export { parseXML } from "./service/parseXML";
export { buildXMLString } from "./service/buildXMLString";
export { XmlElement } from "./helpers/types";
export { getConcurrencyThreshold } from "./service/getConcurrencyThreshold";
export { withConcurrencyLimit } from "./service/withConcurrencyLimit";

// Function to update the log level
export function setLogLevel(level: string) {
Expand Down
83 changes: 33 additions & 50 deletions src/service/buildDisassembledFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { processElement } from "@src/service/processElement";
import { buildRootElementHeader } from "@src/service/buildRootElementHeader";
import { buildLeafFile } from "@src/service/buildLeafFile";
import { parseXML } from "@src/service/parseXML";
import { getConcurrencyThreshold } from "./getConcurrencyThreshold";
import { withConcurrencyLimit } from "./withConcurrencyLimit";

export async function buildDisassembledFiles(
filePath: string,
Expand All @@ -21,74 +19,59 @@ export async function buildDisassembledFiles(
): Promise<void> {
const parsedXml = await parseXML(filePath);
if (parsedXml === undefined) return;

const rootElementName = Object.keys(parsedXml)[1];

const rootElement: XmlElement = parsedXml[rootElementName];
const rootElementHeader = buildRootElementHeader(
rootElement,
rootElementName,
);

let leafContent = "";
let leafCount = 0;
let hasNestedElements: boolean = false;

const childKeys = Object.keys(rootElement).filter(
// Iterate through child elements to find the field name for each
for (const key of Object.keys(rootElement).filter(
(key: string) => !key.startsWith("@"),
);

const concurrencyLimit = getConcurrencyThreshold();

// Create tasks for processing child keys
const tasks: (() => Promise<void>)[] = childKeys.map((key) => {
return async () => {
if (Array.isArray(rootElement[key])) {
await Promise.all(
(rootElement[key] as XmlElement[]).map(async (element) => {
const [
updatedLeafContent,
updatedLeafCount,
updatedHasNestedElements,
] = await processElement({
element,
disassembledPath,
uniqueIdElements,
rootElementName,
rootElementHeader,
key,
indent,
leafContent: "",
leafCount: 0,
hasNestedElements: false,
});
leafContent += updatedLeafContent;
leafCount += updatedLeafCount;
hasNestedElements = hasNestedElements || updatedHasNestedElements;
}),
);
} else {
)) {
if (Array.isArray(rootElement[key])) {
for (const element of rootElement[key] as XmlElement[]) {
const [updatedLeafContent, updatedLeafCount, updatedHasNestedElements] =
await processElement({
element: rootElement[key] as XmlElement,
element,
disassembledPath,
uniqueIdElements,
rootElementName,
rootElementHeader,
key,
indent,
leafContent: "",
leafCount: 0,
hasNestedElements: false,
leafContent,
leafCount,
hasNestedElements,
});
leafContent += updatedLeafContent;
leafCount += updatedLeafCount;
hasNestedElements = hasNestedElements || updatedHasNestedElements;
leafContent = updatedLeafContent;
leafCount = updatedLeafCount;
hasNestedElements = updatedHasNestedElements;
}
};
});

// Execute tasks with concurrency limit
await withConcurrencyLimit(tasks, concurrencyLimit);
} else {
const [updatedLeafContent, updatedLeafCount, updatedHasNestedElements] =
await processElement({
element: rootElement[key] as XmlElement,
disassembledPath,
uniqueIdElements,
rootElementName,
rootElementHeader,
key,
indent,
leafContent,
leafCount,
hasNestedElements,
});
leafContent = updatedLeafContent;
leafCount = updatedLeafCount;
hasNestedElements = updatedHasNestedElements;
}
}

if (!hasNestedElements) {
logger.error(
Expand All @@ -107,6 +90,6 @@ export async function buildDisassembledFiles(
);
}
if (postPurge) {
await unlink(filePath);
unlink(filePath);
}
}
52 changes: 20 additions & 32 deletions src/service/disassembleXMLFileHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import ignore, { Ignore } from "ignore";
import { logger } from "@src/index";
import { INDENT } from "@src/helpers/constants";
import { buildDisassembledFiles } from "@src/service/buildDisassembledFiles";
import { getConcurrencyThreshold } from "./getConcurrencyThreshold";
import { withConcurrencyLimit } from "./withConcurrencyLimit";

export class DisassembleXMLFileHandler {
private readonly ign: Ignore = ignore();
private ign: Ignore = ignore();

async disassemble(xmlAttributes: {
filePath: string;
Expand All @@ -30,7 +28,6 @@ export class DisassembleXMLFileHandler {
ignorePath = ".xmldisassemblerignore",
} = xmlAttributes;
const resolvedIgnorePath = resolve(ignorePath);

if (existsSync(resolvedIgnorePath)) {
const content = await readFile(resolvedIgnorePath);
this.ign.add(content.toString());
Expand Down Expand Up @@ -61,35 +58,26 @@ export class DisassembleXMLFileHandler {
});
} else if (fileStat.isDirectory()) {
const subFiles = await readdir(filePath);
const concurrencyLimit = getConcurrencyThreshold();

// Create tasks for all subfiles
const tasks: (() => Promise<void>)[] = subFiles.map((subFile) => {
for (const subFile of subFiles) {
const subFilePath = join(filePath, subFile);
const relativeSubFilePath = this.posixPath(
relative(process.cwd(), subFilePath),
);

return async () => {
if (
subFilePath.endsWith(".xml") &&
!this.ign.ignores(relativeSubFilePath)
) {
await this.processFile({
dirPath: filePath,
filePath: subFilePath,
uniqueIdElements,
prePurge,
postPurge,
});
} else if (this.ign.ignores(relativeSubFilePath)) {
logger.warn(`File ignored by ${ignorePath}: ${subFilePath}`);
}
};
});

// Run tasks with concurrency limit
await withConcurrencyLimit(tasks, concurrencyLimit);
if (
subFilePath.endsWith(".xml") &&
!this.ign.ignores(relativeSubFilePath)
) {
await this.processFile({
dirPath: filePath,
filePath: subFilePath,
uniqueIdElements,
prePurge,
postPurge,
});
} else if (this.ign.ignores(relativeSubFilePath)) {
logger.warn(`File ignored by ${ignorePath}: ${subFilePath}`);
}
}
}
}

Expand All @@ -107,11 +95,11 @@ export class DisassembleXMLFileHandler {
const fullName = basename(filePath, extname(filePath));
const baseName = fullName.split(".")[0];

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

if (prePurge && existsSync(outputPath)) {
if (prePurge && existsSync(outputPath))
await rm(outputPath, { recursive: true });
}

await buildDisassembledFiles(
filePath,
Expand Down
10 changes: 0 additions & 10 deletions src/service/getConcurrencyThreshold.ts

This file was deleted.

57 changes: 20 additions & 37 deletions src/service/reassembleXMLFileHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { buildReassembledFile } from "@src/service/buildReassembledFiles";
import { buildXMLString } from "@src/service/buildXMLString";
import { parseXML } from "@src/service/parseXML";
import { processFilesForRootElement } from "@src/service/processFilesForRootElement";
import { getConcurrencyThreshold } from "./getConcurrencyThreshold";
import { withConcurrencyLimit } from "./withConcurrencyLimit";

export class ReassembleXMLFileHandler {
async processFilesInDirectory(
dirPath: string,
): Promise<[string[], [string, string | undefined] | undefined]> {
const combinedXmlContents: string[] = [];
let rootResult: [string, string | undefined] | undefined = undefined;
const files = await readdir(dirPath);

// Sort files based on the name
Expand All @@ -24,40 +24,24 @@ export class ReassembleXMLFileHandler {
return fullNameA.localeCompare(fullNameB);
});

const combinedXmlContents: string[] = [];
let rootResult: [string, string | undefined] | undefined = undefined;

const concurrencyLimit = getConcurrencyThreshold();

// Create tasks for processing files
const tasks: (() => Promise<void>)[] = files.map((file, index) => {
return async () => {
const filePath = join(dirPath, file);
const fileStat = await stat(filePath);

if (fileStat.isFile() && filePath.endsWith(".xml")) {
const xmlParsed = await parseXML(filePath);
if (xmlParsed === undefined) return;

const rootResultFromFile =
await processFilesForRootElement(xmlParsed);
rootResult = rootResultFromFile;

const combinedXmlString = buildXMLString(xmlParsed);
combinedXmlContents[index] = combinedXmlString;
} else if (fileStat.isDirectory()) {
const [subCombinedXmlContents, subRootResult] =
await this.processFilesInDirectory(filePath);
rootResult = subRootResult;
combinedXmlContents[index] = subCombinedXmlContents.join("");
}
};
});

// Execute tasks with concurrency limit
await withConcurrencyLimit(tasks, concurrencyLimit);

return [combinedXmlContents.filter(Boolean), rootResult];
for (const file of files) {
const filePath = join(dirPath, file);
const fileStat = await stat(filePath);
if (fileStat.isFile() && filePath.endsWith(".xml")) {
const xmlParsed = await parseXML(filePath);
if (xmlParsed === undefined) continue;
const rootResultFromFile = await processFilesForRootElement(xmlParsed);
rootResult = rootResultFromFile;
const combinedXmlString = buildXMLString(xmlParsed);
combinedXmlContents.push(combinedXmlString);
} else if (fileStat.isDirectory()) {
const [subCombinedXmlContents, subRootResult] =
await this.processFilesInDirectory(filePath);
combinedXmlContents.push(...subCombinedXmlContents);
rootResult = subRootResult;
}
}
return [combinedXmlContents, rootResult];
}

async reassemble(xmlAttributes: {
Expand All @@ -75,7 +59,6 @@ export class ReassembleXMLFileHandler {
);
return;
}

logger.debug(`Parsing directory to reassemble: ${filePath}`);
const [subCombinedXmlContents, rootResult] =
await this.processFilesInDirectory(filePath);
Expand Down
26 changes: 0 additions & 26 deletions src/service/withConcurrencyLimit.ts

This file was deleted.

Loading

0 comments on commit 876ff0c

Please sign in to comment.