1. const fs = require('fs');
  2. const path = require('path');
  3. const { v4: uuidv4 } = require('uuid');
  4. /**
  5. * Archives text block content for short-lived tasks with verbose logging.
  6. * @param {string} title - Title of the archived content.
  7. * @param {string} content - The text content to archive.
  8. */
  9. function archiveText(title, content) {
  10. const timestamp = new Date().toISOString();
  11. const filename = `${timestamp}_${uuidv4()}.txt`; // Unique filename
  12. const filepath = path.join(__dirname, 'archives', filename); // Archive directory
  13. // Ensure the archives directory exists
  14. if (!fs.existsSync(path.join(__dirname, 'archives'))) {
  15. fs.mkdirSync(path.join(__dirname, 'archives'));
  16. }
  17. try {
  18. fs.writeFileSync(filepath, content); // Write content to file
  19. console.log(`[INFO] Archived content: ${title} to ${filepath}`); // Verbose log
  20. } catch (error) {
  21. console.error(`[ERROR] Failed to archive content: ${error.message}`); // Verbose log
  22. }
  23. }
  24. module.exports = archiveText;

Add your comment