1. const fs = require('fs');
  2. const path = require('path');
  3. /**
  4. * Strips metadata from log files.
  5. * @param {string} filePath - The path to the log file.
  6. * @param {string} outputFilePath - The path to save the metadata-stripped log file.
  7. */
  8. function stripMetadata(filePath, outputFilePath) {
  9. try {
  10. const fileContent = fs.readFileSync(filePath, 'utf8');
  11. let cleanedContent = '';
  12. // Simple metadata removal: Remove lines starting with common metadata prefixes.
  13. const metadataPrefixes = ['#', '[', '{', ' ', ' ', ' ', ' ', ' ']; // Add more prefixes as needed.
  14. const lines = fileContent.split('\n');
  15. for (const line of lines) {
  16. let isMetadata = false;
  17. for (const prefix of metadataPrefixes) {
  18. if (line.startsWith(prefix)) {
  19. isMetadata = true;
  20. break;
  21. }
  22. }
  23. if (!isMetadata) {
  24. cleanedContent += line + '\n';
  25. }
  26. }
  27. fs.writeFileSync(outputFilePath, cleanedContent, 'utf8');
  28. console.log(`Metadata stripped from ${filePath} and saved to ${outputFilePath}`);
  29. } catch (error) {
  30. console.error(`Error processing ${filePath}: ${error.message}`);
  31. }
  32. }
  33. module.exports = stripMetadata;

Add your comment