BaseFolder.js 2.8 KB

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