raw-csv.js 862 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. const json2csv = require('json2csv')
  2. module.exports.format = function (data, options) {
  3. const csv = json2csv({ data })
  4. if(options.program) {
  5. // If reporting output is defined, ignore console log here.
  6. if (options.program.reportOutput === undefined) {
  7. console.log(csv)
  8. }
  9. } else {
  10. console.log(csv)
  11. }
  12. return csv
  13. }
  14. const fs = require('fs-extra')
  15. const path = require('path')
  16. module.exports.finalReport = async function(reports, program) {
  17. if (program.reportOutput === undefined) {
  18. return
  19. }
  20. // Ensure the output directory exists.
  21. fs.ensureDirSync(program.reportOutput)
  22. // Write each report to the disk
  23. for(var report of reports) {
  24. var outPath = path.join(program.reportOutput, report.name + '.csv')
  25. console.log('saving', outPath)
  26. fs.writeFileSync(outPath, report.contents, 'utf8')
  27. }
  28. }