raw-json.js 1.5 KB

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