1. /**
  2. * Exports dataset results for dry-run scenarios, supporting older versions.
  3. *
  4. * @param {object} results - The results object containing the dataset data.
  5. * @param {string} format - The desired output format ('json', 'csv', 'txt').
  6. * @param {string} filename - The name of the output file.
  7. * @param {object} options - Optional configuration options.
  8. * @param {string} options.version - The version of the API to support (e.g., 'v1', 'v2').
  9. */
  10. function exportDryRunData(results, format, filename, options = {}) {
  11. const version = options.version || 'v1'; // Default to v1 if version not specified
  12. if (!results || typeof results !== 'object') {
  13. console.error("Invalid results object provided.");
  14. return;
  15. }
  16. if (!format || !['json', 'csv', 'txt'].includes(format)) {
  17. console.error("Invalid format specified. Supported formats: json, csv, txt");
  18. return;
  19. }
  20. try {
  21. let data;
  22. switch (format) {
  23. case 'json':
  24. data = JSON.stringify(results, null, 2); // Pretty print JSON
  25. break;
  26. case 'csv':
  27. data = results.map(row => Object.values(row).join(',')).join('\n'); // Convert object to CSV
  28. break;
  29. case 'txt':
  30. data = JSON.stringify(results, null, 2); // Serialize to string
  31. break;
  32. default:
  33. console.error("Unsupported format.");
  34. return;
  35. }
  36. const fs = require('fs'); // Node.js file system module
  37. fs.writeFileSync(filename, data); // Write data to file
  38. console.log(`Dry-run data exported to ${filename} in ${format} format (Version ${version}).`);
  39. } catch (error) {
  40. console.error("Error exporting data:", error);
  41. }
  42. }

Add your comment