Skip to content

Commit

Permalink
fix: remove ignore dependency from this plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Jan 2, 2025
1 parent 5277039 commit 875be65
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 41 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
},
"dependencies": {
"fast-xml-parser": "^4.3.4",
"ignore": "^5.3.1",
"log4js": "^6.9.1",
"tslib": "^2.6.2",
"xml-disassembler": "^1.3.4"
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

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

48 changes: 11 additions & 37 deletions src/service/xml2jsonDisassembler.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
"use strict";

import { existsSync } from "node:fs";
import { stat, readdir, readFile } from "node:fs/promises";
import {
resolve,
join,
basename,
dirname,
extname,
relative,
} from "node:path/posix";
import ignore, { Ignore } from "ignore";
import { stat, readdir } from "node:fs/promises";
import { resolve, join, basename, dirname, extname } from "node:path/posix";

import { logger } from "@src/index";
import { disassembleHandler } from "@src/service/disassembleHandler";
import { transform2JSON } from "@src/service/transform2JSON";

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

async disassemble(xmlAttributes: {
filePath: string;
uniqueIdElements?: string;
Expand All @@ -33,12 +23,6 @@ export class XmlToJsonDisassembler {
postPurge = false,
ignorePath = ".xmldisassemblerignore",
} = xmlAttributes;
const resolvedIgnorePath = resolve(ignorePath);
if (existsSync(resolvedIgnorePath)) {
const content = await readFile(resolvedIgnorePath);
this.ign.add(content.toString());
}

const fileStat = await stat(filePath);

if (fileStat.isFile()) {
Expand All @@ -47,10 +31,6 @@ export class XmlToJsonDisassembler {
logger.error(`The file path is not an XML file: ${resolvedPath}`);
return;
}
if (this.ign.ignores(filePath)) {
logger.warn(`File ignored by ${ignorePath}: ${resolvedPath}`);
return;
}
await this.processFile({
filePath: resolvedPath,
uniqueIdElements,
Expand All @@ -60,25 +40,16 @@ export class XmlToJsonDisassembler {
});
} else if (fileStat.isDirectory()) {
const subFiles = await readdir(filePath);
const resolvedBasePath = dirname(resolvedIgnorePath); // Base path of the ignore file
for (const subFile of subFiles) {
const subFilePath = join(filePath, subFile);
const relativeSubFilePath = this.posixPath(
relative(resolvedBasePath, subFilePath),
);
if (
subFilePath.endsWith(".xml") &&
!this.ign.ignores(relativeSubFilePath)
) {
if (subFilePath.endsWith(".xml")) {
await this.processFile({
filePath: subFilePath,
uniqueIdElements,
prePurge,
postPurge,
ignorePath,
});
} else if (this.ign.ignores(relativeSubFilePath)) {
logger.warn(`File ignored by ${ignorePath}: ${subFilePath}`);
}
}
}
Expand All @@ -104,10 +75,13 @@ export class XmlToJsonDisassembler {
const fullName = basename(filePath, extname(filePath));
const basePath = dirname(filePath);
const baseName = fullName.split(".")[0];
await transform2JSON(join(basePath, baseName));
}
private posixPath(path: string): string {
// Normalize path to POSIX-style (for cross-platform compatibility)
return path.replace(/\\+/g, "/");
const disassemblePath = join(basePath, baseName);
if (existsSync(disassemblePath)) {
await transform2JSON(disassemblePath);
} else {
logger.warn(
`XML file ${filePath} was unable to disassembled into smaller files. Check the log file and your ignore file.`,
);
}
}
}

0 comments on commit 875be65

Please sign in to comment.