Skip to content

Commit

Permalink
fix: normalize paths to unix style
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Dec 15, 2024
1 parent 2210fea commit 2f3f393
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:

Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/service/deleteReassembledXML.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/service/transform2YAML.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
18 changes: 16 additions & 2 deletions src/service/xml2yamlDisassembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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, "/");
}
}
2 changes: 1 addition & 1 deletion src/service/yaml2xmlReassembler.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion test/main.spec.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 2f3f393

Please sign in to comment.