BaseFolder.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _Node = _interopRequireDefault(require("./Node"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. class BaseFolder extends _Node.default {
  9. constructor(name, parent) {
  10. super(name, parent);
  11. this.children = Object.create(null);
  12. }
  13. get src() {
  14. if (!Object.prototype.hasOwnProperty.call(this, '_src')) {
  15. this._src = this.walk((node, src) => src += node.src || '', '', false);
  16. }
  17. return this._src;
  18. }
  19. get size() {
  20. if (!Object.prototype.hasOwnProperty.call(this, '_size')) {
  21. this._size = this.walk((node, size) => size + node.size, 0, false);
  22. }
  23. return this._size;
  24. }
  25. getChild(name) {
  26. return this.children[name];
  27. }
  28. addChildModule(module) {
  29. const {
  30. name
  31. } = module;
  32. const currentChild = this.children[name]; // For some reason we already have this node in children and it's a folder.
  33. if (currentChild && currentChild instanceof BaseFolder) return;
  34. if (currentChild) {
  35. // We already have this node in children and it's a module.
  36. // Merging it's data.
  37. currentChild.mergeData(module.data);
  38. } else {
  39. // Pushing new module
  40. module.parent = this;
  41. this.children[name] = module;
  42. }
  43. delete this._size;
  44. delete this._src;
  45. }
  46. addChildFolder(folder) {
  47. folder.parent = this;
  48. this.children[folder.name] = folder;
  49. delete this._size;
  50. delete this._src;
  51. return folder;
  52. }
  53. walk(walker, state = {}, deep = true) {
  54. let stopped = false;
  55. Object.values(this.children).forEach(child => {
  56. if (deep && child.walk) {
  57. state = child.walk(walker, state, stop);
  58. } else {
  59. state = walker(child, state, stop);
  60. }
  61. if (stopped) return false;
  62. });
  63. return state;
  64. function stop(finalState) {
  65. stopped = true;
  66. return finalState;
  67. }
  68. }
  69. mergeNestedFolders() {
  70. if (!this.isRoot) {
  71. let childNames;
  72. while ((childNames = Object.keys(this.children)).length === 1) {
  73. const childName = childNames[0];
  74. const onlyChild = this.children[childName];
  75. if (onlyChild instanceof this.constructor) {
  76. this.name += `/${onlyChild.name}`;
  77. this.children = onlyChild.children;
  78. } else {
  79. break;
  80. }
  81. }
  82. }
  83. this.walk(child => {
  84. child.parent = this;
  85. if (child.mergeNestedFolders) {
  86. child.mergeNestedFolders();
  87. }
  88. }, null, false);
  89. }
  90. toChartData() {
  91. return {
  92. label: this.name,
  93. path: this.path,
  94. statSize: this.size,
  95. groups: Object.values(this.children).map(child => child.toChartData())
  96. };
  97. }
  98. }
  99. exports.default = BaseFolder;
  100. ;