raw-json.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. module.exports.format = function (data, options) {
  2. var output = JSON.stringify(data)
  3. if(options.program) {
  4. // If reporting output is defined, ignore console log here.
  5. if (options.program.reportOutput === undefined) {
  6. console.log(output)
  7. } else {
  8. return data
  9. }
  10. } else {
  11. console.log(output)
  12. }
  13. return data
  14. }
  15. const fs = require('fs-extra')
  16. const path = require('path')
  17. module.exports.finalReport = async function(reports, program) {
  18. if (program.reportOutput === undefined) {
  19. return
  20. }
  21. if (program.joinReports) {
  22. var out = {}
  23. for(var report of reports) {
  24. console.log('saving report', report.name)
  25. out[report.name] = report.contents
  26. }
  27. if (program.reportOutput == '-') {
  28. console.log(JSON.stringify(out, null, 2))
  29. } else {
  30. // fs.ensureDirSync(path.dirname(program.reportOutput))
  31. //fs.copySync(sourceFile, outDir)
  32. var outPath = program.reportOutput + '.json'
  33. console.log('writing joined to', outPath)
  34. fs.writeFileSync(outPath, JSON.stringify(out), 'utf8')
  35. }
  36. } else {
  37. // Ensure the output directory exists.
  38. fs.ensureDirSync(program.reportOutput)
  39. // Write each report to the disk
  40. for(var report of reports) {
  41. var outPath = path.join(program.reportOutput, report.name + '.json')
  42. console.log('saving', outPath)
  43. fs.writeFileSync(outPath, JSON.stringify(report.contents), 'utf8')
  44. }
  45. }
  46. }