convert script to ES module conventions

This commit is contained in:
2026-05-13 16:04:08 +02:00
parent 29d7ea2a7a
commit 845e777f08
3 changed files with 31 additions and 81 deletions
+21 -81
View File
@@ -18,73 +18,13 @@
* - _manifest.json with extraction metadata * - _manifest.json with extraction metadata
*/ */
const fs = require('fs').promises; import { promises as fs } from 'fs';
const path = require('path'); import { join as _join, dirname, basename as _basename, extname } from 'path';
const { execSync } = require('child_process'); import { execSync } from 'child_process';
const { existsSync } = require('fs'); import { existsSync } from 'fs';
import crc32 from 'crc-32';
// Import xml-formatter (try multiple locations) import xmlFormatter from 'xml-formatter';
let xmlFormatter; import { XMLParser } from 'fast-xml-parser';
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);
}
}
// ============================================================================ // ============================================================================
// CONFIGURATION // CONFIGURATION
@@ -97,7 +37,7 @@ const CONFIG = {
INDEX_FILE: 'index', INDEX_FILE: 'index',
ITEM_PREFIX: 'item', ITEM_PREFIX: 'item',
INTERNALS_SUFFIX: '_internals', INTERNALS_SUFFIX: '_internals',
VERSION: '1.0.0' VERSION: '0.0.1',
}; };
// ============================================================================ // ============================================================================
@@ -165,7 +105,7 @@ const XML_PARSE_OPTIONS = {
function parseXmlDocument(xmlString) { function parseXmlDocument(xmlString) {
if (typeof xmlString !== 'string') return null; if (typeof xmlString !== 'string') return null;
try { try {
return new fastXmlParser.XMLParser(XML_PARSE_OPTIONS).parse(xmlString); return new XMLParser(XML_PARSE_OPTIONS).parse(xmlString);
} catch (error) { } catch (error) {
return null; return null;
} }
@@ -403,7 +343,7 @@ async function createOutputDirectory(outputPath, allowOverwrite = false, cleanEx
*/ */
async function saveStringProperty(dirPath, filename, content, options = {}) { async function saveStringProperty(dirPath, filename, content, options = {}) {
const ext = getFileExtension(content); const ext = getFileExtension(content);
const filepath = path.join(dirPath, `${filename}.${ext}`); const filepath = _join(dirPath, `${filename}.${ext}`);
let output = content; let output = content;
@@ -420,7 +360,7 @@ async function saveStringProperty(dirPath, filename, content, options = {}) {
* Save a JSON object to file * Save a JSON object to file
*/ */
async function saveJsonProperty(dirPath, filename, obj, options = {}) { 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); const output = prettifyJson(obj, CONFIG.JSON_INDENT);
await fs.writeFile(filepath, output, 'utf-8'); await fs.writeFile(filepath, output, 'utf-8');
@@ -431,7 +371,7 @@ async function saveJsonProperty(dirPath, filename, obj, options = {}) {
* Create subdirectory for a property * Create subdirectory for a property
*/ */
async function createPropertyDirectory(outputPath, propertyName) { async function createPropertyDirectory(outputPath, propertyName) {
const dirPath = path.join(outputPath, propertyName); const dirPath = _join(outputPath, propertyName);
await fs.mkdir(dirPath, { recursive: true }); await fs.mkdir(dirPath, { recursive: true });
return dirPath; return dirPath;
} }
@@ -440,7 +380,7 @@ async function createPropertyDirectory(outputPath, propertyName) {
* Save backup of original file * Save backup of original file
*/ */
async function saveBackup(outputPath, data) { 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); const output = prettifyJson(data, CONFIG.JSON_INDENT);
await fs.writeFile(backupPath, output, 'utf-8'); await fs.writeFile(backupPath, output, 'utf-8');
return backupPath; return backupPath;
@@ -451,7 +391,7 @@ async function saveBackup(outputPath, data) {
*/ */
async function createCrc32Checksum(outputPath, inputPath) { async function createCrc32Checksum(outputPath, inputPath) {
const fileCrc32 = await calculateFileCrc32(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'); await fs.writeFile(checksumPath, `${fileCrc32}\n`, 'utf-8');
return { checksumPath, fileCrc32 }; return { checksumPath, fileCrc32 };
} }
@@ -555,7 +495,7 @@ async function processArrayProperty(outputPath, propertyName, arr) {
} }
// Save index metadata // 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); const indexOutput = prettifyJson(indexData, CONFIG.JSON_INDENT);
await fs.writeFile(indexPath, indexOutput, 'utf-8'); await fs.writeFile(indexPath, indexOutput, 'utf-8');
result.files.unshift(indexPath); // Put index first in list 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 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); const output = prettifyJson(obj, CONFIG.JSON_INDENT);
await fs.writeFile(filepath, output, 'utf-8'); await fs.writeFile(filepath, output, 'utf-8');
@@ -604,7 +544,7 @@ async function processProperty(outputPath, propertyName, value) {
}; };
const dirPath = await createPropertyDirectory(outputPath, propertyName); 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'); await fs.writeFile(filepath, prettifyJson(value, CONFIG.JSON_INDENT), 'utf-8');
result.files.push(filepath); 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); const output = prettifyJson(manifest, CONFIG.JSON_INDENT);
await fs.writeFile(manifestPath, output, 'utf-8'); await fs.writeFile(manifestPath, output, 'utf-8');
@@ -672,9 +612,9 @@ async function main() {
} }
// Determine output directory // Determine output directory
const dir = path.dirname(inputPath); const dir = dirname(inputPath);
const basename = path.basename(inputPath, path.extname(inputPath)); const basename = _basename(inputPath, extname(inputPath));
const outputPath = path.join(dir, `${basename}${CONFIG.INTERNALS_SUFFIX}`); const outputPath = _join(dir, `${basename}${CONFIG.INTERNALS_SUFFIX}`);
console.log('\n📁 ElixForms JSON Extractor\n'); console.log('\n📁 ElixForms JSON Extractor\n');
console.log(`📖 Reading: ${inputPath}`); console.log(`📖 Reading: ${inputPath}`);
+3
View File
@@ -1,9 +1,12 @@
{ {
"name": "extract-elx", "name": "extract-elx",
"version": "0.0.1",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "extract-elx",
"version": "0.0.1",
"dependencies": { "dependencies": {
"crc-32": "^1.2.2", "crc-32": "^1.2.2",
"fast-xml-parser": "^5.8.0", "fast-xml-parser": "^5.8.0",
+7
View File
@@ -1,4 +1,11 @@
{ {
"name": "extract-elx",
"version": "0.0.1",
"description": "Extract root properties from ElixForms .elx JSON files",
"scripts": {
"start": "node extract-elx.js"
},
"type": "module",
"dependencies": { "dependencies": {
"crc-32": "^1.2.2", "crc-32": "^1.2.2",
"fast-xml-parser": "^5.8.0", "fast-xml-parser": "^5.8.0",