json.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. module.exports.format = function (data, options) {
  2. var data = data.map(el => {
  3. var row = {}
  4. // Iterate over the columns and add each item to the new row.
  5. for (var key in options.columns) {
  6. row[key] = options.columns[key](el)
  7. }
  8. return row
  9. })
  10. // Strigify the output, using 2 space indent.
  11. var output = JSON.stringify(data, null, 2)
  12. if(options.program) {
  13. // If reporting output is defined, ignore console log here.
  14. if (options.program.reportOutput === undefined) {
  15. console.log(output)
  16. } else {
  17. return data
  18. }
  19. } else {
  20. console.log(output)
  21. }
  22. return data
  23. }
  24. const fs = require('fs-extra')
  25. const path = require('path')
  26. module.exports.finalReport = async function(reports, program) {
  27. if (program.reportOutput === undefined) {
  28. return
  29. }
  30. if (program.joinReports) {
  31. var out = {}
  32. for(var report of reports) {
  33. console.log('saving report', report.name)
  34. out[report.name] = report.contents
  35. }
  36. if (program.reportOutput == '-') {
  37. console.log(JSON.stringify(out, null, 2))
  38. } else {
  39. // fs.ensureDirSync(path.dirname(program.reportOutput))
  40. //fs.copySync(sourceFile, outDir)
  41. var outPath = program.reportOutput + '.json'
  42. console.log('writing joined to', outPath)
  43. fs.writeFileSync(outPath, JSON.stringify(out, null, 2), 'utf8')
  44. }
  45. } else {
  46. console.log(program.reportOutput)
  47. fs.ensureDirSync(program.reportOutput)
  48. for(var report of reports) {
  49. var outPath = path.join(program.reportOutput, report.name + '.json')
  50. console.log('saving', outPath)
  51. fs.writeFileSync(outPath, JSON.stringify(report.contents, null, 2), 'utf8')
  52. }
  53. }
  54. }