convert script to ES module conventions
This commit is contained in:
@@ -18,73 +18,13 @@
|
||||
* - _manifest.json with extraction metadata
|
||||
*/
|
||||
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const { execSync } = require('child_process');
|
||||
const { existsSync } = require('fs');
|
||||
|
||||
// Import xml-formatter (try multiple locations)
|
||||
let xmlFormatter;
|
||||
try {
|
||||
// Try local node_modules first
|
||||
xmlFormatter = require('xml-formatter');
|
||||
} 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 globalXmlFormatter = path.join(npmPrefix, 'node_modules', 'xml-formatter');
|
||||
xmlFormatter = require(globalXmlFormatter);
|
||||
} catch (err2) {
|
||||
console.error(
|
||||
'\n❌ Missing dependency: xml-formatter\n' +
|
||||
'Please install globally with: npm install -g xml-formatter\n' +
|
||||
'Or locally in the current directory with: npm install xml-formatter\n'
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
// Import crc-32 for file integrity checks (try multiple locations)
|
||||
let crc32;
|
||||
try {
|
||||
// Try local node_modules first
|
||||
crc32 = require('crc-32');
|
||||
} 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 globalCrc32 = join(npmPrefix, 'node_modules', 'crc-32');
|
||||
crc32 = require(globalCrc32);
|
||||
} catch (err2) {
|
||||
console.error(
|
||||
'\n❌ Missing dependency: crc-32\n' +
|
||||
'Please install globally with: npm install -g crc-32\n' +
|
||||
'Or locally in the current directory with: npm install crc-32\n'
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
import { promises as fs } from 'fs';
|
||||
import { join as _join, dirname, basename as _basename, extname } from 'path';
|
||||
import { execSync } from 'child_process';
|
||||
import { existsSync } from 'fs';
|
||||
import crc32 from 'crc-32';
|
||||
import xmlFormatter from 'xml-formatter';
|
||||
import { XMLParser } from 'fast-xml-parser';
|
||||
|
||||
// ============================================================================
|
||||
// CONFIGURATION
|
||||
@@ -97,7 +37,7 @@ const CONFIG = {
|
||||
INDEX_FILE: 'index',
|
||||
ITEM_PREFIX: 'item',
|
||||
INTERNALS_SUFFIX: '_internals',
|
||||
VERSION: '1.0.0'
|
||||
VERSION: '0.0.1',
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
@@ -165,7 +105,7 @@ const XML_PARSE_OPTIONS = {
|
||||
function parseXmlDocument(xmlString) {
|
||||
if (typeof xmlString !== 'string') return null;
|
||||
try {
|
||||
return new fastXmlParser.XMLParser(XML_PARSE_OPTIONS).parse(xmlString);
|
||||
return new XMLParser(XML_PARSE_OPTIONS).parse(xmlString);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
@@ -403,7 +343,7 @@ async function createOutputDirectory(outputPath, allowOverwrite = false, cleanEx
|
||||
*/
|
||||
async function saveStringProperty(dirPath, filename, content, options = {}) {
|
||||
const ext = getFileExtension(content);
|
||||
const filepath = path.join(dirPath, `${filename}.${ext}`);
|
||||
const filepath = _join(dirPath, `${filename}.${ext}`);
|
||||
|
||||
let output = content;
|
||||
|
||||
@@ -420,7 +360,7 @@ async function saveStringProperty(dirPath, filename, content, options = {}) {
|
||||
* Save a JSON object to file
|
||||
*/
|
||||
async function saveJsonProperty(dirPath, filename, obj, options = {}) {
|
||||
const filepath = path.join(dirPath, `${filename}.json`);
|
||||
const filepath = _join(dirPath, `${filename}.json`);
|
||||
const output = prettifyJson(obj, CONFIG.JSON_INDENT);
|
||||
|
||||
await fs.writeFile(filepath, output, 'utf-8');
|
||||
@@ -431,7 +371,7 @@ async function saveJsonProperty(dirPath, filename, obj, options = {}) {
|
||||
* Create subdirectory for a property
|
||||
*/
|
||||
async function createPropertyDirectory(outputPath, propertyName) {
|
||||
const dirPath = path.join(outputPath, propertyName);
|
||||
const dirPath = _join(outputPath, propertyName);
|
||||
await fs.mkdir(dirPath, { recursive: true });
|
||||
return dirPath;
|
||||
}
|
||||
@@ -440,7 +380,7 @@ async function createPropertyDirectory(outputPath, propertyName) {
|
||||
* Save backup of original file
|
||||
*/
|
||||
async function saveBackup(outputPath, data) {
|
||||
const backupPath = path.join(outputPath, CONFIG.BACKUP_SUFFIX);
|
||||
const backupPath = _join(outputPath, CONFIG.BACKUP_SUFFIX);
|
||||
const output = prettifyJson(data, CONFIG.JSON_INDENT);
|
||||
await fs.writeFile(backupPath, output, 'utf-8');
|
||||
return backupPath;
|
||||
@@ -451,7 +391,7 @@ async function saveBackup(outputPath, data) {
|
||||
*/
|
||||
async function createCrc32Checksum(outputPath, inputPath) {
|
||||
const fileCrc32 = await calculateFileCrc32(inputPath);
|
||||
const checksumPath = path.join(outputPath, `crc32.txt`);
|
||||
const checksumPath = _join(outputPath, `crc32.txt`);
|
||||
await fs.writeFile(checksumPath, `${fileCrc32}\n`, 'utf-8');
|
||||
return { checksumPath, fileCrc32 };
|
||||
}
|
||||
@@ -555,7 +495,7 @@ async function processArrayProperty(outputPath, propertyName, arr) {
|
||||
}
|
||||
|
||||
// Save index metadata
|
||||
const indexPath = path.join(dirPath, `${CONFIG.INDEX_FILE}.json`);
|
||||
const indexPath = _join(dirPath, `${CONFIG.INDEX_FILE}.json`);
|
||||
const indexOutput = prettifyJson(indexData, CONFIG.JSON_INDENT);
|
||||
await fs.writeFile(indexPath, indexOutput, 'utf-8');
|
||||
result.files.unshift(indexPath); // Put index first in list
|
||||
@@ -575,7 +515,7 @@ async function processObjectProperty(outputPath, propertyName, obj) {
|
||||
};
|
||||
|
||||
const dirPath = await createPropertyDirectory(outputPath, propertyName);
|
||||
const filepath = path.join(dirPath, `${CONFIG.INDEX_FILE}.json`);
|
||||
const filepath = _join(dirPath, `${CONFIG.INDEX_FILE}.json`);
|
||||
const output = prettifyJson(obj, CONFIG.JSON_INDENT);
|
||||
|
||||
await fs.writeFile(filepath, output, 'utf-8');
|
||||
@@ -604,7 +544,7 @@ async function processProperty(outputPath, propertyName, value) {
|
||||
};
|
||||
|
||||
const dirPath = await createPropertyDirectory(outputPath, propertyName);
|
||||
const filepath = path.join(dirPath, `${CONFIG.INDEX_FILE}.json`);
|
||||
const filepath = _join(dirPath, `${CONFIG.INDEX_FILE}.json`);
|
||||
await fs.writeFile(filepath, prettifyJson(value, CONFIG.JSON_INDENT), 'utf-8');
|
||||
result.files.push(filepath);
|
||||
|
||||
@@ -640,7 +580,7 @@ async function createManifest(outputPath, data, processedProperties) {
|
||||
};
|
||||
}
|
||||
|
||||
const manifestPath = path.join(outputPath, CONFIG.MANIFEST_FILE);
|
||||
const manifestPath = _join(outputPath, CONFIG.MANIFEST_FILE);
|
||||
const output = prettifyJson(manifest, CONFIG.JSON_INDENT);
|
||||
await fs.writeFile(manifestPath, output, 'utf-8');
|
||||
|
||||
@@ -672,9 +612,9 @@ async function main() {
|
||||
}
|
||||
|
||||
// Determine output directory
|
||||
const dir = path.dirname(inputPath);
|
||||
const basename = path.basename(inputPath, path.extname(inputPath));
|
||||
const outputPath = path.join(dir, `${basename}${CONFIG.INTERNALS_SUFFIX}`);
|
||||
const dir = dirname(inputPath);
|
||||
const basename = _basename(inputPath, extname(inputPath));
|
||||
const outputPath = _join(dir, `${basename}${CONFIG.INTERNALS_SUFFIX}`);
|
||||
|
||||
console.log('\n📁 ElixForms JSON Extractor\n');
|
||||
console.log(`📖 Reading: ${inputPath}`);
|
||||
|
||||
Reference in New Issue
Block a user