raw-csv.js 854 B

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