raw-json.js 1.4 KB

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