raw-csv.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const json2csv = require('json2csv')
  2. const log = require('../util/log')
  3. const fs = require('fs-extra')
  4. const path = require('path')
  5. module.exports = {
  6. // This report wants raw data.
  7. isRaw: true,
  8. format (data, options) {
  9. const csv = json2csv({ data })
  10. if (options.program) {
  11. // If reporting output is defined, ignore console log here.
  12. if (options.program.output === undefined) {
  13. log.raw(csv)
  14. }
  15. } else {
  16. log.raw(csv)
  17. }
  18. return csv
  19. },
  20. finalReport (reports, program) {
  21. if (program.output === undefined) {
  22. return
  23. }
  24. // Ensure the output directory exists.
  25. fs.ensureDirSync(program.output)
  26. // Write each report to the disk
  27. for (var report of reports) {
  28. var outPath = path.join(program.output, report.name + '.csv')
  29. log.action('saving', outPath)
  30. if (program.output === '-') {
  31. console.log(JSON.stringify(report.contents))
  32. } else {
  33. fs.writeFileSync(outPath, report.contents, 'utf8')
  34. }
  35. }
  36. }
  37. }