json.js 1.6 KB

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