generated from AllanOricil/js-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reassemble the original XML file from the JSONs
- Loading branch information
Showing
9 changed files
with
259 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"use strict"; | ||
|
||
import { stat, readdir, rm } from "node:fs/promises"; | ||
import { join } from "node:path"; | ||
|
||
export async function deleteReassembledXML( | ||
disassembledPath: string, | ||
): Promise<void> { | ||
const files = await readdir(disassembledPath); | ||
for (const file of files) { | ||
const filePath = join(disassembledPath, file); | ||
const fileStat = await stat(filePath); | ||
if (fileStat.isFile() && filePath.endsWith(".xml")) { | ||
await rm(filePath); | ||
} else if (fileStat.isDirectory()) { | ||
await deleteReassembledXML(filePath); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"use strict"; | ||
|
||
import { stat, readdir } from "node:fs/promises"; | ||
import { join } from "node:path"; | ||
|
||
import { logger } from "@src/index"; | ||
import { reassembleHandler } from "@src/service/reassembleHandler"; | ||
import { transform2XML } from "@src/service/transform2XML"; | ||
import { deleteReassembledXML } from "@src/service/deleteReassembledXML"; | ||
|
||
export class JsonToXmlReassembler { | ||
async reassemble(xmlAttributes: { | ||
jsonPath: string; | ||
fileExtension?: string; | ||
postPurge?: boolean; | ||
}): Promise<void> { | ||
const { | ||
jsonPath, | ||
fileExtension = "xml", | ||
postPurge = false, | ||
} = xmlAttributes; | ||
const fileStat = await stat(jsonPath); | ||
|
||
if (fileStat.isFile()) { | ||
logger.error(`The path ${jsonPath} is not a directory.`); | ||
return; | ||
} else if (fileStat.isDirectory()) { | ||
await this.processFile(jsonPath); | ||
} | ||
|
||
await reassembleHandler(jsonPath, fileExtension, postPurge); | ||
// delete XML files created during reassembly - this is needed if postPurge is false | ||
if (!postPurge) await deleteReassembledXML(jsonPath); | ||
} | ||
|
||
async processFile(jsonPath: string): Promise<void> { | ||
const files = await readdir(jsonPath); | ||
for (const file of files) { | ||
const filePath = join(jsonPath, file); | ||
const fileStat = await stat(filePath); | ||
if (fileStat.isFile() && filePath.endsWith(".json")) { | ||
await transform2XML(filePath); | ||
} else if (fileStat.isDirectory()) { | ||
await this.processFile(filePath); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"use strict"; | ||
|
||
import { ReassembleXMLFileHandler } from "xml-disassembler"; | ||
|
||
export async function reassembleHandler( | ||
xmlPath: string, | ||
fileExtension: string, | ||
postpurge: boolean, | ||
): Promise<void> { | ||
const handler = new ReassembleXMLFileHandler(); | ||
await handler.reassemble({ | ||
xmlPath, | ||
fileExtension, | ||
postPurge: postpurge, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"use strict"; | ||
|
||
import { readFile, writeFile } from "node:fs/promises"; | ||
import { XMLBuilder } from "fast-xml-parser"; | ||
|
||
import { logger } from "@src/index"; | ||
import { JSON_PARSER_OPTION, INDENT } from "@src/helpers/types"; | ||
|
||
export async function transform2XML( | ||
jsonPath: string, | ||
indentLevel: number = 0, | ||
): Promise<void> { | ||
const jsonString = await readFile(jsonPath, "utf-8"); | ||
const jsonObject = JSON.parse(jsonString); | ||
|
||
// Remove XML declaration from JSON string | ||
const xmlBuilder = new XMLBuilder(JSON_PARSER_OPTION); | ||
const xmlString = xmlBuilder.build(jsonObject) as string; | ||
|
||
// Manually format the XML string with the desired indentation | ||
const formattedXml: string = xmlString | ||
.split("\n") | ||
.map((line: string) => `${" ".repeat(indentLevel * INDENT.length)}${line}`) | ||
.join("\n") | ||
.trimEnd(); | ||
|
||
const xmlPath = jsonPath.replace(/\.json$/, ".xml"); | ||
await writeFile(xmlPath, formattedXml); | ||
logger.debug(`${jsonPath} has been transformed into ${xmlPath}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters