diff --git a/README.md b/README.md index b1e260a..cd86cb1 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ npm install xml2yaml-disassembler ```typescript /* FLAGS -- filePath: Path to 1 XML file or a directory of XML files to disassemble, then transform into YAML files. If the path provided is a directory, only the files in the immediate directory will be disassembled and transformed. +- filePath: Relative path to 1 XML file or a directory of XML files to disassemble, then transform into YAML files. If the path provided is a directory, only the files in the immediate directory will be disassembled and transformed. - 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 transformed directories prior to disassembling and transformed the file. @@ -51,7 +51,7 @@ await handler.disassemble({ }); ``` -Disassemble then transform 1 or multiple XML files into YAML files. If the `filePath` is a directory, only the XMLs in the immediate directory will be processed. Each XML wiill be transformed into YAML files in new sub-directories using the XML's base name (everything before the first period in the file-name). +Disassemble then transform 1 or multiple XML files into YAML files. Paths provided must be **relative** paths. If the `filePath` is a directory, only the XMLs in the immediate directory will be processed. Each XML wiill be transformed into YAML files in new sub-directories using the XML's base name (everything before the first period in the file-name). Example: @@ -122,7 +122,7 @@ will be disassembled into a sub-directory named `HR_Admin` as such: ```typescript /* FLAGS -- filePath: Path to the directory containing the YAML files to reassemble into 1 XML file (must be a directory). +- filePath: Relative path to the directory containing the YAML files to reassemble into 1 XML file (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 directory containing YAML files after the XML is reassembled. Defaults to false. @@ -137,7 +137,7 @@ await handler.reassemble({ }); ``` -Reassemble all of the YAML files in a directory into 1 XML file. **Note:** You should only be reassembling YAML files created by the `XmlToYamlDisassembler` class for intended results. 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. +Reassemble all of the YAML files in a directory into 1 XML file. Path provided must be a **relative** path. **Note:** You should only be reassembling YAML files created by the `XmlToYamlDisassembler` class for intended results. 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. ## Ignore File diff --git a/src/service/deleteReassembledXML.ts b/src/service/deleteReassembledXML.ts index 9fcfcc1..be1a6f8 100644 --- a/src/service/deleteReassembledXML.ts +++ b/src/service/deleteReassembledXML.ts @@ -1,7 +1,7 @@ "use strict"; import { stat, readdir, rm } from "node:fs/promises"; -import { join } from "node:path"; +import { join } from "node:path/posix"; export async function deleteReassembledXML( disassembledPath: string, diff --git a/src/service/transform2YAML.ts b/src/service/transform2YAML.ts index 9d34138..399bc29 100644 --- a/src/service/transform2YAML.ts +++ b/src/service/transform2YAML.ts @@ -1,7 +1,7 @@ "use strict"; import { readdir, readFile, rm, stat, writeFile } from "node:fs/promises"; -import { join } from "node:path"; +import { join } from "node:path/posix"; import { XMLParser } from "fast-xml-parser"; import { stringify } from "yaml"; diff --git a/src/service/xml2yamlDisassembler.ts b/src/service/xml2yamlDisassembler.ts index 70aece3..9f71187 100644 --- a/src/service/xml2yamlDisassembler.ts +++ b/src/service/xml2yamlDisassembler.ts @@ -2,7 +2,14 @@ import { existsSync } from "node:fs"; import { stat, readdir, readFile } from "node:fs/promises"; -import { resolve, join, basename, dirname, extname, relative } from "node:path"; +import { + resolve, + join, + basename, + dirname, + extname, + relative, +} from "node:path/posix"; import ignore, { Ignore } from "ignore"; import { logger } from "@src/index"; @@ -53,9 +60,12 @@ export class XmlToYamlDisassembler { }); } 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 = relative(process.cwd(), subFilePath); + const relativeSubFilePath = this.posixPath( + relative(resolvedBasePath, subFilePath), + ); if ( subFilePath.endsWith(".xml") && !this.ign.ignores(relativeSubFilePath) @@ -96,4 +106,8 @@ export class XmlToYamlDisassembler { const baseName = fullName.split(".")[0]; await transform2YAML(join(basePath, baseName)); } + private posixPath(path: string): string { + // Normalize path to POSIX-style (for cross-platform compatibility) + return path.replace(/\\+/g, "/"); + } } diff --git a/src/service/yaml2xmlReassembler.ts b/src/service/yaml2xmlReassembler.ts index e046869..a9757dc 100644 --- a/src/service/yaml2xmlReassembler.ts +++ b/src/service/yaml2xmlReassembler.ts @@ -1,7 +1,7 @@ "use strict"; import { stat, readdir } from "node:fs/promises"; -import { join } from "node:path"; +import { join } from "node:path/posix"; import { logger } from "@src/index"; import { reassembleHandler } from "@src/service/reassembleHandler"; diff --git a/test/main.spec.ts b/test/main.spec.ts index 626a3f0..5912d1e 100644 --- a/test/main.spec.ts +++ b/test/main.spec.ts @@ -1,6 +1,6 @@ import { readdir, readFile, writeFile, rm } from "node:fs/promises"; import { strictEqual } from "node:assert"; -import { resolve, join } from "node:path"; +import { resolve, join } from "node:path/posix"; import { copy } from "fs-extra"; import {