add fast-xml-parser dependency management

This commit is contained in:
2026-05-12 15:36:09 +02:00
parent a28e2968f8
commit f4da370e08
+22 -4
View File
@@ -22,7 +22,6 @@ const fs = require('fs').promises;
const path = require('path'); const path = require('path');
const { execSync } = require('child_process'); const { execSync } = require('child_process');
const { existsSync } = require('fs'); const { existsSync } = require('fs');
const { XMLParser } = require('fast-xml-parser');
// Import xml-formatter (try multiple locations) // Import xml-formatter (try multiple locations)
let xmlFormatter; let xmlFormatter;
@@ -45,6 +44,27 @@ try {
} }
} }
// Import fast-xml-parser (try multiple locations)
let fastXmlParser;
try {
// Try local node_modules first
fastXmlParser = require('fast-xml-parser');
} catch (err) {
try {
// Try to find npm global prefix and load from there
const npmPrefix = execSync('npm config get prefix', { encoding: 'utf-8' }).trim();
const globalFastXmlParser = path.join(npmPrefix, 'node_modules', 'fast-xml-parser');
fastXmlParser = require(globalFastXmlParser);
} catch (err2) {
console.error(
'\n❌ Missing dependency: fast-xml-parser\n' +
'Please install globally with: npm install -g fast-xml-parser\n' +
'Or locally in the current directory with: npm install fast-xml-parser\n'
);
process.exit(1);
}
}
// ============================================================================ // ============================================================================
// CONFIGURATION // CONFIGURATION
// ============================================================================ // ============================================================================
@@ -119,12 +139,10 @@ const XML_PARSE_OPTIONS = {
trimValues: false, trimValues: false,
}; };
const xmlParser = new XMLParser(XML_PARSE_OPTIONS);
function parseXmlDocument(xmlString) { function parseXmlDocument(xmlString) {
if (typeof xmlString !== 'string') return null; if (typeof xmlString !== 'string') return null;
try { try {
return xmlParser.parse(xmlString); return new fastXmlParser.XMLParser(XML_PARSE_OPTIONS).parse(xmlString);
} catch (error) { } catch (error) {
return null; return null;
} }