#!/usr/bin/env node
const { program } = require('commander');
const fs = require('fs');
const path = require('path');
program
.version('1.0.0')
.description('Convert experiment log formats');
program
.command('convert <inputFormat> <outputFormat> <inputFilePath> <outputFilePath>')
.description('Convert log file from one format to another')
.option('-d, --delimiter <delimiter>', 'Delimiter for log entries (default: \\n)')
.action((inputFormat, outputFormat, inputFilePath, outputFilePath, options) => {
const delimiter = options.delimiter || '\n';
try {
const logEntries = fs.readFileSync(inputFilePath, 'utf8').split(delimiter);
let convertedEntries = [];
for (const entry of logEntries) {
// Basic conversion logic - customize as needed
let convertedEntry = entry;
if (inputFormat === 'raw' && outputFormat === 'json') {
try {
const parsed = JSON.parse(entry);
convertedEntry = JSON.stringify(parsed, null, 2); //pretty print
} catch (e) {
console.error(`Error parsing JSON: ${e}`);
convertedEntry = `Error: Invalid JSON - ${entry}`;
}
} else if (inputFormat === 'json' && outputFormat === 'raw') {
try {
const parsed = JSON.parse(entry);
convertedEntry = JSON.stringify(parsed);
} catch (e) {
console.error(`Error parsing JSON: ${e}`);
convertedEntry = `Error: Invalid JSON - ${entry}`;
}
} else if (inputFormat === 'raw' && outputFormat === 'csv') {
// Simple split by comma
convertedEntry = entry.split(',').join(',');
}
//Add more conversion rules as needed
convertedEntries.push(convertedEntry);
}
fs.writeFileSync(outputFilePath, convertedEntries.join(delimiter), 'utf8');
console.log(`Successfully converted ${inputFilePath} to ${outputFilePath}`);
} catch (err) {
console.error(`Error processing files: ${err.message}`);
}
});
program.parse(process.argv);
Add your comment