DebugLogger.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DebugLogger = void 0;
  4. const fs_extra_1 = require("fs-extra");
  5. const util_1 = require("./util");
  6. class DebugLogger {
  7. constructor(isEnabled = true) {
  8. this.isEnabled = isEnabled;
  9. this.data = {};
  10. }
  11. add(key, value) {
  12. if (!this.isEnabled) {
  13. return;
  14. }
  15. const dataPath = key.split(".");
  16. let o = this.data;
  17. let lastName = null;
  18. for (const p of dataPath) {
  19. if (p === dataPath[dataPath.length - 1]) {
  20. lastName = p;
  21. break;
  22. }
  23. else {
  24. if (o[p] == null) {
  25. o[p] = Object.create(null);
  26. }
  27. else if (typeof o[p] === "string") {
  28. o[p] = [o[p]];
  29. }
  30. o = o[p];
  31. }
  32. }
  33. if (Array.isArray(o[lastName])) {
  34. o[lastName] = [...o[lastName], value];
  35. }
  36. else {
  37. o[lastName] = value;
  38. }
  39. }
  40. save(file) {
  41. // toml and json doesn't correctly output multiline string as multiline
  42. if (this.isEnabled && Object.keys(this.data).length > 0) {
  43. return (0, fs_extra_1.outputFile)(file, (0, util_1.serializeToYaml)(this.data));
  44. }
  45. else {
  46. return Promise.resolve();
  47. }
  48. }
  49. }
  50. exports.DebugLogger = DebugLogger;
  51. //# sourceMappingURL=DebugLogger.js.map