1 | 'use strict';
|
2 |
|
3 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
4 |
|
5 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
6 |
|
7 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
8 |
|
9 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
10 |
|
11 | var _ = require('lodash');
|
12 | var filesize = require('filesize');
|
13 |
|
14 | var Node = function () {
|
15 | function Node(name, parent) {
|
16 | _classCallCheck(this, Node);
|
17 |
|
18 | this.name = name;
|
19 | this.parent = parent;
|
20 | }
|
21 |
|
22 | Node.prototype.toString = function toString(indent) {
|
23 | indent = indent || '|';
|
24 |
|
25 | return indent + ' ' + this.name;
|
26 | };
|
27 |
|
28 | _createClass(Node, [{
|
29 | key: 'path',
|
30 | get: function get() {
|
31 | var path = [];
|
32 | var node = this;
|
33 |
|
34 | while (node) {
|
35 | path.push(node.name);
|
36 | node = node.parent;
|
37 | }
|
38 |
|
39 | return path.reverse().join('/');
|
40 | }
|
41 | }]);
|
42 |
|
43 | return Node;
|
44 | }();
|
45 |
|
46 | var File = function (_Node) {
|
47 | _inherits(File, _Node);
|
48 |
|
49 | function File(name, data, parent) {
|
50 | _classCallCheck(this, File);
|
51 |
|
52 | var _this = _possibleConstructorReturn(this, _Node.call(this, name, parent));
|
53 |
|
54 | _this.data = data;
|
55 | return _this;
|
56 | }
|
57 |
|
58 | File.prototype.toString = function toString(indent) {
|
59 | return _Node.prototype.toString.call(this, indent) + ' [' + this.data.id + '] (' + filesize(this.size) + ' / ' + filesize(this.parsedSize) + ')';
|
60 | };
|
61 |
|
62 | File.prototype.toChartData = function toChartData() {
|
63 | return {
|
64 | id: this.data.id,
|
65 | label: this.name,
|
66 | path: this.path,
|
67 | weight: this.parsedSize === undefined ? this.size : this.parsedSize,
|
68 | statSize: this.size,
|
69 | parsedSize: this.parsedSize
|
70 | };
|
71 | };
|
72 |
|
73 | _createClass(File, [{
|
74 | key: 'size',
|
75 | get: function get() {
|
76 | return this.data.size;
|
77 | }
|
78 | }, {
|
79 | key: 'parsedSize',
|
80 | get: function get() {
|
81 | return this.data.parsedSize;
|
82 | }
|
83 | }]);
|
84 |
|
85 | return File;
|
86 | }(Node);
|
87 |
|
88 | var Folder = function (_Node2) {
|
89 | _inherits(Folder, _Node2);
|
90 |
|
91 | function Folder(name, parent) {
|
92 | _classCallCheck(this, Folder);
|
93 |
|
94 | var _this2 = _possibleConstructorReturn(this, _Node2.call(this, name, parent));
|
95 |
|
96 | _this2.children = Object.create(null);
|
97 | return _this2;
|
98 | }
|
99 |
|
100 | Folder.prototype.getChild = function getChild(name) {
|
101 | return this.children[name];
|
102 | };
|
103 |
|
104 | Folder.prototype.addFolder = function addFolder(name) {
|
105 | var folder = new Folder(name, this);
|
106 |
|
107 | this.children[name] = folder;
|
108 | delete this._size;
|
109 | delete this._parsedSize;
|
110 |
|
111 | return folder;
|
112 | };
|
113 |
|
114 | Folder.prototype.addFile = function addFile(name, data) {
|
115 | var overwrite = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
116 |
|
117 | var file = new File(name, data, this);
|
118 |
|
119 | if (this.children[name] && !overwrite) {
|
120 | return this.children[name];
|
121 | }
|
122 |
|
123 | this.children[name] = file;
|
124 | delete this._size;
|
125 | delete this._parsedSize;
|
126 |
|
127 | return file;
|
128 | };
|
129 |
|
130 | Folder.prototype.addFileByPath = function addFileByPath(path, data) {
|
131 | var _ref = [path.slice(0, -1), _.last(path)],
|
132 | folderNames = _ref[0],
|
133 | fileName = _ref[1];
|
134 |
|
135 | var currentFolder = this;
|
136 |
|
137 | _.each(folderNames, function (folderName) {
|
138 | currentFolder = currentFolder.getChild(folderName) || currentFolder.addFolder(folderName);
|
139 | });
|
140 |
|
141 | currentFolder.addFile(fileName, data);
|
142 | };
|
143 |
|
144 | Folder.prototype.walk = function walk(walker) {
|
145 | var state = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
146 |
|
147 | var stopped = false;
|
148 |
|
149 | _.each(this.children, function (child) {
|
150 | if (child.walk) {
|
151 | state = child.walk(walker, state, stop);
|
152 | } else {
|
153 | state = walker(child, state, stop);
|
154 | }
|
155 |
|
156 | if (stopped) return false;
|
157 | });
|
158 |
|
159 | return state;
|
160 |
|
161 | function stop(finalState) {
|
162 | stopped = true;
|
163 | return finalState;
|
164 | }
|
165 | };
|
166 |
|
167 | Folder.prototype.toString = function toString(indent, opts) {
|
168 | var _ref2 = opts || {},
|
169 | sortBy = _ref2.sortBy;
|
170 |
|
171 | indent = indent || '|';
|
172 |
|
173 | var str = indent + ' ' + this.name + ' (' + filesize(this.size) + ' / ' + filesize(this.parsedSize) + ')\n';
|
174 |
|
175 | str += _(this.children).sortBy(sortBy).reverse().map(function (child) {
|
176 | return child.toString(indent + ' |', opts);
|
177 | }).join('\n');
|
178 |
|
179 | return str;
|
180 | };
|
181 |
|
182 | Folder.prototype.toChartData = function toChartData() {
|
183 | return {
|
184 | label: this.name,
|
185 | path: this.path,
|
186 | weight: this.parsedSize === undefined ? this.size : this.parsedSize,
|
187 | statSize: this.size,
|
188 | parsedSize: this.parsedSize,
|
189 | groups: _.invokeMap(this.children, 'toChartData')
|
190 | };
|
191 | };
|
192 |
|
193 | _createClass(Folder, [{
|
194 | key: 'size',
|
195 | get: function get() {
|
196 | if (!_.has(this, '_size')) {
|
197 | this._size = this.walk(function (node, size) {
|
198 | return size + node.size;
|
199 | }, 0);
|
200 | }
|
201 |
|
202 | return this._size;
|
203 | }
|
204 | }, {
|
205 | key: 'parsedSize',
|
206 | get: function get() {
|
207 | if (!_.has(this, '_parsedSize')) {
|
208 | this._parsedSize = this.walk(function (node, size, stop) {
|
209 | if (node.parsedSize === undefined) {
|
210 | return stop(undefined);
|
211 | }
|
212 |
|
213 | return size + node.parsedSize;
|
214 | }, 0);
|
215 | }
|
216 |
|
217 | |
218 |
|
219 |
|
220 |
|
221 | return this._parsedSize;
|
222 | }
|
223 | }]);
|
224 |
|
225 | return Folder;
|
226 | }(Node);
|
227 |
|
228 | module.exports = {
|
229 | Node: Node,
|
230 | File: File,
|
231 | Folder: Folder
|
232 | }; |
\ | No newline at end of file |