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